rsync多线程备份脚本 --pytho
闲话少说,脚本奉上,希望对大家有帮助。
#!/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/src/2 /tmp/linshi',
'test2':'/usr/bin/rsync -tzrp --stats /usr/local/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()
相关文章