(一)场景
就是做一备份文件的脚本,功能很简单,备份完成,发邮件通知,每天定时运行,并且自动删除前一周的数据(注:目前只有备份,和发邮件,其他的功能陆续加上)
(二)代码
# -*- coding : utf-8 -*- import os import time import tarfile import string import smtplib import email logfile = "/usr/svnback/log/svnbak.log" bakdir = "/usr/svnback" svndir = "/usr/svncode" destdir = bakdir+"/"+time.strftime("%Y-%m-%d") today = time.strftime("%Y-%m-%d %H:%M:%S") def get_time(): now = time.ctime() parsed = time.strptime(now) return time.strftime("%Y-%m-%d %H:%M:%S")def write_log(time,info):
open_log = open(logfile,'a+')
open_log.write(time + ":" +info +"\n")
open_log.close()def compress_svn(destdir):
tar = tarfile.open(destdir+'.bz2','w|bz2')
tar.add(svndir)
write_log(today,"compress_svn")
tar.close()
sendmail(today+'svncode has bak')def sendmail(content):
mail_host ='smtp.163.com'
addrfrom = 'xxx@163.com'
addrto = 'xxx@qq.com'
msg = "To:xxxx@qq.com\r\nFrom:xxx@163.com\r\nSubject:svn backup problem\r\nDate:" + email.Utils.formatdate()+"\r\n\r\n" + content + "\r\n"
svr = smtplib.SMTP()
svr.connect(mail_host)
svr.login('username','pwd')
svr.sendmail(addrfrom,addrto,msg)
svr.close()if name=="main":
time = get_time()
write_log(time,"start back up!!")
if os.path.exists(destdir):
write_log(time,"back already exists!")
else:
compress_svn(destdir)
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于