Python检查 云备份进程是否正常运行代码实例


Posted in Python onAugust 22, 2019

这篇文章主要介绍了Python检查 云备份进程是否正常运行代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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

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

#! /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 = 'online@netbraintech.com'
  #receiver = ['gavin.yuan@netbraintech.com' , 'feng.liu@netbraintech.com']
  #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,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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python SQLite3数据库日期与时间常见函数用法分析
Aug 14 Python
Python3 XML 获取雅虎天气的实现方法
Feb 01 Python
Python中函数参数调用方式分析
Aug 09 Python
Python 监测文件是否更新的方法
Jun 10 Python
解决Python安装时报缺少DLL问题【两种解决方法】
Jul 15 Python
python中的反斜杠问题深入讲解
Aug 12 Python
基于 Django 的手机管理系统实现过程详解
Aug 16 Python
Python拆分大型CSV文件代码实例
Oct 07 Python
基于Tensorflow读取MNIST数据集时网络超时的解决方式
Jun 22 Python
Django中如何用xlwt生成表格的方法步骤
Jan 31 Python
Python使用pyenv实现多环境管理
Feb 05 Python
DjangoRestFramework 使用 simpleJWT 登陆认证完整记录
Jun 22 Python
浅谈Python 递归算法指归
Aug 22 #Python
python求加权平均值的实例(附纯python写法)
Aug 22 #Python
python求平均数、方差、中位数的例子
Aug 22 #Python
python2和python3实现在图片上加汉字的方法
Aug 22 #Python
Python使用微信itchat接口实现查看自己微信的信息功能详解
Aug 22 #Python
简单了解python 生成器 列表推导式 生成器表达式
Aug 22 #Python
Python实现的微信红包提醒功能示例
Aug 22 #Python
You might like
DOTA2 6.87版本后新眼位详解攻略
2020/04/20 DOTA
destoon整合UCenter图文教程
2014/06/21 PHP
PHP+ajax实现获取新闻数据简单示例
2018/05/08 PHP
关于JQuery($.load)事件的用法和分析
2013/04/09 Javascript
JS图片切换的具体方法(带缩略图版)
2013/11/12 Javascript
深入理解javascript中return的作用
2013/12/30 Javascript
JavaScript网页定位详解
2014/01/13 Javascript
jquery下div 的resize事件示例代码
2014/03/09 Javascript
jQuery 如何先创建、再修改、后添加DOM元素
2014/05/20 Javascript
javascript中声明函数的方法及调用函数的返回值
2014/07/22 Javascript
javascript转换日期字符串为Date日期对象的方法
2015/02/13 Javascript
jQuery siblings()用法实例详解
2016/04/26 Javascript
JS使用正则表达式过滤多个词语并替换为相同长度星号的方法
2016/08/03 Javascript
Openlayers+EasyUI Tree动态实现图层控制
2020/09/28 Javascript
jquery插件实现轮播图效果
2020/10/19 jQuery
[52:02]完美世界DOTA2联赛PWL S2 FTD.C vs SZ 第一场 11.27
2020/11/30 DOTA
用Python进行行为驱动开发的入门教程
2015/04/23 Python
python中利用xml.dom模块解析xml的方法教程
2017/05/24 Python
Python实现按学生年龄排序的实际问题详解
2017/08/29 Python
Python 实现删除某路径下文件及文件夹的实例讲解
2018/04/24 Python
python3人脸识别的两种方法
2019/04/25 Python
python实现图片中文字分割效果
2019/07/22 Python
django 基于中间件实现限制ip频繁访问过程详解
2019/07/30 Python
Python实现RGB与HSI颜色空间的互换方式
2019/11/27 Python
python rsa-oaep加密的示例代码
2020/09/23 Python
python二维图制作的实例代码
2020/12/03 Python
HTML5里的placeholder属性使用实例和美化显示效果的方法
2014/04/23 HTML / CSS
丝芙兰法国官网:SEPHORA法国
2016/09/01 全球购物
Sandro Paris美国官网:典雅别致的法国时尚服饰品牌
2017/12/26 全球购物
写clone()方法时,通常都有一行代码,是什么?
2012/10/31 面试题
工程造价专业大学生自荐信
2013/10/01 职场文书
企业领导对照检查材料
2014/08/20 职场文书
授权委托书公证
2014/09/14 职场文书
部门经理迟到检讨书
2015/02/16 职场文书
Go使用协程交替打印字符
2021/04/29 Golang
Python中的嵌套循环详情
2022/03/23 Python