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 图片验证码代码
Dec 07 Python
python判断、获取一张图片主色调的2个实例
Apr 10 Python
Python中optparse模块使用浅析
Jan 01 Python
Python实现监控程序执行时间并将其写入日志的方法
Jun 30 Python
Django 使用logging打印日志的实例
Apr 28 Python
Python 旋转打印各种矩形的方法
Jul 09 Python
python中的TCP(传输控制协议)用法实例分析
Nov 15 Python
python:动态路由的Flask程序代码
Nov 22 Python
基于MSELoss()与CrossEntropyLoss()的区别详解
Jan 02 Python
Python关于反射的实例代码分享
Feb 20 Python
python 爬虫爬取京东ps4售卖情况
Dec 18 Python
Python使用Turtle模块绘制国旗的方法示例
Feb 28 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中的MVC模式运用技巧
2007/05/03 PHP
PHP 登录记住密码实现思路
2013/05/07 PHP
关于php支持分块与断点续传文件下载功能代码
2014/05/09 PHP
Laravel框架中自定义模板指令总结
2017/12/17 PHP
详解json在php中的应用
2018/09/30 PHP
Smarty缓存机制实例详解【三种缓存方式】
2019/07/20 PHP
PHP实现简单用户登录界面
2019/10/23 PHP
找到一点可怜的关于dojo资料,谢谢作者!
2006/12/06 Javascript
jquery.jstree 增加节点的双击事件代码
2010/07/27 Javascript
超炫的jquery仿flash导航栏特效
2014/11/11 Javascript
vsCode安装使用教程和插件安装方法
2020/08/24 Javascript
使用canvas实现一个vue弹幕组件功能
2018/11/30 Javascript
JavaScript学习笔记之DOM基础操作实例小结
2019/01/09 Javascript
弱类型语言javascript中 a,b 的运算实例小结
2019/08/07 Javascript
修改layui的后台模板的左侧导航栏可以伸缩的方法
2019/09/10 Javascript
详解Vscode中使用Eslint终极配置大全
2019/11/08 Javascript
[05:06]2017亚洲邀请赛DAC回顾片
2017/04/19 DOTA
Python实现从订阅源下载图片的方法
2015/03/11 Python
Python设计模式中单例模式的实现及在Tornado中的应用
2016/03/02 Python
python编程使用协程并发的优缺点
2018/09/20 Python
Python基于plotly模块实现的画图操作示例
2019/01/23 Python
Scrapy-Redis结合POST请求获取数据的方法示例
2019/05/07 Python
Centos7 下安装最新的python3.8
2019/10/28 Python
基于python实现学生信息管理系统
2019/11/22 Python
Tensorflow 卷积的梯度反向传播过程
2020/02/10 Python
Python sys模块常用方法解析
2020/02/20 Python
美国一家著名的儿童鞋制造商:Stride Rite
2017/01/02 全球购物
4s店总经理岗位职责
2013/12/31 职场文书
旷课检讨书大全
2014/01/21 职场文书
纠风工作实施方案
2014/03/15 职场文书
中央空调节能方案
2014/06/15 职场文书
社保转移委托书范本
2014/10/08 职场文书
简单聊一聊SQL注入及防止SQL注入
2022/03/23 MySQL
BCL经典机 SONY ICF-5900W电路分析
2022/04/24 无线电
CSS 左边固定宽右边自适应的6种方法
2022/05/15 HTML / CSS
腾讯云服务器部署前后分离项目之前端部署
2022/06/28 Servers