#-*- coding:utf-8 -*-
import sys,os,shutil,filecmp
MAXVERSIONS = 100
def backup(tree_top, bakdir_name="bakdir"):
for dir,subdirs,files in os.walk(tree_top):
#确保每个目录都有一个备份目录
backup_dir = os.path.join(dir,bakdir_name)
if not os.path.exists(backup_dir):
os.makedirs(backup_dir)
#停止对备份目录的递归
subdirs[:] = [d for d in subdirs if d != bakdir_name]
for file in files:
filepath = os.path.join(dir,file)
destpath = os.path.join(backup_dir,file)
#检查以前的版本是否存在
for index in xrange(MAXVERSIONS):
backfile = '%s.%2.2d' % (destpath, index)
if not os.path.exists(backfile):
break
if index > 0:
old_backup = '%s.%2.2d' % (destpath,index-1)
abspath = os.path.abspath(filepath)
try:
if os.path.isfile(old_backup) and filecmp.cmp(abspath, old_backup,shallow=False):
continue
except OSError:
pass
try:
shutil.copy(filepath,backfile)
except OSError:
pass
if __name__ == '__main__':
try:
tree_top = sys.argv[1]
except IndexError:
tree_top = '.'
backup(tree_top)