欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python3 实现完全备份、增量备份目录以及文件

程序员文章站 2022-10-03 13:17:49
import osfrom time import strftimeimport pickle as pimport tarfileimport hashlib#运行脚本,每周四进行完全备份,其他时间进行增量备份。src_dir = '/root/security' #定义源目录dst_dir = '/tmp/mybackup' #定义目标目录md5_file = '/tmp/m....
import os
from time import strftime
import pickle as p
import tarfile
import hashlib
#运行脚本,每周四进行完全备份,其他时间进行增量备份。

src_dir = '/root/security'                            #定义源目录
dst_dir = '/tmp/mybackup'                             #定义目标目录
md5_file = '/tmp/mybackup/md5.data'                   #采用MD5进行校验
nowdate = strftime('%Y-%m-%d')


def check_md5(fname):                      #定义MD5处理函数,每次处理4096字节更新MD5值
    m = hashlib.md5()
    with open(fname, 'rb') as fobj:
        data = fobj.read(4096)
        if data:
            m.update(data)
    return m.hexdigest()


def full_backup(src_dir, dst_dir, md5_file):
    md5_dict = {}
    fname = 'full_backup' + os.path.basename(src_dir) + '-' + nowdate + '.tar.gz'
    path_file = os.path.join(dst_dir, fname)
    tar = tarfile.open(path_file, 'w:gz')
    tar.add(src_dir)
    tar.close()
    for path, folders, files in os.walk(src_dir):   #采用os的walk遍历目录
        for each_file in files:
            key = os.path.join(path, each_file)
            md5_dict[key] = check_md5(key)
    with open(md5_file, 'wb') as fobj:
        p.dump(md5_dict, fobj)


def incr_backup(src_dir, dst_dir, md5_file):
    fname = 'incr_backup' + os.path.basename(src_dir) + '-' + nowdate + '.tar.gz'
    path_file = os.path.join(dst_dir, fname)
    with open(md5_file, 'rb') as fobj:
        ori_md5_dict = p.load(fobj)
    new_md5_dict = {}
    tar = tarfile.open(path_file, 'w:gz')
    for path, folders, files in os.walk(src_dir):
        for each_file in files:
            key = os.path.join(path, each_file)
            new_md5_dict[key] = check_md5(key)
            if ori_md5_dict.get(key) != new_md5_dict[key]:
                tar.add(key)
    tar.close()


if __name__ == '__main__':
    if not os.path.exists(dst_dir):
        os.mkdir(dst_dir)
    if strftime('%a') == 'Thu':
        full_backup(src_dir, dst_dir, md5_file)
    else:
        incr_backup(src_dir, dst_dir, md5_file)

 

本文地址:https://blog.csdn.net/qq_27592485/article/details/109643322