python rsync服务器之间文件夹同步脚本
程序员文章站
2023-01-04 08:09:56
本文实例为大家分享了python rsync服务器之间文件夹同步的具体代码,供大家参考,具体内容如下
about rsync
配置两台服务器之间3ssh-k...
本文实例为大家分享了python 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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Windows环境下安装PHP Pear的方法图文教程
下一篇: js的新生代垃圾回收知识点总结