Python 实现文件的全备份和差异备份详解


Posted in Python onDecember 27, 2016

Python实现文件的全备份和差异备份

之前有写利用md5方式来做差异备份,但是这种md5方式来写存在以下问题:

  • md5sum获取有些软连接的MD5值存在问题
  • 不支持对空目录进行备份,因为md5sum无法获取空目录的md5值
  • 权限的修改md5sum无法判断

解决方案:

利用文件的mtime ctime

mtime(Modified time)是在写入文件时随文件内容的更改而更改的

ctime(Create time)是在写入文件、更改所有者、权限或链接设置时随Inode的内容更改而更改的

废话不多说直接上代码:

#!/usr/bin/env python
import time,os,sys,cPickle
fileInfo = {}
def logger(time,fileName,status,fileNum):
  f = open('backup.log','a')
  f.write("%s\t%s\t%s\t\t%s\n" % (time,fileName,status,fileNum))
def tar(sDir,dDir,fileNum):
  command = "tar zcf %s %s >/dev/null 2>&1" % (dDir + ".tar.gz",sDir)
  if os.system(command) == 0:
    logger(time.strftime('%F %X'),dDir + ".tar.gz",'success',fileNum)
  else:
    logger(time.strftime('%F %X'),dDir + ".tar.gz",'failed',fileNum)
def fullBak(path):
  fileNum = 0
  for root,dirs,files in os.walk(path):
    for name in files:
      file = os.path.join(root, name)
      mtime = os.path.getmtime(file)
      ctime = os.path.getctime(file)
      fileInfo[file] = (mtime,ctime)
      fileNum += 1
  f = open(P,'w')
  cPickle.dump(fileInfo,f)
  f.close()
  tar(S,D,fileNum)
def diffBak(path):
  for root,dirs,files in os.walk(path):
    for name in files:
      file = os.path.join(root,name)
      mtime = os.path.getmtime(file)
      ctime = os.path.getctime(file)
      fileInfo[file] = (mtime,ctime)
  if os.path.isfile(P) == 0:
    f = open(P,'w')
    f.close()
  if os.stat(P).st_size == 0:
    f = open(P,'w')
    cPickle.dump(fileInfo,f)
    fileNum = len(fileInfo.keys())
    f.close()
    print fileNum
    tar(S,D,fileNum)
  else:
    f = open(P)
    old_fileInfo = cPickle.load(f)
    f.close()
    difference = dict(set(fileInfo.items())^set(old_fileInfo.items()))
    fileNum = len(difference)
    print fileNum
    difference_file = ' '.join(difference.keys())
    print difference_file
    tar(difference_file,D,fileNum)
    f = open(P,'w')
    cPickle.dump(fileInfo,f)
    f.close()
def Usage():
  print '''
    Syntax: python file_bakcup.py pickle_file model source_dir filename_bk
      model: 1:Full backup 2:Differential backup
    example: python file_backup.py fileinfo.pk 2 /etc etc_$(date +%F)
      explain: Automatically add '.tar.gz' suffix
  '''
  sys.exit()
if len(sys.argv) != 5:
  Usage()
P = sys.argv[1]
M = int(sys.argv[2])
S = sys.argv[3]
D = sys.argv[4]
if M == 1:
  fullBak(S)
elif M == 2:
  diffBak(S)
else:
  print "\033[;31mDoes not support this mode\033[0m"
  Usage()

测试:

