场景:服务器自动备份数据库文件,每两小时生成一个新备份文件,通过云备份客户端自动上传,需要每天检查是否备份成功。

实现:本脚本实现检查文件是否备份成功,进程是否正常运行,并且发送相关邮件提醒。

#! /usr/bin/env python

import os
import time
import smtplib from email.mime.text import MIMEText
from email.header import Header from configparser import ConfigParser def 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,receiver,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 = '*****'
#receiver = ['****' , '****']
#pwd = '****' 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,sender,pwd,receiver.split(','),msg.as_string()) if __name__ == '__main__':
main()

  

ini配置文件内容

[os]
path=D:\test [email]
server=smtp.office365.com
sender=xxxx@outlook.com
pwd=xxxxx
receiver=xx@outlook.com,xxxxx@gmail.com

  

最新文章

  1. 无法打开键: UNKNOWN\Components\BE1FB738077DBE490AF18C3B9B1A1EE8\E5F5286B58B542741A00A0A9AA420B27
  2. Asp.Net调试方法备忘
  3. 大熊君大话NodeJS之------Http模块
  4. (1)Underscore.js入门
  5. 【MVC】 基础
  6. 【Android】如何实现ButterKnife
  7. Outlook Web App简介
  8. poj 1050 To the Max(最大子矩阵之和,基础DP题)
  9. 异步调用webservice
  10. Linux下MySQL5.6的修改字符集编码为UTF8
  11. Trident内核中取验证码图片的几种方法
  12. iOS 多线程 之 GCD(大中枢派发)(一)
  13. Spring Cloud Stream同一通道根据消息内容分发不同的消费逻辑
  14. JFreeChart入门
  15. 怎么把excel表格内的数据导入数据库?
  16. Strategic Game HDU - 1054(最小顶点覆盖)
  17. 命令:man
  18. delphi project of object
  19. Redis Commands(1)
  20. Hololens 开发环境配置(转)

热门文章

  1. CentOS 7.6部署Vue + SrpingBoot + MySQL单体项目
  2. OLAP引擎:基于Presto组件进行跨数据源分析
  3. C# Linq 延迟查询的执行
  4. 前端进阶(1)Web前端性能优化
  5. 面试有关TCP常问的几个问题
  6. DPAPI机制学习
  7. VUE+Element 前端应用开发框架功能介绍
  8. Linux 究级基础入门命令整理
  9. vuex 引用方法
  10. SpringCloud之Ribbon负载均衡策略