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

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

#! /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

  

Python检查 文件备份是否正常 云备份进程是否正常运行的更多相关文章

  1. [Windows Server 2008] 搭建数据云备份

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:如何搭建数 ...

  2. Microsoft云备份解决方案Azure Backup的常见配置问题

    这篇博客文章有助于解决 Microsoft云备份解决方案(即 Azure Backup)的常见配置问题.客户通常会在安装或注册 Azure Backup时遇到这些问题.以下是有关如何诊断和解决问题的建 ...

  3. linuxserver本地和百度云备份脚本小试

    本地单文件上传脚本.命名uf 这是在本机上做的測试,利用bpcs_uploader脚本实现,仅仅是进行简单的封装.自己主动完好云端文件路径. 技术要点:使用dirname获取文件所在文件夹.使用pwd ...

  4. windows下python检查文件是否被其它文件打开

    windows下python检查文件是否被其它文件打开.md 有时候我们需要能够判断一个文件是否正在被其它文件访问,几乎不可避免的要调用操作系统接口 from ctypes import cdll i ...

  5. python并发编程之Queue线程、进程、协程通信(五)

    单线程.多线程之间.进程之间.协程之间很多时候需要协同完成工作,这个时候它们需要进行通讯.或者说为了解耦,普遍采用Queue,生产消费模式. 系列文章 python并发编程之threading线程(一 ...

  6. Python 3 并发编程多进程之守护进程

    Python 3 并发编程多进程之守护进程 主进程创建守护进程 其一:守护进程会在主进程代码执行结束后就终止 其二:守护进程内无法再开启子进程,否则抛出异常:AssertionError: daemo ...

  7. shell脚本监控k8s集群job状态,若出现error通过触发阿里云的进程监控报警

    #!/bin/bash while [ 1 ] do job_error_no=`kubectl get pod -n weifeng |grep -i "job"|grep -c ...

  8. shell脚本监控阿里云专线网络状态,若不通通过触发阿里云的进程监控报警

    #!/bin/bash while [ 1 ] do rtt=`ping -c 3 15.0.160.18 |grep rtt |awk '{print $4}' |awk -F'/' '{print ...

  9. Python检查数组元素是否存在类似PHPisset()方法

    Python检查数组元素是否存在类似PHP isset()方法 sset方法来检查数组元素是否存在,在Python中无对应函数,在Python中一般可以通过异常来处理数组元素不存在的情况,而无须事先检 ...

随机推荐

  1. [贪心]P1049 装箱问题

    装箱问题 题目描述 有一个箱子容量为V(正整数,0≤V≤20000),同时有n个物品(0<n≤30),每个物品有一个体积(正整数). 要求n个物品中,任取若干个装入箱内,使箱子的剩余空间为最小. ...

  2. JAVAEE_Servlet_20_登录注册功能

    实现登录注册功能 注册功能 import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import j ...

  3. 面试有关TCP常问的几个问题

    在面试中网络问题是一定会考察的,而TCP协议则是考察网络知识的重点.经常会被问道的问题如下: 请讲一下TCP协议建立连接的过程 请介绍TCP协议中的三次握手和四次挥手是怎么样的 为什么TCP协议要三次 ...

  4. Jenkins 自动触发执行的配置

    1. 两种触发方式 2. jenkins 和 github 同步配置 ngrok 安装 webhook 配置 1. 两种触发条件 Jenkins 中建立的任务是可以设置自动触发,更进一步的实现自动化. ...

  5. vue2整个项目中,数据请求显示loading图----------未完成阅读,码

    一般项目中,有时候会要求,你在数据请求的时候显示一张gif图片,然后数据加载完后,消失.这个,一般只需要在封装的axios中写入js事件即可.当然,我们首先需要在app.vue中,加入此图片.如下: ...

  6. 发现个利器--FastAPI(Py3.6+版本)

    from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"H ...

  7. 病毒木马查杀实战第021篇:Ring3层主动防御之编程实现

    前言 我们这次会依据上次的内容,编程实现一个Ring3层的简单的主动防御软件.整个程序使用MFC实现,程序开始监控时,会将DLL程序注入到explorer.exe进程中,这样每当有新的进程创建,程序首 ...

  8. C#-DUP

    void jianting() { int port = 8888; UdpClient udpclient = new UdpClient(port); IPEndPoint ipendpoint ...

  9. 前端不得不了解的TCP协议

    背景 早期的网络是基于OSI(开放式系统互联网,一般叫OSI参考模型)模型,该模型是由ISO国际标准组织制定的,包含了七层(应用层.表示层.会话层.传输层.网络层.数据链路层.物理层),即复杂又不实用 ...

  10. [LeetCode每日一题]1143. 最长公共子序列

    [LeetCode每日一题]1143. 最长公共子序列 问题 给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度.如果不存在 公共子序列 ,返回 0 . 一个字符串 ...