国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python 實(shí)現(xiàn)文件的全備份和差異備份詳解

2019-11-25 16:26:07
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Python實(shí)現(xiàn)文件的全備份和差異備份

之前有寫(xiě)利用md5方式來(lái)做差異備份,但是這種md5方式來(lái)寫(xiě)存在以下問(wèn)題:

  • md5sum獲取有些軟連接的MD5值存在問(wèn)題
  • 不支持對(duì)空目錄進(jìn)行備份,因?yàn)閙d5sum無(wú)法獲取空目錄的md5值
  • 權(quán)限的修改md5sum無(wú)法判斷

解決方案:

利用文件的mtime ctime

mtime(Modified time)是在寫(xiě)入文件時(shí)隨文件內(nèi)容的更改而更改的

ctime(Create time)是在寫(xiě)入文件、更改所有者、權(quán)限或鏈接設(shè)置時(shí)隨Inode的內(nèi)容更改而更改的

廢話(huà)不多說(shuō)直接上代碼:

#!/usr/bin/env pythonimport 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 = 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()

測(cè)試:

$ python file_backup.py data.pk 1 data data_$(date +%F) #全備份$ > data/www.linuxeye.com #測(cè)試創(chuàng)建文件,修改文件權(quán)限$ chmod 777 data/py/eshop_bk/data.db$ python file_backup.py data.pk 2 data data_$(date +%F)_1 #備份改變的文件2data/py/eshop_bk/data.db data/www.linuxeye.com

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 寿阳县| 维西| 宿松县| 扎兰屯市| 科技| 廊坊市| 玉树县| 吴旗县| 平原县| 禄劝| 永清县| 黄浦区| 安康市| 永修县| 喀喇沁旗| 辰溪县| 仁布县| 武乡县| 汉寿县| 广东省| 连城县| 兴安县| 玉山县| 旌德县| 邹城市| 青阳县| 青河县| 遂宁市| 克山县| 日土县| 阿巴嘎旗| 深圳市| 丁青县| 怀来县| 夏邑县| 全州县| 庆安县| 梅河口市| 卓尼县| 类乌齐县| 江门市|