最近在研究关于svn的自动增量备份,然后自己写了个python脚本,实现了svn的增量备份,使用的话只需要修改上面3个路径,下面上代码:import os import time import sys bakdir='' svndir='' logdir='' timeformat='%Y-%m-%d %X' logformat='%Y-%m-%d' def max_index(list): max=list[0] for i in list: if i>max: max=i return max def do_bak(): num_list=[] list=[] for i in os.listdir(bakdir): num=i[:-5] l=num.split('-') for j in l: num_list.append(j) old=max_index(num_list) list.append('old version : %s \n'%old) youngest=os.popen('svnlook youngest %s'%svndir).read().strip() list.append('youngest version : %s \n'%youngest) logtime=time.strftime(logformat,time.localtime()) LogFile=logdir+os.sep+'%s.log'%logtime local_time=time.strftime(timeformat,time.localtime()) list.append('baktime = %s \n'%local_time) if old==youngest: list.append('no bak because of the version is not change! \n') list.append('-'*50) list.append('\n') logfile=open(LogFile,'a') logfile.writelines(list) logfile.close() else: old=str(int(old)+1) bakfile=bakdir+os.sep+'%s-%s.dump'%(old,youngest) if old==youngest: list.append('no bak because of the change is smaller than 2! \n') else: os.system('svnadmin dump %s -r %s:%s --incremental > %s'%(svndir,old,youngest,bakfile)) list.append('bakdir = %s \n'%bakdir) list.append('svndir = %s \n'%svndir) list.append('bakfile = %s \n'%bakfile) list.append('-'*50) list.append('\n') logfile=open(LogFile,'a') logfile.writelines(list) logfile.close() if __name__=='__main__': do_bak() 上面只是实现了svn的增量备份,要实现自动定期的备份,如果是linux只需要使用系统自带的crontab就可以了。 指令crontab -e,加入0 7 * * * python /.../svnbak.py,保存退出, 每天早上7点自动备份。 (责任编辑:IT) |