$ python file_backup.py data.pk 1 data data_$(date +%F) #全备份
$ > data/www.linuxeye.com #测试创建文件,修改文件权限
$ chmod 777 data/py/eshop_bk/data.db
$ python file_backup.py data.pk 2 data data_$(date +%F)_1 #备份改变的文件
2
data/py/eshop_bk/data.db data/www.linuxeye.com

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Python 相关文章推荐
python实现用于测试网站访问速率的方法
May 26 Python
在Python的while循环中使用else以及循环嵌套的用法
Oct 14 Python
利用Python爬取微博数据生成词云图片实例代码
Aug 31 Python
Python与人工神经网络:使用神经网络识别手写图像介绍
Dec 19 Python
django-初始配置(纯手写)详解
Jul 30 Python
浅谈Python2之汉字编码为unicode的问题(即类似\xc3\xa4)
Aug 12 Python
python实现上传文件到linux指定目录的方法
Jan 03 Python
Python autoescape标签用法解析
Jan 17 Python
使用celery和Django处理异步任务的流程分析
Feb 19 Python
python如何快速拼接字符串
Oct 28 Python
python tqdm库的使用
Nov 30 Python
python脚本使用阿里云slb对恶意攻击进行封堵的实现
Feb 04 Python
Python Queue模块详细介绍及实例
Dec 27 #Python
Ubuntu 16.04 LTS中源码安装Python 3.6.0的方法教程
Dec 27 #Python
Python 递归函数详解及实例
Dec 27 #Python
python实现二维码扫码自动登录淘宝
Dec 27 #Python
使用Python的Scrapy框架十分钟爬取美女图
Dec 26 #Python
Python中如何获取类属性的列表
Dec 26 #Python
Python中强大的命令行库click入门教程
Dec 26 #Python
You might like
php正则校验用户名介绍
2008/07/19 PHP
PHP在线生成二维码(google api)的实现代码详解
2013/06/04 PHP
PHP使用redis实现统计缓存mysql压力的方法
2015/11/14 PHP
FleaPHP框架数据库查询条件($conditions)写法总结
2016/03/19 PHP
php下载文件超时时间的设置方法
2016/10/06 PHP
PHP排序算法之归并排序(Merging Sort)实例详解
2018/04/21 PHP
实例讲解通过​PHP创建数据库
2019/01/20 PHP
tp5(thinkPHP5框架)captcha验证码配置及验证操作示例
2019/05/28 PHP
Javascript异步编程的4种方法让你写出更出色的程序
2013/01/17 Javascript
一个简单的瀑布流效果(主体形式自写)
2013/05/27 Javascript
js计算系统当前日期是星期几的方法
2016/07/14 Javascript
javascript insertAfter()定义与用法示例
2016/07/25 Javascript
js常用DOM方法详解
2017/02/04 Javascript
Node.js中.pfx后缀文件的处理方法
2017/03/10 Javascript
浅谈angularJS的$watch失效问题的解决方案
2017/08/11 Javascript
Electron整合React使用搭建开发环境的步骤详解
2020/06/07 Javascript
python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
2014/08/22 Python
利用Python绘制数据的瀑布图的教程
2015/04/07 Python
详解python基础之while循环及if判断
2017/08/24 Python
Python删除Java源文件中全部注释的实现方法
2017/08/30 Python
Python 闭包的使用方法
2017/09/07 Python
Python创建普通菜单示例【基于win32ui模块】
2018/05/09 Python
利用pyinstaller将py文件打包为exe的方法
2018/05/14 Python
PYTHON发送邮件YAGMAIL的简单实现解析
2019/10/28 Python
Django CSRF认证的几种解决方案
2020/03/03 Python
CSS3属性background-size使用指南
2014/12/09 HTML / CSS
突破canvas语法限制 让他支持链式语法
2012/12/24 HTML / CSS
美国鲍勃商店:Bob’s Stores
2018/07/22 全球购物
比利时的在线灯具店:Lampen24.be
2019/07/01 全球购物
俄罗斯最大的灯具网站:Fandeco
2020/03/14 全球购物
外贸主管求职简历的自我评价
2013/10/23 职场文书
总经理助理的八要求
2013/11/12 职场文书
大门门卫岗位职责
2013/11/30 职场文书
公司聘任书模板
2014/03/29 职场文书
李开复演讲稿
2014/05/24 职场文书
《正面管教》读后有感:和善而坚定的旅程
2019/12/19 职场文书