Python 3 自动化运维实战小项目之校验源和备份目录之间的差异
2023-09-14 09:16:00 时间
项目目标:
由于我们无法确认备份目录和源目录文件是否保存一致,包括源目录中的新文件、目录等 有无成功同步,所以写个程序来对比下,没有成功就补上去。
#!/usr/bin/python3
#
import os
import sys
import filecmp
import re
import shutil
holderlist=[]
def compareme(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: #递归子目录
compareme(os.path.abspath(os.path.join(dir1,item)), os.path.abspath(os.path.join(dir2,item)))
return holderlist
def checkargv():
if len(sys.argv) < 2: #要求输入源目录与备份目录
print ("Usage: ", sys.argv[0], "datadir backupdir")
sys.exit()
else:
dir1 = sys.argv[1]
dir2 = sys.argv[2]
source_files = compareme(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 #再次调用compareme函数标记
if createdir_bool: #重新调用compareme函数,重新遍历新创建目录的内容
destination_files = []
source_files = []
source_files = compareme(dir1,dir2) #调用compareme函数
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) #讲源目录与备份目录文件清单拆分成元组
for item in copy_pair:
if os.path.isfile(item[0]): #判断是否为文件,是则进行复制操作
shutil.copyfile(item[0], item[1])
if __name__ == '__main__':
checkargv()
相关文章
- 【Python成长之路】python 基础篇 -- 装饰器【华为云分享】
- Python MySQLdb模块连接操作mysql数据库实例_python
- Python操作Mysql实例代码教程在线版(查询手册)_python
- 理解Python的双下划线命名(转)
- python用reduce和map把字符串转为数字的方法
- [Python] Use Python Classes
- Python 爬取指定ID的所有博客备份到本地硬盘上
- Python Task对象详解
- Python编程语言学习:python语言中快速查询python自带模块&函数的用法及其属性方法、如何查询某个函数&关键词的用法、输出一个类或者实例化对象的所有属性和方法名之详细攻略
- Python:利用python代码编程实现将视频的avi格式转换为MP4格式
- Python语言学习:利用python语言实现调用内部命令(python调用Shell脚本)—命令提示符cmd的几种方法
- Python:python语言中与时间有关的库函数简介、安装、使用方法(获取当前时间/计算程序块前后运行时间/模型训练时间或耗费时间)之详细攻略
- Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Entry/Entry的Command)
- Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(计算器简单功能)
- 已解决Visit python.org to download an earlier version of Python.
- 《看漫画学Python》1、2版分享,python最佳入门教程,中学生用业余时间都能学会,北大教授看完都这样定义它
- 【python代码】:能在手机上敲 Python 代码几款App
- 〖Python WEB 自动化测试实战篇③〗- python-selenium环境配置搭建
- 〖Python 数据库开发实战 - Python与MySQL交互篇⑩〗- 创建新闻管理系统的具体python文件
- python 将一个JSON 字典转换为一个Python 对象
- Python Neural Networks for Beginners 学习笔记 on Youtube
- Python 基础数据结构
- Python 博客园快速备份脚本
- Python可视化数据分析01、python环境搭建
- python中的异常处理(try,except,else, finally)
- (非线性规划Python)计及动态约束及节能减排环保要求的经济调度
- python零基础学编程:Python是什么?为什么Python这么火,学Python能干什么?
- Python基础之条件判断