
场景:服务器自动备份数据库文件,每两小时生成一个新备份文件,通过云备份客户端自动上传,需要每天检查是否备份成功。
实现:本脚本实现检查文件是否备份成功,进程是否正常运行,并且发送相关邮件提醒。
#! /usr/bin/env pythonimport osimport timeimport smtplibfrom email.mime.text import MIMETextfrom email.header import headerfrom configparser import ConfigParserdef SendMail(server,sender,pwd,receiver,msg): ‘‘‘ Conncet to Office365 mail server and sent emails ‘‘‘ email = smtplib.SMTP(server,587) email.starttls() email.ehlo(server) email.login(sender,pwd) email.sendmail(sender,msg) email.quit() def GetNewfiles(path,num): ‘‘‘ Get file Lists and return the last num created files ‘‘‘ Lists = os.Listdir(path) Lists.sort(key=lambda fn:os.path.getctime(path+‘\‘+fn)) return Lists[-num : ] def CheckProcess(name): ‘‘‘ Check if the process exits and return result. [‘\n‘,‘Image name PID Session name Session# Mem Usage\n‘,‘========================= ======== ================ =========== ============\n‘,‘DropBox.exe 20484 Console 1 71,652 K\n‘,‘DropBox.exe 23232 Console 1 2,456 K\n‘,‘DropBox.exe 61120 Console 1 2,168 K\n‘] ‘‘‘ proc = [] p = os.popen(‘taskList /FI "IMAGEname eq %s"‘ % name) for x in p: proc.append(x) p.close() return proc def MailContent(path,num): ‘‘‘ make the mail contents ‘‘‘ content = [] dropBox = CheckProcess(‘dropBox.exe‘) carboniteservice = CheckProcess(‘carboniteservice.exe‘) #IF process doesn‘t run if len(dropBox) < 2 or len(carboniteservice) < 2 : content.append("DropBox or CarBonite doesn‘t run") s = ‘\n\t‘.join(dropBox) + ‘\n\n‘ + ‘\n\t‘.join(carboniteservice) content.append("Process Check Result:\n\t" + s) return content #Check if the backup files are correct. files = GetNewfiles(path,num) file_ctime = os.path.getctime(path + ‘\‘ + files[0]) Now = time.time() - 86400 if file_ctime > Now : content.append("DB Backup Successfull") body = "\nThe Backup files are:\n\t" + ‘\n\t‘.join(files) content.append(body) return content else : content.append("DB Backup Failed") body = "\nThe last backup sucessfull file is " + files[-1] content.append(body) return content def main(): #server = ‘smtp.office365.com‘ #sender = ‘[email protected]‘ #receiver = [‘[email protected]‘,‘[email protected]‘] #pwd = ‘Netbrain12‘ config = ConfigParser() config.read_file(open(‘config.ini‘)) path = config.get(‘os‘,‘path‘) receiver = config.get(‘email‘,‘receiver‘) server = config.get(‘email‘,‘server‘) sender = config.get(‘email‘,‘sender‘) pwd = config.get(‘email‘,‘pwd‘) content = MailContent(path,12) #content = MailContent("D:\test",6) mail_content = content[1] msg = MIMEText(mail_content,"plain","utf-8") msg["Subject"] = header(content[0],"utf-8") msg["From"] = sender msg["To"] = header(receiver) SendMail(server,receiver.split(‘,‘),msg.as_string())if __name__ == ‘__main__‘: main()
ini配置文件内容
[os]path=D:\test[email]server=smtp.office365.com[email protected]pwd=xxxxx[email protected],[email protected]总结
以上是内存溢出为你收集整理的Python检查 文件备份是否正常 云备份进程是否正常运行全部内容,希望文章能够帮你解决Python检查 文件备份是否正常 云备份进程是否正常运行所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)