Msl23
世上最美的,莫过于从泪水中挣脱出来的那个微笑。

Linux运维自动化脚本之rsync

起因

  因为要做同步日志的脚本,考虑到机器比较多,后面也要扩展。

  最近在学Python(写的比较菜)。

思路

  组成:配置文件 + 执行脚本

    脚本执行后读取配置文件的各个字段内容,并且赋值给对应的变量,在执行命令。

配置文件格式:

  密码文件路径;用户名;IP;模块名;目标位置路径

 

Code:

#!/bin/env python

import time,os,datetime with open(
'config/auto_rsync.conf','r',encoding='utf-8') as f: file_info = f.readlines() count = 0 count1 = 1 for file_use in file_info: file_use.strip(" ") if file_use[0] == "#" or file_use[0] == "\n": continue else: file_line = file_use.split(";") count+=1 while count1 <= count: PASS,USER,IP,MOD_NAME,DDIR = file_line[0],file_line[1],file_line[2],file_line[3],file_line[4] #print (PASS,USER,IP,MOD_NAME,DDIR) nowTime = datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S') os.system("/usr/bin/nohup /usr/bin/rsync -avzP --password-file=%s %s@%s::%s %s >> /var/log/rsync_%s.log" % (PASS,USER,IP,MOD_NAME,DDIR,nowTime)) count1+=1 time.sleep(1)

 

posted on 2018-07-29 18:03  Msl23  阅读(276)  评论(1编辑  收藏  举报