python脚本实现文件备份
本脚本通过判断是否为周一来执行完全备份或增量备份,需提前放到计划任务中每天执行,实现周一完全备份,之后每天增量备份的功能.具体代码实现如下
#!/root/bin/python
from time import strftime
import os
import tarfile
import hashlib
import pickle
def check_md5(fname):
m = hashlib.md5()
with open(fname,'rb') as fobj:
while 1:
data = fobj.read(4096)
if not data:
break
m.update(data)
return m.hexdigest()
def full_backup(src,dst,md5file):
fname = os.path.basename(src)
fname = '%s_full_%s.tar.gz'% (fname,strftime('%Y%m%d'))
fname = os.path.join(dst,fname)
tar = tarfile.open(fname,'w:gz')
tar.add(src)
tar.close()
md5dict = {}
for path,folders,files in os.walk(src):
for file in files:
key = os.path.join(path,file)
md5dict[key] = check_md5(key)
with open(md5file,'wb') as fobj:
pickle.dump(md5dict,fobj)
def incr_backup(src,dst,md5file):
fname = os.path.basename(src)
fname = '%s_incr_%s.tar.gz'% (fname,strftime('%Y%m%d'))
fname = os.path.join(dst,fname)
md5dict = {}
for path,folders,files in os.walk(src):
for file in files:
key = os.path.join(path,file)
md5dict[key] = check_md5(key)
with open(md5file, 'rb') as fobj:
old_md5 = pickle.load(fobj)
tar = tarfile.open(fname, 'w:gz')
for key in md5dict:
if old_md5.get(key) != md5dict[key]:
tar.add(key)
tar.close()
with open(md5file,'wb') as fobj:
pickle.dump(md5dict,fobj)
if __name__ == '__main__':
src = '/tmp/demo/security'
dst = '/tmp/demo/backup'
md5file = '/tmp/demo/backup/md5.data'
if strftime('%a') == 'Mon':
full_backup(src,dst,md5file)
else:
incr_backup(src,dst,md5file)