用Python写脚本,实现完全备份和增量备份的示例


Posted in Python onApril 29, 2018

需求:

在/root/backup下面有两个文件夹dst和src。要求在周一的时候进行完全备份,其余日子进行增量备份。从src备份到dst。

思路及关键点:

建立一个文件,以字典方式记录src的文件名以及文件对应的md5的值

完全备份的时候将文件名和md5值写在一个文件里面。cPickle的知识点。

增量备份的时候比较文件名是否在key里面,没有就要备份;有的话,这个文件的md5值是否改变,改变了就要备份

os.path.join()拼接路径,os.listdir(),os.chdir()

time.strftime()判断周几

cPickle,可以无损记录所有Python的变量类型。文件操作。

tarfile对文件打包的使用

hashlib用于计算文件md5的值。注意不要一次打开一个文件,4k地打开,防止打开一个超大文件爆内存。

with file()可以打开一个文件之后不f.close()

#!/usr/bin/env python
import time
import os
import cPickle as p
import tarfile
import hashlib
baseDir = '/root/backup'
srcDir = 'src'
dstDir = 'dst'
fullName = "full_%s_%s.tar.gz" % (srcDir, time.strftime('%Y%m%d'))
incrName = "incr_%s_%s.tar.gz" % (srcDir, time.strftime('%Y%m%d'))
md5file = 'md5.data'
def md5sum(fname):
 m = hashlib.md5()
 with file(fname) as f:
  while True:
   data = f.read(4096)
   if len(data) == 0:
    break
   m.update(data)
 return m.hexdigest()
def fullBackup():
 md5Dict = {}
 fileList = os.listdir(os.path.join(baseDir,srcDir))
 for eachFile in fileList:
  md5Dict[eachFile] = md5sum(os.path.join(baseDir,srcDir,eachFile))
 with file(os.path.join(baseDir,dstDir,md5file),'w') as f:
  p.dump(md5Dict,f)
 tar = tarfile.open(os.path.join(baseDir,dstDir,fullName),'w:gz')
 os.chdir(baseDir)
 tar.add(srcDir)
 tar.close()
def incrBackup():
 newmd5 = {}
 fileList = os.listdir(os.path.join(baseDir,srcDir))
 for eachFile in fileList:
  newmd5[eachFile] = md5sum(os.path.join(baseDir,srcDir,eachFile))
 with file(os.path.join(baseDir,dstDir,md5file)) as f:
  storedmd5 = p.load(f)
 tar = tarfile.open(os.path.join(baseDir,dstDir,incrName),'w:gz')
 os.chdir(baseDir)
 for eachKey in newmd5:
  if (eachKey not in storedmd5) or (newmd5[eachKey] != storedmd5[eachKey]):
   tar.add(os.path.join(srcDir,eachKey))
 tar.close()
 with file(os.path.join(baseDir,dstDir,md5file),'w') as f:
  p.dump(newmd5,f)
def main():
 if time.strftime('%a') == 'Mon':
  fullBackup()
 else:
  incrBackup()
if __name__ == '__main__':
 main()
~

以上这篇用Python写脚本,实现完全备份和增量备份的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python获取远程图片大小和尺寸的方法
Mar 26 Python
Python中用于检查英文字母大写的isupper()方法
May 19 Python
Python定时执行之Timer用法示例
May 27 Python
详解Python多线程Selenium跨浏览器测试
Apr 01 Python
python使用opencv进行人脸识别
Apr 07 Python
python实时分析日志的一个小脚本分享
May 07 Python
Python爬虫实现获取动态gif格式搞笑图片的方法示例
Dec 24 Python
Python 中包/模块的 `import` 操作代码
Apr 22 Python
浅析Django 接收所有文件,前端展示文件(包括视频,文件,图片)ajax请求
Mar 09 Python
使用python客户端访问impala的操作方式
Mar 28 Python
Python pymsql模块的使用
Sep 07 Python
Python基于百度API识别并提取图片中文字
Jun 27 Python
基于python的多进程共享变量正确打开方式
Apr 28 #Python
基于Python log 的正确打开方式
Apr 28 #Python
python+pandas分析nginx日志的实例
Apr 28 #Python
详谈套接字中SO_REUSEPORT和SO_REUSEADDR的区别
Apr 28 #Python
python实现关键词提取的示例讲解
Apr 28 #Python
python实现扫描日志关键字的示例
Apr 28 #Python
python socket网络编程之粘包问题详解
Apr 28 #Python
You might like
如何利用php+mysql保存和输出文件
2006/10/09 PHP
第八节--访问方式
2006/11/16 PHP
zf框架的session会话周期及次数限制使用示例
2014/03/13 PHP
php实现简单的上传进度条
2015/11/17 PHP
php pthreads多线程的安装与使用
2016/01/19 PHP
深入理解PHP中的empty和isset函数
2016/05/26 PHP
Yii安装与使用Excel扩展的方法
2016/07/13 PHP
PHP实现的简单对称加密与解密方法实例小结
2017/08/28 PHP
JQuery textlimit 显示用户输入的字符数 限制用户输入的字符数
2009/05/14 Javascript
js TextArea的选中区域处理
2010/12/28 Javascript
Jquery利用mouseenter和mouseleave实现鼠标经过弹出层且可以点击
2014/02/12 Javascript
利用JS生成博文目录及CSS定制博客
2016/02/10 Javascript
JavaScript中push(),join() 函数 实例详解
2016/09/06 Javascript
浅谈javascript中执行环境(作用域)与作用域链
2016/12/08 Javascript
利用JS hash制作单页Web应用的方法详解
2017/10/10 Javascript
nodejs中art-template模板语法的引入及冲突解决方案
2017/11/07 NodeJs
解决在Vue中使用axios POST请求变成OPTIONS的问题
2020/08/14 Javascript
在CentOS6上安装Python2.7的解决方法
2018/01/09 Python
Python爬取数据并写入MySQL数据库的实例
2018/06/21 Python
python sort、sort_index方法代码实例
2019/03/28 Python
Python Django框架url反向解析实现动态生成对应的url链接示例
2019/10/18 Python
使用Python刷淘宝喵币(低阶入门版)
2019/10/30 Python
keras训练浅层卷积网络并保存和加载模型实例
2020/07/02 Python
python中xlutils库用法浅析
2020/12/29 Python
Mavi牛仔裤美国官网:土耳其著名牛仔品牌
2016/09/24 全球购物
Under Armour澳大利亚官网:美国知名的高端功能性运动品牌
2018/02/22 全球购物
阿玛尼美妆俄罗斯官网:Giorgio Armani Beauty RU
2020/07/19 全球购物
市级优秀班主任事迹材料
2014/05/13 职场文书
图书馆标语
2014/06/19 职场文书
海上钢琴师观后感
2015/06/03 职场文书
学术会议领导致辞
2015/07/29 职场文书
学校教师培训工作总结
2015/10/14 职场文书
2016年小学教师政治学习心得体会
2016/01/23 职场文书
python opencv通过按键采集图片源码
2021/05/20 Python
用 Python 定义 Schema 并生成 Parquet 文件详情
2021/09/25 Python
SQL SERVER实现连接与合并查询
2022/02/24 SQL Server