Python检查 云备份进程是否正常运行代码实例
作者:星拂晓空 发布时间:2023-07-08 23:59:05
标签:python,检查,云备份,进程,正常,运行
这篇文章主要介绍了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
来源:https://www.cnblogs.com/xingfuxiaokong/p/11393387.html


猜你喜欢
- 本文实例讲述了GO语言常用的文件读取方式。分享给大家供大家参考。具体分析如下:Golang 的文件读取方法很多,刚上手时不知道怎么选择,所以
- 在不使用matlab的情况下,可以选择用python来实现自动控制理论有关系统打时域分析和频率域分析等,安装的package是python-
- transforms.CenterCrop(size)将给定的PIL.Image进行中心切割,得到给定的size,size可以是tuple,
- parseFloat()方法的定义和用法:parseFloat()方法可以解析一个字符串,并返回一个浮点数。注:如果字符串中的第一个字符不能
- 本文实例讲述了JS生成一维码(条形码)功能的方法。分享给大家供大家参考,具体如下:1、js代码:(function() { if (!exp
- 这样处理的弊端是:如果数据量大,子分类很多,达到4级以上,这方法处理极端占用数据库连接池 对性能影响很大。 如果用SQL下面的CTE递归处理
- 假设我们需要一个函数什么事都不干,只是抛出异常(在某些系统中有些handler就是干这事的),我们可以很直观的写出下面的代码:def fun
- 最近有个功能需要java与python之间的数据交互,java需要把参数传给python,然后python计算的结果返回给java.于是就写
- 初学 Python 的开发者经常会发现很多 Python 函数中用到了 yield 关键字,然而,带有 yield 的函数执行流程却和普通函
- 看代码吧~import torchimport numpy as npfrom torchvision.transforms import
- 本文实例为大家分享了Thinkphp微信公众号支付接口,供大家参考,具体内容如下第一步 先把文件夹的那两个图片 配置成一样的路径
- 桑基图桑基图(Sankey diagram),即桑基能量分流图,也叫桑基能量平衡图。它是一种特定类型的流程图,图中延伸的分支的宽度对应数据流
- 前期准备及前情回顾#对于一维向量用np.arange生成以元组形式输出从0开始的数组([0, 1, 2, 3, 4, 5, 6, 7, 8,
- 1、PHP中的抽象类PHP 5 支持抽象类和抽象方法。定义为抽象的类不能被实例化。任何一个类,如果它里面至少有一个方法是被声明为抽象的,那么
- 如何在服务器端调用winzip命令行对上传的多个文件打包压缩?要解决这个问题,首先要了解一下Windows Scripting Host,简
- 本文实例讲述了GO语言实现简单TCP服务的方法。分享给大家供大家参考。具体实现方法如下:package mainimport ("
- 本实验中分别针对空库、脱机、联机三种方式,配置一主两从的mysql标准异步复制。只做整服务器级别的复制,不考虑对个别库表或使用过滤复制的情况
- argparse库是python下的一个命令行参数管理库,支持int、str、float、bool、数组等5种基本数据类型。在解析命令行参数
- 一、安装pyinstallerPyInstaller是一个用来将Python程序打包成一个独立可执行文件的第三方包。因是第三方包,所以需要安
- poplib模块接收邮件python的poplib模块是用来从pop3收取邮件的,也可以说它是处理邮件的第一步。POP3协议并不复杂,它也是