首页 > 编程语言 > 详细

备份:python脚本实现完全备份和增量备份

时间:2021-04-23 16:39:13      阅读:25      评论:0      收藏:0      [点我收藏+]
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 = f‘{os.path.basename(src)}_full_{strftime("%Y%m%d")}.tar.gz‘ fname = os.path.join(dst, fname) # 完全备份,打tar包 with tarfile.open(fname, ‘w:gz‘) as tar: tar.add(src) # 计算文件md5值 md5dict = {} for path, folders, files in os.walk(src): for file in files: key = os.path.join(path, file) md5dict[key] = check_md5(key) # 保存md5字典到md5file with open(md5file, ‘wb‘) as fobj: pickle.dump(md5dict, fobj) def incr_backup(src, dst, md5file): ‘增量备份‘ # 拼接备份文件的文件名 fname = f‘{os.path.basename(src)}_incr_{strftime("%Y%m%d")}.tar.gz‘ fname = os.path.join(dst, fname) # 计算文件md5值 md5dict = {} for path, folders, files in os.walk(src): for file in files: key = os.path.join(path, file) md5dict[key] = check_md5(key) # 取出前一天的md5值 with open(md5file, ‘rb‘) as fobj: old_md5 = pickle.load(fobj) # 找到新增文件和改变的文件,进行备份 with tarfile.open(fname, ‘w:gz‘) as tar: for key in md5dict: if old_md5.get(key) != md5dict[key]: tar.add(key) # 更新md5文件 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‘ # md5文件 # 如果是周一或从来没有备份过,就进行完全备份 if (strftime(‘%a‘) == ‘Mon‘) or (not os.path.exists(md5file)): full_backup(src, dst, md5file) else: incr_backup(src, dst, md5file) # mkdir -p /tmp/demo/backup # cp -r /etc/security /tmp/demo

备份:python脚本实现完全备份和增量备份

原文:https://www.cnblogs.com/stonejob/p/14693061.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!