Shell脚本-版本库备份

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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值