备份程序

import os
import tarfile
import hashlib
import pickle as p
from time import strftime

def check_md5(fname):
m = hashlib.md5()

with open(fname, 'rb') as fobj:
    while True:
        data = fobj.read(4096)
        if not data:
            break
        m.update(data)

return m.hexdigest()

def full_backup(src_dir, dst_dir, md5file):
fname = “%s_full_%s.tar.gz” % \
(os.path.basename(src_dir.rstrip(‘/’)), strftime(‘%Y%m%d’))
fname = os.path.join(dst_dir, fname)
tar = tarfile.open(fname, ‘w:gz’)
tar.add(src_dir)
tar.close()

md5_dict = {}
for path, folders, files in os.walk(src_dir):
    for each_file in files:
        key = os.path.join(path, each_file)
        md5_dict[key] = check_md5(key)

with open(md5file, 'wb') as fobj:
    p.dump(md5_dict, fobj)

def incr_backup(src_dir, dst_dir, md5file):
fname = “%s_incr_%s.tar.gz” % \
(os.path.basename(src_dir.rstrip(‘/’)), strftime(‘%Y%m%d’))
fname = os.path.join(dst_dir, fname)

new_md5 = {}
with open(md5file, 'rb') as fobj:
    old_md5 = p.load(fobj)

for path, folders, files in os.walk(src_dir):
    for each_file in files:
        key = os.path.join(path, each_file)
        new_md5[key] = check_md5(key)

with open(md5file, 'wb') as fobj:
    p.dump(new_md5, fobj)

tar = tarfile.open(fname, 'w:gz')
for key in new_md5:
    if old_md5.get(key) != new_md5[key]:
        tar.add(key)
tar.close()

if name == ‘main‘:
# cp -r /etc/security /tmp
# mkdir /tmp/backup
src_dir = ‘/tmp/security’
dst_dir = ‘/tmp/backup/’
md5file = ‘/tmp/backup/md5.data’
if strftime(‘%a’) == ‘Mon’:
full_backup(src_dir, dst_dir, md5file)
else:
incr_backup(src_dir, dst_dir, md5file)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值