Posted in Python onAugust 29, 2019
本文实例为大家分享了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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。
python rsync服务器之间文件夹同步脚本
- Author -
life4711声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@