Shell脚本—版本库文件夹备份
应公司要求,需要每月对版本库,scm文件夹下的文件进行全量备份并在备份结束后,发送内网邮件进行通知,故想到写一个shell脚本,进行每月自动备份。备份脚本如下
#!/usr/bin/bash
## samba scm backup
## by shanhai
file_name=scm_backup_`date +%F`.tar.gz
sorce_file=/home/gbase/app/samba/mount
destination_file=/data/scm_backup/
remote_address=root@10.0.0.2:
remote_file=/scm_backup
tar -czPf $destination_file/$file_name $source_file
find $destination_file -name "$file_name"
if [ $? -eq 0 ];then
/usr/bin/python3 /home/gbase/app/scm_backup.py
else
echo "scm backup is faild,Please check it"
exit
fi
/usr/bin/expect <<-EOF
set timeout -1
spawn scp $destination_file$file_name $remote_address$romote_file
expect {
"(yes/no?)" { send "yes\r"; exp_continue }
"password:" { send "password\r" };
}
expect "100%"
expect eof
EOF
邮件发送脚本如下
#!/usr/bin/env python3
import time
import sys
from datetime import datetime
import subprocess
time_start=time.time()
datetime.now()
current_tm=datetime.now().strftime('%Y%m%d%H%M')
today=datetime.now().strftime('%Y-%m-%d')
import smtplib
from socket import gaierror
port = 25
smtp_server = "10.0.0.99"
login = "**@**.com"
password = "****"
sender - "**@**,cin"
receiver_1 = "shanhai@**.com"
receiver_2 = "**@**.com"
message - f"""\
Subject: SCM Backup Norification E-Mail {today}
To: {receiver_1},{receiver_2}
From: {sender}
------------------------------------------------------------
The scm_package backup is successful
The path is 10.0.0.1/data/scm_backup and 10.0.0.2/scm_backup
------------------------------------------------------------
This is a message by scm_backup with Pythn."""
try:
with smtplib.SMTP(smtp_server, port) as server:
server.login(login, password)
server.sendmail(sender, receiver_1, message)
server.sendmail(sencer, receiver_2, message)
print('Sent')
except (gaierror, ConnectionRefusecdError):
print('Failed to connect to the server. Bad connection settings?')
except smtplib.SMTPServerDisconnected:
print('Failed to connect to the server. Wrong user/password?')
except smtplib.SMTPException as e:
print('SMTP error occurred: ' + str(e))
计划任务如下
0 6 5,20 * * /bin/sh /home/gbase/app/scm_backup.sh