需求:
在/root/backup下面有两个文件夹dst和src。要求在周一的时候进行彻底备份,其他日子进行增量备份。从src备份到dst。python
思路及关键点:web
#!/usr/bin/env python
import time
import os
import cPickle as p
import tarfile
import hashlib
baseDir = '/root/backup'
srcDir = 'src'
dstDir = 'dst'
fullName = "full_%s_%s.tar.gz" % (srcDir, time.strftime('%Y%m%d'))
incrName = "incr_%s_%s.tar.gz" % (srcDir, time.strftime('%Y%m%d'))
md5file = 'md5.data'
def md5sum(fname):
m = hashlib.md5()
with file(fname) as f:
while True:
data = f.read(4096)
if len(data) == 0:
break
m.update(data)
return m.hexdigest()
def fullBackup():
md5Dict = {}
fileList = os.listdir(os.path.join(baseDir,srcDir))
for eachFile in fileList:
md5Dict[eachFile] = md5sum(os.path.join(baseDir,srcDir,eachFile))
with file(os.path.join(baseDir,dstDir,md5file),'w') as f:
p.dump(md5Dict,f)
tar = tarfile.open(os.path.join(baseDir,dstDir,fullName),'w:gz')
os.chdir(baseDir)
tar.add(srcDir)
tar.close()
def incrBackup():
newmd5 = {}
fileList = os.listdir(os.path.join(baseDir,srcDir))
for eachFile in fileList:
newmd5[eachFile] = md5sum(os.path.join(baseDir,srcDir,eachFile))
with file(os.path.join(baseDir,dstDir,md5file)) as f:
storedmd5 = p.load(f)
tar = tarfile.open(os.path.join(baseDir,dstDir,incrName),'w:gz')
os.chdir(baseDir)
for eachKey in newmd5:
if (eachKey not in storedmd5) or (newmd5[eachKey] != storedmd5[eachKey]):
tar.add(os.path.join(srcDir,eachKey))
tar.close()
with file(os.path.join(baseDir,dstDir,md5file),'w') as f:
p.dump(newmd5,f)
def main():
if time.strftime('%a') == 'Mon':
fullBackup()
else:
incrBackup()
if __name__ == '__main__':
main()
~