D84814037CE71170D9F4B465D815AC9A.jpg
windows环境下若有文件改动,则实时推送到rsync服务器上去,推送失败则发邮件警告,用python实现。
rsync-py

#-*- coding: utf-8 -*-
import subprocess
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
rsyncSrc='D:\opt'
rsyncDes='xxx@xx.xx.xx.xx::opt'
listen='inotifywait -mrq --format "%%e %%w\%%f" "D:\opt"'
popen=subprocess.Popen(listen,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)

def smail(mailfrom,receivers,subject,body,att):
    sender = 'devops@netinn.net'
    mailtotag = 'devops all'
    smtpserver = 'smtp.netinn.net'
    username = 'devops@netinn.net'
    password = 'Test123456'

    #创建一个带附件的实例
    message = MIMEMultipart()
    message['From'] = Header(mailfrom, 'utf-8')
    message['To'] =  Header(mailtotag, 'utf-8')
    message['Subject'] = Header(subject, 'utf-8')

    #邮件正文内容
    if isinstance(body,unicode):
        body = str(body)
    message.attach(MIMEText(body, 'plain', 'utf-8'))

    # 构造附件1,传送当前目录下的 test.txt 文件
    att1 = MIMEText(open(att, 'rb').read(), 'base64', 'utf-8')
    att1["Content-Type"] = 'application/octet-stream'
    # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
    att1["Content-Disposition"] = 'attachment; filename=log'
    message.attach(att1)

    att2 = MIMEText(open(att, 'rb').read(), 'base64', 'utf-8')
    att2["Content-Type"] = 'application/octet-stream'
    att2["Content-Disposition"] = 'attachment; filename=att'
    #message.attach(att2)

    message["Accept-Language"] = "zh-CN"
    message["Accept-Charset"] = "ISO-8859-1,utf-8"

    try:
        smtpObj = smtplib.SMTP(smtpserver)
        smtpObj.login(username, password)
        smtpObj.sendmail(sender, receivers, message.as_string())
        print "mail send"
    except smtplib.SMTPException:
        print "Error: send failed"

mailfrom='cop@quyiyuan.com'
receivers = ['xx@netinn.net','xx@netinn.net','xx@qq.com','xx@hotmail.com','xx@xx.com','xx@xx.com']  # 接收邮件

while True:
    line=popen.stdout.readline().strip()
    #print line
    lineArr=line.split(' ')
    oper=lineArr[0]
    file=lineArr[1]
    touched=False
    #print file
    if file.index(rsyncSrc)==0:
        if (oper=='MOVED_TO') or (oper=='CREATE'):
            _cureent_file=file.replace(rsyncSrc,'/cygdrive/d/opt')
            cureent_file=_cureent_file.replace('\\','/')
            cmd='set CYGWIN=nodosfilewarning && cd /d '+rsyncSrc+' && '+'start /b rsync -avz -R -d --port=873 --delete --progress '+cureent_file+' '+rsyncDes+' <"C:\Program Files (x86)\ICW\etc\pass.txt" 2>D:\logs\error.log'
            touched=True
    if touched:
        #print cmd
        rsyncAction=os.popen(cmd)
        rsyncStat=rsyncAction.read()
        if "speedup is" in rsyncStat:
            print file+' rsynced!'
            print rsyncStat
        else:
            print file+' rsync failed!'
            att='D:\\logs\\error.log' 
            subject ='SyncError:'+file
            body=rsyncStat
            smail(mailfrom,receivers,subject,body,att)
        #rsyncAction=subprocess.check_output(cmd1)
        #rsyncAction=subprocess.check_output(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True)

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code