《python运维自动化》-备份目录

本文介绍了一个使用Python编写的简单目录比较与同步脚本。该脚本能够对比两个目录的内容差异,并将源目录中独有的文件及不同内容的文件复制到目标目录中。此外,它还支持递归比较子目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#! /usr/bin/env python

import os, 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.adspath(os.path.join(dir1, item)), \
            os.path.adspath(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()
    print "source:" + dir1+",dest:"+dir2
    source_files=compareme(dir1, dir2)
    print "source_files:"+str(source_files)
    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)
        #print "dest:"+str(destination_files)
        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=compareme(dir1, dir2)
        for item in source_files:
            destination_dir=re.sub(dir1, dir2, item)
            destination_files.append(destination_dir)

    print "udpate item:"    
    print source_files
    copy_pair=zip(source_files, destination_files)
    print "source dir:"+str(source_files)+",dest dir:"+str(destination_files)
    for item in copy_pair:
        if os.path.isfile(item[0]):
            shutil.copyfile(item[0], item[1])

if __name__ == '__main__':
    main()

[root@localhost test]# ls
cmp.py dir1 dir2
[root@localhost test]# ls dir1 dir2
dir1:
1 test01 test02 test.log

dir2:
1 test02 test.log
[root@localhost test]# cat dir1/test.log dir2/test.log
test a bc
TEST a bc
[root@localhost test]# python cmp.py /root/test/dir1 root/test/dir2
source:/root/test/dir1,dest:/root/test/dir2
source_files:[‘/root/test/dir1/test01’, ‘/root/test/dir1/test.log’]
udpate item:
[‘/root/test/dir1/test01’, ‘/root/test/dir1/test.log’]
source dir:[‘/root/test/dir1/test01’, ‘/root/test/dir1/test.log’],dest dir:[‘/root/test/dir2/test01’, ‘/root/test/dir2/test.log’]
[root@localhost test]#
[root@localhost test]# ls dir1 dir2
dir1:
1 test01 test02 test.log

dir2:
1 test01 test02 test.log
[root@localhost test]# cat dir1/test.log dir2/test.log
test a bc
test a bc
[root@localhost test]#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值