python实现对文件的全量、增量备份
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python实现对文件的全量、增量备份,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3447字,纯文字阅读大概需要5分钟。
内容图文
# !/user/bin/env python # @Time :2018/6/6 10:10 # @Author :PGIDYSQ # @File :FileBackup2.py import os import filecmp import shutil import sys import time,sched ‘‘‘ 定时任务备份,增量备份 ‘‘‘ schedule = sched.scheduler(time.time, time.sleep) def autoBackup(scrDir,dstDir): if((not os.path.isdir(scrDir))or(not os.path.isdir(dstDir))or (os.path.abspath(scrDir) != scrDir)or(os.path.abspath(dstDir) != dstDir)): usage() for item in os.listdir(scrDir): scrItem = os.path.join(scrDir,item) dstItem= scrItem.replace(scrDir,dstDir) if os.path.isdir(scrItem): #创建新增加的文件夹,保证目标文件夹结构与原始文件一致ifnot os.path.exists(dstItem): os.makedirs(dstItem) print(‘make directory‘+dstItem) #递归调用自身函数 autoBackup(scrItem,dstItem) elif os.path.isfile(scrItem): #只复制新增或修改的文件if((not os.path.exists(dstItem))or(not filecmp.cmp(scrItem,dstItem,shallow=False))): shutil.copyfile(scrItem,dstItem) print(‘file:‘+scrItem+‘==>‘+dstItem,os.system(‘echo %time%‘)) schedule.enter(10, 0, autoBackup, (scrDir, dstDir)) def usage(): print(‘Error‘) print(‘For example:{0}‘.format(sys.argv[0])) sys.exit(0) if__name__ == "__main__": # if len(sys.argv) != 3:# usage()# scrDir,dstDir = sys.argv[1],sys.argv[2] scrDir, dstDir = r‘E:\PyCharm\WorkSpace\TestPkg\base\src‘,r‘E:\PyCharm\WorkSpace\TestPkg\base\dest‘#定时周期执行备份任务 schedule.enter(10, 0, autoBackup, (scrDir,dstDir)) schedule.run() # 持续运行,直到计划时间队列变成空为止#autoBackup(scrDir,dstDir)
#
!/user/bin/env python
#
@Time :2018/6/6 10:10
#
@Author :PGIDYSQ
#
@File :FileBackup.py
import
time
import
os
import
tarfile
import
shutil
import
pickle as p
import
hashlib
‘‘‘
实现每周一全量备份,其余时间增量备份
‘‘‘
def
md5check(fname):
m = hashlib.md5()
with open(fname) as fobj:
while True:
data = fobj.read(4096)
ifnot data:
break
m.update(data.encode())#payreturn m.hexdigest()
def full_backup(src_dir: object, dst_dir, md5file):
par_dir, base_dir = os.path.split(src_dir.rstrip(‘/‘))
back_name = ‘%s_full_%s.tar.gz‘ % (base_dir, time.strftime(‘%Y%m%d‘))
full_name = os.path.join(dst_dir, back_name)
md5dict = {}
# tar = tarfile.open(full_name, ‘w:gz‘)# tar.add(src_dir)# tar.close()if os.path.exists(dst_dir):
shutil.rmtree(dst_dir)
shutil.copytree(src_dir,dst_dir)#拷贝文件#shutil.make_archive(dst_dir,‘zip‘,src_dir)#将文件压缩,注:如果压缩tar,中文文件名有可能乱码for path, folders, files in os.walk(src_dir):
for fname in files:
full_path = os.path.join(path, fname)
md5dict[full_path] = md5check(full_path)
if os.path.exists(md5file):
with open(md5file, ‘wb‘) as f0:
p.dump(md5dict, f0)
else:
with open(md5file,‘xb‘) as f1:
p.dump(md5dict,f1)
def incr_backup(src_dir, dst_dir, md5file):
par_dir, base_dir = os.path.split(src_dir.rstrip(‘/‘))
back_name = ‘%s_incr_%s.tar.gz‘ % (base_dir, time.strftime(‘%Y%m%d‘))
full_name = os.path.join(dst_dir, back_name)
md5new = {}
for path, folders, files in os.walk(src_dir):
for fname in files:
full_path = os.path.join(path, fname)
md5new[full_path] = md5check(full_path)
with open(md5file,‘rb‘) as fobj:
md5old = p.load(fobj)
with open(md5file, ‘wb‘) as fobj:
p.dump(md5new, fobj)
#tar = tarfile.open(full_name, ‘w:gz‘)for key in md5new:
if md5old[key] != md5new[key]:
#tar.add(key) shutil.copyfile(key,dst_dir)
#tar.close()if__name__ == ‘__main__‘:
src_dir = ‘base/src‘
dst_dir = ‘base/dest/backupfile‘
md5file = ‘base/md5.data‘
full_backup(src_dir, dst_dir, md5file)
# if time.strftime(‘%a‘) == ‘Mon‘:# full_backup(src_dir, dst_dir, md5file)# else:# incr_backup(src_dir, dst_dir, md5file)
原文:https://www.cnblogs.com/ysq0908/p/9157380.html
内容总结
以上是互联网集市为您收集整理的python实现对文件的全量、增量备份全部内容,希望文章能够帮你解决python实现对文件的全量、增量备份所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。