• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Python实现文件的全备份和差异备份

其他 开心洋葱 2276次浏览 0个评论

Python实现文件的全备份和差异备份

之前有写利用md5方式来做差异备份,但是这种md5方式来写存在以下问题:

  • md5sum获取有些软连接的MD5值存在问题
  • 不支持对空目录进行备份,因为md5sum无法获取空目录的md5值
  • 权限的修改md5sum无法判断

解决方案

利用文件的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.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实现文件的全备份和差异备份
喜欢 (0)

您必须 登录 才能发表评论!

加载中……