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 相关文章推荐
python2.7删除文件夹和删除文件代码实例
Dec 18 Python
python基于itchat实现微信群消息同步机器人
Feb 27 Python
Python简单实现自动删除目录下空文件夹的方法
Aug 29 Python
Python subprocess库的使用详解
Oct 26 Python
Mac下Anaconda的安装和使用教程
Nov 29 Python
Django框架视图介绍与使用详解
Jul 18 Python
FFrpc python客户端lib使用解析
Aug 24 Python
详解Python图像处理库Pillow常用使用方法
Sep 02 Python
基于python实现获取网页图片过程解析
May 11 Python
详解python算法常用技巧与内置库
Oct 17 Python
详解使用scrapy进行模拟登陆三种方式
Feb 21 Python
python和anaconda的区别
May 06 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
PHP中for循环语句的几种变型
2007/03/16 PHP
php中inlcude()性能对比详解
2012/09/16 PHP
PHP 正则判断中文UTF-8或GBK的思路及具体实现
2013/11/26 PHP
PHP函数import_request_variables()用法分析
2016/04/02 PHP
PHP微信刮刮卡 附微信接口
2016/07/22 PHP
PHP+Ajax异步带进度条上传文件实例
2016/11/01 PHP
PHP中Session ID的实现原理实例分析
2019/08/17 PHP
Javascript客户端将指定区域导出到Word、Excel的代码
2008/10/22 Javascript
JavaScript移除数组内重复元素的方法
2015/03/18 Javascript
jquery中的工具使用方法$.isFunction, $.isArray(), $.isWindow()
2015/08/09 Javascript
javascript禁止超链接跳转的方法
2016/02/02 Javascript
js类式继承与原型式继承详解
2016/04/07 Javascript
浅析jQuery事件之on()方法绑定多个选择器,多个事件
2016/04/27 Javascript
jquery实现垂直和水平菜单导航栏
2020/08/27 Javascript
JS匿名函数实例分析
2016/11/26 Javascript
简单谈谈JS中的正则表达式
2017/09/11 Javascript
vue路由中前进后退的一些事儿
2019/05/18 Javascript
js中的this的指向问题详解
2019/08/29 Javascript
python 动态获取当前运行的类名和函数名的方法
2014/04/15 Python
python入门基础之用户输入与模块初认识
2016/11/14 Python
python函数的5种参数详解
2017/02/24 Python
matplotlib.pyplot画图 图片的二进制流的获取方法
2018/05/24 Python
python使用pipeline批量读写redis的方法
2019/02/18 Python
Python 使用 attrs 和 cattrs 实现面向对象编程的实践
2019/06/12 Python
在PYQT5中QscrollArea(滚动条)的使用方法
2019/06/14 Python
浅析Python 简单工厂模式和工厂方法模式的优缺点
2020/07/13 Python
HTML5 新旧语法标记对我们有什么好处
2012/12/13 HTML / CSS
Hawes & Curtis官网:英国经典品牌
2019/07/27 全球购物
人事主管岗位职责
2014/01/30 职场文书
文秘自荐信
2014/06/28 职场文书
公司活动总结范文
2014/07/01 职场文书
初中学习计划书范文
2014/09/15 职场文书
县政府领导班子“四风”方面突出问题整改措施
2014/09/23 职场文书
工伤死亡理赔协议书
2014/10/20 职场文书
背起爸爸上学观后感
2015/06/08 职场文书
html用代码制作虚线框怎么做? dw制作虚线圆圈的技巧
2022/12/24 HTML / CSS