Python实现定期检查源目录与备份目录的差异并进行备份功能示例


Posted in Python onFebruary 27, 2019

本文实例讲述了Python实现定期检查源目录与备份目录的差异并进行备份功能。分享给大家供大家参考,具体如下:

在项目中,经常要更新文件,在更新之前首先要备份源文件,所以就用到了这个脚本(来自于Python自动化运维这本书),总共有以下几个步骤:

1. 获取要进行比较的两个目录,进行差异比较,把源目录特有的文件或目录、以及和备份目录不同的文件或目录保存到列表中,并且判断目录下面是否还有目录,递归进行保存这些差异文件。
2. 将差异文件列表中文件或目录的路径换成对应的备份路径,进行判断,如果备份路径不存在,就创建目录。
3. 继续对比源目录和新创建的备份目录中的差异文件,把源路径换成备份目录的路径。
4. 然后遍历复制源目录文件到备份目录。

以下是具体的实现代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys
import filecmp
import re
import shutil
holderlist = []
##对应第一个步骤
def compare_me(dir1, dir2):
  dircomp = filecmp.dircmp(dir1, dir2)
  only_in_one = dircomp.left_only
  diff_in_one = dircomp.diff_files
  dirpath = os.path.abspath(dir1)
  [ holderlist.append(os.path.abspath(os.path.join(dir1, x))) for x in only_in_one ]
  [ holderlist.append(os.path.abspath(os.path.join(dir1, x))) for x in diff_in_one ]
  if len(dircomp.common_dirs) > 0:
    for item in dircomp.common_dirs:
      compare_me(os.path.abspath(os.path.join(dir1, item)), os.path.abspath(os.path.join(dir2, item)))
  return holderlist
##对应第二个步骤
def main():
  if len(sys.argv) > 2:
    dir1 = sys.argv[1]
    dir2 = sys.argv[2]
  else:
    print "Usage: ", sys.argv[0], "datadir backupdir"
    sys.exit()
  source_files = compare_me(dir1, dir2)
  dir1 = os.path.abspath(dir1)
  if not dir2.endswith('/'):
    dir2 = dir2 + '/'
  dir2 = os.path.abspath(dir2)
  destination_files = []
  createdir_bool = False
  for item in source_files:
    destination_dir = re.sub(dir1, dir2, item)
    destination_files.append(destination_dir)
    if os.path.isdir(item):
      if not os.path.exists(destination_dir):
        os.makedirs(destination_dir)
        createdir_bool = True
   ##对应第三个步骤
  if createdir_bool:
    destination_files = []
    source_files = []
    source_files = compare_me(dir1, dir2)
    for item in source_files:
      destination_dir = re.sub(dir1, dir2, item)
      destination_files.append(destination_dir)
  ##对应第四个步骤
  print "update item: "
  print source_files
  copy_pair = zip(source_files, destination_files)
  print "copy_pair is %s" % copy_pair
  for item in copy_pair:
    print "item is %s, %s" % (item[0], item[1])
    if os.path.isfile(item[0]):
      shutil.copyfile(item[0], item[1])
if __name__ == '__main__':
  main()

最后根据需要,可以设定一个定时检查,进行自动同步源目录和备份目录,让其保持一致性。

Python 相关文章推荐
Python中的生成器和yield详细介绍
Jan 09 Python
利用python写个下载teahour音频的小脚本
May 08 Python
一个Python最简单的接口自动化框架
Jan 02 Python
Python解决八皇后问题示例
Apr 22 Python
Django中使用Celery的教程详解
Aug 24 Python
更改Python的pip install 默认安装依赖路径方法详解
Oct 27 Python
Pycharm保存不能自动同步到远程服务器的解决方法
Jun 27 Python
python3 enum模块的应用实例详解
Aug 12 Python
为什么是 Python -m
Jun 19 Python
快速解释如何使用pandas的inplace参数的使用
Jul 23 Python
PyQt5的相对布局管理的实现
Aug 07 Python
Python基础学习之奇异的GUI对话框
May 27 Python
详解Django-restframework 之频率源码分析
Feb 27 #Python
Python的UTC时间转换讲解
Feb 26 #Python
Python逐行读取文件中内容的简单方法
Feb 26 #Python
Python计算时间间隔(精确到微妙)的代码实例
Feb 26 #Python
python3编写ThinkPHP命令执行Getshell的方法
Feb 26 #Python
初探利用Python进行图文识别(OCR)
Feb 26 #Python
Python编写合并字典并实现敏感目录的小脚本
Feb 26 #Python
You might like
利用static实现表格的颜色隔行显示
2006/10/09 PHP
php数组函数序列之array_push() 数组尾部添加一个或多个元素(入栈),返回新长度。
2011/11/07 PHP
PHP Global变量定义当前页面的全局变量实现探讨
2013/06/05 PHP
php函数重载的替代方法--伪重载详解
2015/05/08 PHP
PHP使用SWOOLE扩展实现定时同步 MySQL 数据
2017/04/09 PHP
TNC vs BOOM BO3 第二场2.13
2021/03/10 DOTA
更换select下拉菜单背景样式的实现代码
2011/12/20 Javascript
ExtJs默认的字体大小改变的几种方法(自己整理)
2013/04/18 Javascript
js操纵dom生成下拉列表框的方法
2014/02/24 Javascript
jquery的trigger和triggerHandler的区别示例介绍
2014/04/20 Javascript
Egret引擎开发指南之视觉编程
2014/09/03 Javascript
jQuery中bind(),live(),delegate(),on()绑定事件方法实例详解
2016/01/19 Javascript
JavaScript lodash常见用法系列小结
2016/08/24 Javascript
PhotoSwipe异步动态加载图片方法
2016/08/25 Javascript
使用JQuery选择HTML遍历函数的方法
2016/09/17 Javascript
Canvas 制作动态进度加载水球详解及实例代码
2016/12/09 Javascript
vue实现列表的添加点击
2016/12/29 Javascript
深入对Vue.js $watch方法的理解
2017/03/20 Javascript
vue中mint-ui环境搭建详细介绍
2017/04/06 Javascript
详解VUE自定义组件中用.sync修饰符与v-model的区别
2018/06/26 Javascript
Vue源码解读之Component组件注册的实现
2018/08/24 Javascript
webpack dll打包重复问题优化的解决
2018/10/10 Javascript
Vue组件间数据传递的方式(3种)
2020/07/13 Javascript
python list使用示例 list中找连续的数字
2014/01/27 Python
Python实现监控程序执行时间并将其写入日志的方法
2015/06/30 Python
Tornado协程在python2.7如何返回值(实现方法)
2017/06/22 Python
Python实现excel转sqlite的方法
2017/07/17 Python
python多线程之事件Event的使用详解
2018/04/27 Python
python re库的正则表达式入门学习教程
2019/03/08 Python
pycharm创建一个python包方法图解
2019/04/10 Python
Python利用FFT进行简单滤波的实现
2020/02/26 Python
python实现QQ邮箱发送邮件
2020/03/06 Python
团结演讲稿范文
2014/05/23 职场文书
毕业证委托书范文
2014/09/26 职场文书
精神文明建设汇报材料
2014/12/24 职场文书
导游词之西安骊山
2019/12/20 职场文书