Python 實現檔案的全備份和差異備份詳解,

來源:互聯網
上載者:User

Python 實現檔案的全備份和差異備份詳解,

Python實現檔案的全備份和差異備份

之前有寫利用md5方式來做差異備份,但是這種md5方式來寫存在以下問題:

  • md5sum擷取有些軟串連的MD5值存在問題
  • 不支援對空目錄進行備份,因為md5sum無法擷取空目錄的md5值
  • 許可權的修改md5sum無法判斷

解決方案:

利用檔案的mtime ctime

mtime(Modified time)是在寫入檔案時隨檔案內容的更改而更改的

ctime(Create time)是在寫入檔案、更改所有者、許可權或連結設定時隨Inode的內容更改而更改的

廢話不多說直接上代碼:

#!/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()

測試:

$ python file_backup.py data.pk 1 data data_$(date +%F) #全備份$ > data/www.linuxeye.com #測試建立檔案,修改檔案許可權$ 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

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.