rsync多线程备份脚本 --pytho
短信预约 -IT技能 免费直播动态提醒
闲话少说,脚本奉上,希望对大家有帮助。
#!/bin/env python
#coding=utf8
import os,sys,time,re,threading
now=time.strftime('%Y%m%d')
log_02='/tmp/tongbu_stats_%s.txt' % (now)
# rsync -t保持稳健时间细细
# -z对备份的文件传输中压缩处理
# -r对子目录进行递归处理
# -p保持file权限
# -l保持软连接
dict_cmd={
'test1':'/usr/bin/rsync -tzrlp --stats /usr/local/class="lazy" data-src/2 /tmp/linshi',
'test2':'/usr/bin/rsync -tzrp --stats /usr/local/class="lazy" data-src/openfire /tmp/linshi0304'
}
def write_title(log_stat):
file_log=open(log_stat,'a')
file_log.write('strt_time,end_time,module,Number of files,Number of files transferred,Total file size(byte),Total transferred file size(byte)\n')
file_log.close()
def write_notes(cmd,log_stat,nam):
fle=open(log_stat,'a')
try:
s_time=time.strftime('%H:%M:%S')
cmd_put=os.popen(cmd)
list_tmp=[]
re_p=re.compile('(Number of files:|Number of files transferred:|Total file size:|Total transferred file size:) (\d+)?')
for each_line1 in cmd_put:
for each_line2 in re_p.finditer(each_line1):
if each_line2 is not None:
list_tmp.append(each_line2.group(2))
else:
pass
e_time=time.strftime('%H:%M:%S')
cmd_stat='%s,%s,%s,%s,%s,%s,%s,%s' % (s_time,e_time,nam,list_tmp[0],list_tmp[1],list_tmp[2],list_tmp[3],os.linesep)
cmd_put.close()
fle.write(cmd_stat)
except:
cmd_stat='%s,%s,%s,%s,%s,%s,%s,%s' % (s_time,e_time,nam,'error','error','error','error',os.linesep)
fle.write(cmd_stat)
fle.close()
if __name__=='__main__':
write_title(log_02)
threads={}
for i in dict_cmd:
t=threading.Thread(target=write_notes,args=(dict_cmd[i],log_02,i))
print 'exec',i
threads[i]=t
for i in dict_cmd:
threads[i].start()
time.sleep(10)
print 'exec',threads[i]
for i in dict_cmd:
threads[i].join()
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341