Python實現(xiàn)網(wǎng)站文件的全備份和差異備份
之前有寫利用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.dbjr.com.cn #測試創(chuàng)建文件,修改文件權限 $ 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.dbjr.com.cn
看了博主的代碼,很受啟發(fā),但是有一個問題,如果我完成完整備份之后,刪除了其中某個文件,再做差異備份,可以檢測出被刪除的文件,但是執(zhí)行tar就會出錯,因為這個文件已經(jīng)是不存在的了,所以在執(zhí)行tar之前,最好用os.path.exists()判斷一下差異文件路徑是否存在,如果不存在則不執(zhí)行tar, 反饋一條文件刪除信息。
相關文章
Python使用sftp實現(xiàn)上傳和下載功能(實例代碼)
在Python中可以使用paramiko模塊中的sftp登陸遠程主機,實現(xiàn)上傳和下載功能。接下來通過本文給大家介紹Python使用sftp實現(xiàn)上傳和下載功能,需要的朋友參考下2017-03-03Python實現(xiàn)在matplotlib中兩個坐標軸之間畫一條直線光標的方法
這篇文章主要介紹了Python實現(xiàn)在matplotlib中兩個坐標軸之間畫一條直線光標的方法,涉及Python操作matplotlib模塊繪圖的相關技巧,需要的朋友可以參考下2015-05-05Python3 使用pip安裝git并獲取Yahoo金融數(shù)據(jù)的操作
這篇文章主要介紹了Python3 使用pip安裝git并獲取Yahoo金融數(shù)據(jù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04python使用pip安裝SciPy、SymPy、matplotlib教程
今天小編大家分享一篇python使用pip安裝SciPy、SymPy、matplotlib教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11使用PyTorch構建神經(jīng)網(wǎng)絡的操作指南
PyTorch 是一個在研究領域廣泛使用的深度學習框架,提供了大量的靈活性和效率,本文將向你介紹如何使用 PyTorch 構建你的第一個神經(jīng)網(wǎng)絡,感興趣的小伙伴可以參考閱讀2023-07-07