python rsync服务器之间文件夹同步脚本
作者:life4711 时间:2023-02-23 05:05:56
本文实例为大家分享了python rsync服务器之间文件夹同步的具体代码,供大家参考,具体内容如下
About rsync
配置两台服务器之间3ssh-key后,可以实现自动化无需手动输入密码,脚本如下:
import argparse
import datetime
from functools import partial
import multiprocessing as mp
import os
import paramiko as pmk
import time
def check_ssh(host, user, port, passwd, dest_path):
ssh_client = pmk.SSHClient()
ssh_client.load_system_host_keys()
ssh_client.set_missing_host_key_policy(pmk.AutoAddPolicy())
try:
ssh_client.connect(host, username=user, port=port, timeout=10, password=passwd)
ssh_client.exec_command('mkdir ' + os.path.join(dest_path, 'data'))
except BaseException as e:
print 'failed to connect to host: %r: %r' % (host, e)
return False
else:
return True
def select_from_file(file_path):
file_list = []
if os.path.exists(file_path):
path_dir = os.listdir(file_path)
for all_dir in path_dir:
file_list.append(os.path.join('%s' % all_dir))
return file_list
def sync_file(file_name, remote_host, remote_user, remote_port, src_path, dest_path):
sync_cmd = "rsync -azrvhP --progress -e 'ssh -p " + str(remote_port) + "' --skip-compress=gz/zip/ " + \
file_name + " " + remote_user + "@" + remote_host + ":" + os.path.join(dest_path,'data')
print sync_cmd
os.chdir(src_path)
os.system(sync_cmd)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-w', '--workers', dest='workers', type=int, default=12)
parser.add_argument('-H', '--host', dest='host', type=str, default='192.168.254.156')
parser.add_argument('-u', '--user', dest='user', type=str, default='shubao')
parser.add_argument('-p', '--password', dest='password', type=str, default='123456')
parser.add_argument('-P', '--port', dest='port', type=int, default=22)
parser.add_argument('-r', '--remotepath', dest='remotepath', type=str, default='/home/shubao/')
parser.add_argument('-s', '--srcpath', dest='srcpath', type=str, default='/home/Jesse/data')
args = parser.parse_args()
if not check_ssh(args.host, args.user, args.port, args.password, args.remotepath):
print 'SSH connect faild!'
exit(-1)
pool = mp.Pool(processes=args.workers)
try:
while True:
print "New check start at %s..." % str(datetime.datetime.now())
file_list_ = select_from_file(args.srcpath)
print "File_list: "
print file_list_
p_work = partial(sync_file, remote_host=args.host, remote_user=args.user, remote_port=args.port,
src_path=args.srcpath, dest_path=args.remotepath)
pool.map(p_work, file_list_)
time.sleep(10)
finally:
pool.terminate()
pool.join()
来源:https://blog.csdn.net/lvshubao1314/article/details/70272996
标签:python,rsync,文件夹同步
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用Python设计一个经典小游戏
2023-07-11 02:57:09
![](https://img.aspxhome.com/file/2023/6/60496_0s.png)
asp三天学好ADO对象之第一天
2008-10-09 12:46:00
统计热门文章的算法
2008-03-16 15:40:00
python中openpyxl库用法详解
2023-07-15 09:53:45
![](https://img.aspxhome.com/file/2023/2/60722_0s.png)
Python实现根据日期获取当天凌晨时间戳的方法示例
2023-08-04 14:16:46
Python 中获取数组的子数组示例详解
2022-11-04 19:26:26
优雅管理Go Project生命周期
2023-06-16 01:10:42
![](https://img.aspxhome.com/file/2023/0/87630_0s.jpg)
DateDiff函数在Sql与Access中的区别
2009-06-04 18:02:00
Python pandas.DataFrame调整列顺序及修改index名的方法
2023-11-22 20:53:59
PHP笛卡尔积实现算法示例
2023-09-08 19:58:09
sql server数据库最大Id冲突问题解决方法之一
2012-01-05 19:28:42
Mysql数据库的安全性问题释疑
2009-02-26 16:20:00
XML卷之实战锦囊(1):动态排序
2008-09-05 17:20:00
仅允许指定的机器连接SQL Server服务器
2010-07-22 19:54:00
在js中的replace方法详解
2007-08-21 15:47:00
详解Laravel服务容器的优势
2023-10-31 03:36:04
类型转换的小乐趣
2010-11-30 21:37:00
SQL Server性能的改进得益于逻辑数据库设计
2009-10-23 13:55:00
Django中实现点击图片链接强制直接下载的方法
2023-08-03 13:29:19
python微信跳一跳系列之棋子定位颜色识别
2023-01-16 04:52:49
![](https://img.aspxhome.com/file/2023/4/66814_0s.png)