python rsync服务器之间文件夹同步脚本

时间:2021-05-22

本文实例为大家分享了python rsync服务器之间文件夹同步的具体代码,供大家参考,具体内容如下

About rsync

配置两台服务器之间3ssh-key后,可以实现自动化无需手动输入密码,脚本如下:

import argparseimport datetimefrom functools import partialimport multiprocessing as mpimport osimport paramiko as pmkimport 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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章