Python实现文件的全备份和差异备份
2016-09-28 09:43
507 查看
转自 http://www.75271.com/1258.html Python实现文件的全备份和差异备份之前有写利用md5方式来做差异备份,但是这种md5方式来写存在以下问题:md5sum获取有些软连接的MD5值存在问题不支持对空目录进行备份,因为md5sum无法获取空目录的md5值权限的修改md5sum无法判断解决方案:利用文件的mtime ctimemtime(Modified time)是在写入文件时随文件内容的更改而更改的ctime(Create time)是在写入文件、更改所有者、权限或链接设置时随Inode的内容更改而更改的废话不多说直接上代码:#!/usr/bin/env <a href="http://www.75271.com/tag/python" title="查看更多关于python的文章" target="_blank">python</a>import time,os,sys,cPicklefileInfo = {}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 = 0for 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 += 1f = 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 fileNumtar(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 fileNumdifference_file = ' '.join(difference.keys())print difference_filetar(difference_file,D,fileNum)f = open(P,'w')cPickle.dump(fileInfo,f)f.close()def Usage():print '''Syntax: <a href="http://www.75271.com/tag/python" title="查看更多关于python的文章" target="_blank">python</a> file_bakcup.py pickle_file model source_dir filename_bkmodel: 1:Full backup 2:Differential backupexample: <a href="http://www.75271.com/tag/python" title="查看更多关于python的文章" target="_blank">python</a> 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.75271.com #测试创建文件,修改文件权限 $ chmod 777 data/py/75271com_bk/data.db $ python file_backup.py data.pk 2 data data_$(date +%F)_1 #备份改变的文件 2 data/py/75271com_bk/data.db data/www.75271.com
相关文章推荐
- Python实现网站文件的全备份和差异备份
- Python 实现文件的全备份和差异备份详解
- Python实现网站文件的全备份和差异备份
- Python实现linux下文件备份
- Python实现备份文件实例
- python difflib模块实现两个文件差异对比,并输出html格式。
- python-实现重要文件的备份
- 用python编写脚本实现备份文件
- Python在Windows下实现文件备份 脚本程序 版本1
- Python 通过difflib模块实现文件内容差异对比
- python3 实现对比conf 文件差异
- python实现数通设备tftp备份配置文件示例
- Python脚本实现windows下文件的备份
- Python实现文件备份
- 【Python笔记】no.3 练习-实现linux下的差异备份
- Python实现目录文件的全量和增量备份
- Python实现备份文件实例
- Python实现配置文件备份的方法
- Python实现简单的文件传输与MySQL备份的脚本分享
- python实现windows下文件备份脚本