python rsync服务器之间文件夹同步脚本
本文实例为大家分享了pythonrsync服务器之间文件夹同步的具体代码,供大家参考,具体内容如下
About rsync
配置两台服务器之间3ssh-key后,可以实现自动化无需手动输入密码,脚本如下:
importargparse
importdatetime
fromfunctoolsimportpartial
importmultiprocessingasmp
importos
importparamikoaspmk
importtime
defcheck_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'))
exceptBaseExceptionase:
print'failedtoconnecttohost:%r:%r'%(host,e)
returnFalse
else:
returnTrue
defselect_from_file(file_path):
file_list=[]
ifos.path.exists(file_path):
path_dir=os.listdir(file_path)
forall_dirinpath_dir:
file_list.append(os.path.join('%s'%all_dir))
returnfile_list
defsync_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')
printsync_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()
ifnotcheck_ssh(args.host,args.user,args.port,args.password,args.remotepath):
print'SSHconnectfaild!'
exit(-1)
pool=mp.Pool(processes=args.workers)
try:
whileTrue:
print"Newcheckstartat%s..."%str(datetime.datetime.now())
file_list_=select_from_file(args.srcpath)
print"File_list:"
printfile_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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。