1 案例1:备份程序
1.1 问题
编写backup.py脚本,实现以下目标:
需要支持完全和增量备份
周一执行完全备份
其他时间执行增量备份
备份文件需要打包为tar文件并使用gzip格式压缩
- 1
- 2
- 3
- 4
1.2 方案
整体框架创建3个函数,分别实现完全备份、增量备份、文件加密3种功能:
1.首先导入time模块,利用if进行判断,如果当地时间是星期一,执行完全备份函数,否则执行增量备份函数,其中,通配符%a代表时间星期几缩写,上传参数分别为要备份的原目录、目标目录、md5字典存放目录
2.调用完全备份函数:
a)首先获取新文件名,将新文件名放入目标目录下,目的是定义备份文件的绝对路径,以写压缩方式打开目标目录下新文件,将原目录写入新文件中,完成完全备份,其中os.path.join作用是将目录名和文件的基名拼接成一个完整的路径
b)了解os.walk()目录遍历器输出文件结构,利用for循环将要备份原目录中文件遍历出来作为字典键值对键, md5加密结果作为字典键值对的值(此时将原目录中文件作为上传参数调用文件加密函数),存入空字典中,字典中每个文件对应一个md5值,最后将字典写入到md5字典存放目录中
3.调用文件加密函数:将原目录文件循环读取逐一加密,返回加密结果
4.调用增量备份函数:
a)增量备份函数代码与完全备份函数基本一致
b)区别在于,备份前要先以二进制读方式打开md5字典存放目录,读取旧数据,判断旧数据中键对应的加密值与新加密值是否相同,如果不相同,则将新增内容写入到目标文件中(即只备份新数据)
5.注意:md5主要用于原文件与新文件判断
1.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:编写脚本
[root@localhost day06]# vim backup.py
#!/usr/bin/env python3
import time
import os
import tarfile
import hashlib
import pickle
#用于判断两个文件是否相同,提取每个文件中的前4字节的内容然后输出md5码进行比较
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 = os.path.basename(src_dir.rstrip('/'))
fname = '%s_full_%s.tar.gz' % (fname, time.strftime('%Y%m%d'))
fname = os.path.join(dst_dir, fname)
md5dict = {}
tar = tarfile.open(fname, 'w:gz')
tar.add(src_dir)
tar.close()
for path, folders, files in os.walk(src_dir):
for each_file in files:
key = os.path.join(path, each_file)
md5dict[key] = check_md5(key)
with open(md5file, 'wb') as fobj:
pickle.dump(md5dict, fobj)
def incr_backup(src_dir, dst_dir, md5file):
fname = os.path.basename(src_dir.rstrip('/'))
fname = '%s_incr_%s.tar.gz' % (fname, time.strftime('%Y%m%d'))
fname = os.path.join(dst_dir, fname)
md5dict = {}
with open(md5file, 'rb') as fobj:
oldmd5 = pickle.load(fobj)
for path, folders, files in os.walk(src_dir):
for each_file in files:
key = os.path.join(path, each_file)
md5dict[key] = check_md5(key)
with open(md5file, 'wb') as fobj:
pickle.dump(md5dict, fobj)
tar = tarfile.open(fname, 'w:gz')
for key in md5dict:
if oldmd5.get(key) != md5dict[key]:
tar.add(key)
tar.close()
if __name__ == '__main__':
# mkdir /tmp/demo; cp -r /etc/security /tmp/demo
src_dir = '/tmp/demo/security'
dst_dir = '/var/tmp/backup' # mkdir /var/tmp/backup
md5file = '/var/tmp/backup/md5.data'
if time.strftime('%a') == 'Mon':
full_backup(src_dir, dst_dir, md5file)
else:
incr_backup(src_dir, dst_dir, md5file)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
步骤二:测试脚本执行
[root@localhost day07]# python3 backup.py
[root@localhost day07]# cd /var/tmp/backup/
[root@localhost backup]# ls
md5.data security_full_20180502.tar.gz security_incr_20180502.tar.gz
- 1
- 2
- 3
- 4
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
<div class="more-toolbox">
<div class="left-toolbox">
<ul class="toolbox-list">
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count"></span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
<img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
<img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{"mod":"popu_379"}" target="_blank">解启超</a></span>
</div>
<div class="text"><span>发布了404 篇原创文章</span> · <span>获赞 56</span> · <span>访问量 4万+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm attented bt-button personal-watch" data-report-click="{"mod":"popu_379"}">已关注</a>
</div>
</div>
</div>
</article>