黑松山资源网 Design By www.paidiu.com
之前有写利用md5方式来做差异备份,但是这种md5方式来写存在以下问题:
"color: #ff0000">利用文件的mtime ctime
mtime(Modified time)是在写入文件时随文件内容的更改而更改的
ctime(Create time)是在写入文件、更改所有者、权限或链接设置时随Inode的内容更改而更改的
废话不多说直接上代码:
#!/usr/bin/env python import time,os,sys,cPickle fileInfo = {} def logger(time,fileName,status,fileNum): f = open('backup.log','a') f.write("%s\t%s\t%s\t\t%s\n" % (time,fileName,status,fileNum)) def tar(sDir,dDir,fileNum): command = "tar zcf %s %s >/dev/null 2>&1" % (dDir + ".tar.gz",sDir) if os.system(command) == 0: logger(time.strftime('%F %X'),dDir + ".tar.gz",'success',fileNum) else: logger(time.strftime('%F %X'),dDir + ".tar.gz",'failed',fileNum) def fullBak(path): fileNum = 0 for root,dirs,files in os.walk(path): for name in files: file = os.path.join(root, name) mtime = os.path.getmtime(file) ctime = os.path.getctime(file) fileInfo[file] = (mtime,ctime) fileNum += 1 f = open(P,'w') cPickle.dump(fileInfo,f) f.close() tar(S,D,fileNum) def diffBak(path): for root,dirs,files in os.walk(path): for name in files: file = os.path.join(root,name) mtime = os.path.getmtime(file) ctime = os.path.getctime(file) fileInfo[file] = (mtime,ctime) if os.path.isfile(P) == 0: f = open(P,'w') f.close() if os.stat(P).st_size == 0: f = open(P,'w') cPickle.dump(fileInfo,f) fileNum = len(fileInfo.keys()) f.close() print fileNum tar(S,D,fileNum) else: f = open(P) old_fileInfo = cPickle.load(f) f.close() difference = dict(set(fileInfo.items())^set(old_fileInfo.items())) fileNum = len(difference) print fileNum difference_file = ' '.join(difference.keys()) print difference_file tar(difference_file,D,fileNum) f = open(P,'w') cPickle.dump(fileInfo,f) f.close() def Usage(): print ''' Syntax: python file_bakcup.py pickle_file model source_dir filename_bk model: 1:Full backup 2:Differential backup example: python file_backup.py fileinfo.pk 2 /etc etc_$(date +%F) explain: Automatically add '.tar.gz' suffix ''' sys.exit() if len(sys.argv) != 5: Usage() P = sys.argv[1] M = int(sys.argv[2]) S = sys.argv[3] D = sys.argv[4] if M == 1: fullBak(S) elif M == 2: diffBak(S) else: print "\033[;31mDoes not support this mode\033[0m" Usage()
测试:
$ python file_backup.py data.pk 1 data data_$(date +%F) #全备份 $ > data/www.jb51.net #测试创建文件,修改文件权限 $ chmod 777 data/py/eshop_bk/data.db $ python file_backup.py data.pk 2 data data_$(date +%F)_1 #备份改变的文件 2 data/py/eshop_bk/data.db data/www.jb51.net
看了博主的代码,很受启发,但是有一个问题,如果我完成完整备份之后,删除了其中某个文件,再做差异备份,可以检测出被删除的文件,但是执行tar就会出错,因为这个文件已经是不存在的了,所以在执行tar之前,最好用os.path.exists()判断一下差异文件路径是否存在,如果不存在则不执行tar, 反馈一条文件删除信息。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年10月10日
2024年10月10日
- 《原神》「星路拾忆」网页活动壁纸展示
- 群星.2024-祝你幸福!电影原声带【阿里巴巴】【FLAC分轨】
- 于台烟.2004-执着【乾坤唱片】【WAV+CUE】
- 黄品源.2004-感谢·情人(引进版)【滚石】【WAV+CUE】
- 《最终幻想16》评测:游、戏两难全的系列转折
- 《纸嫁衣》五部打包¥104,《鬼泣5》仅需¥44
- 《崩坏:星穹铁道》造物大赛卷出新高度
- 《王菲 菲常传奇 2CD》[WAV+CUE][950MB]
- 《王菲 空灵独味唱腔 非常传奇 紫水晶2CD》[WAV+CUE][860MB]
- 《车载专业测试王1号 极品音质》[WAV/分轨][980MB]
- 炉石传说的国服回归送什么奖励 炉石传说9.25回归最新消息介绍
- 云顶之弈最强阵容汇总2024 云顶之弈最强阵容搭配最新版本
- S14lol全球总决赛在哪观看 lol全球总决赛观赛直播地址汇总
- 黄思婷2011-感谢天[豪记唱片][WAV+CUE]
- 孙淑媚2013-太阳[台湾首版][WAV+CUE]