日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

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

系統(tǒng) 1761 0

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

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

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

解決方案:

利用文件的mtime ctime

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

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

廢話不多說直接上代碼:

            
#!/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.linuxeye.com #測試創(chuàng)建文件,修改文件權(quán)限
$ 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.linuxeye.com

          

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


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!?。?/p>

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 额敏县| 中山市| 香格里拉县| 桦南县| 双鸭山市| 东明县| 钟祥市| 吴川市| 文昌市| 南溪县| 古田县| 突泉县| 绵阳市| 乐陵市| 清水县| 武夷山市| 陇川县| 石家庄市| 从化市| 视频| 含山县| 临潭县| 镇赉县| 惠州市| 洪雅县| 门头沟区| 阳新县| 资兴市| 崇州市| 沙坪坝区| 新竹市| 泽普县| 桑日县| 高陵县| 那坡县| 远安县| 宝山区| 澜沧| 江城| 汉寿县| 安达市|