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 = '*****'
#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检查 文件备份是否正常 云备份进程是否正常运行的更多相关文章
- [Windows Server 2008] 搭建数据云备份
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:如何搭建数 ...
- Microsoft云备份解决方案Azure Backup的常见配置问题
这篇博客文章有助于解决 Microsoft云备份解决方案(即 Azure Backup)的常见配置问题.客户通常会在安装或注册 Azure Backup时遇到这些问题.以下是有关如何诊断和解决问题的建 ...
- linuxserver本地和百度云备份脚本小试
本地单文件上传脚本.命名uf 这是在本机上做的測试,利用bpcs_uploader脚本实现,仅仅是进行简单的封装.自己主动完好云端文件路径. 技术要点:使用dirname获取文件所在文件夹.使用pwd ...
- windows下python检查文件是否被其它文件打开
windows下python检查文件是否被其它文件打开.md 有时候我们需要能够判断一个文件是否正在被其它文件访问,几乎不可避免的要调用操作系统接口 from ctypes import cdll i ...
- python并发编程之Queue线程、进程、协程通信(五)
单线程.多线程之间.进程之间.协程之间很多时候需要协同完成工作,这个时候它们需要进行通讯.或者说为了解耦,普遍采用Queue,生产消费模式. 系列文章 python并发编程之threading线程(一 ...
- Python 3 并发编程多进程之守护进程
Python 3 并发编程多进程之守护进程 主进程创建守护进程 其一:守护进程会在主进程代码执行结束后就终止 其二:守护进程内无法再开启子进程,否则抛出异常:AssertionError: daemo ...
- shell脚本监控k8s集群job状态,若出现error通过触发阿里云的进程监控报警
#!/bin/bash while [ 1 ] do job_error_no=`kubectl get pod -n weifeng |grep -i "job"|grep -c ...
- shell脚本监控阿里云专线网络状态,若不通通过触发阿里云的进程监控报警
#!/bin/bash while [ 1 ] do rtt=`ping -c 3 15.0.160.18 |grep rtt |awk '{print $4}' |awk -F'/' '{print ...
- Python检查数组元素是否存在类似PHPisset()方法
Python检查数组元素是否存在类似PHP isset()方法 sset方法来检查数组元素是否存在,在Python中无对应函数,在Python中一般可以通过异常来处理数组元素不存在的情况,而无须事先检 ...
随机推荐
- 文件查找工具 find 详解(附:生产示例)
1. 文件查找:在文件系统上查找符合条件的文件 命令 解释 which 查看可执行文件的位置,只能寻找执行文件,并在PATH变量里面寻找 whereis 查看文件的位置:只能查二进制文件,说明文档,源 ...
- ionic3 StatusBar 不显示问题
import { StatusBar } from '@ionic-native/status-bar'; constructor(private statusBar: StatusBar) { } ...
- JavaScript深入理解-PWA渐进式应用
WPA-渐进式 web 应用 PWA 是什么 渐进式 Web 应用,提升 web app 浏览体验. manifest 应用程序清单 基本介绍: web app manifest是 PWA 技术集合中 ...
- 使用Leaflet创建地图模块
背景 最近需要为某单位开发地图展示系统,因此开始涉略和使用Leaflet这个轻量级地图库. 创建基础地图需要以下几步 引入相关js和css文件,创建基础地图 <div id="map& ...
- Mybatis3源码笔记(八)小窥MyBatis-plus
前言 Mybatis-Plus是一个 MyBatis增强工具包,简化 CRUD 操作,在 MyBatis 的基础上只做增强不做改变,为简化开发.提高效率而生,号称无侵入,现在开发中比较常用,包括我自己 ...
- ServletConfig和ServletContext接口
ServletConfig 在web.xml文件中使用一个或多个init-param元素进行配置后,Tomcat初始化Servlet时,都会将该Servlet的配置信息封装到一个ServletConf ...
- WebGL之绘制三维地球
通过Three.js也许可以很方便的展示出3D模型,但是你知道它是怎么一步一步从构建网格到贴图到最终渲染出3D模型的吗?现在我们直接使用底层的webgl加上一点点的数学知识就可以实现它. 本节实现的效 ...
- aws EKS EFS 上安装mysql Operation notpermitted
在AWS EKS k8s.EFS nfs.mysql.changing ownership of '/var/lib/mysql/': Operation notpermitted 在aws eks ...
- springboot的logback的配置文件
之前敲出来一个 logback 的配置文件,防止以后找起来麻烦在这里存个档 修改了日志输出的内容 设置默认级别为 INFO 正常日志 ( TRACE 到 WARN ) 输出到单独的文件夹 异常日志 ( ...
- 【转】【linux系统】nacos + confd配置nginx
为什么要支持confd,老的应用配置管理模式是启动时读取配置文件,然后重新读取配置文件需要应用重启.一般的配置管理系统都是代码侵入性的,应用接入配置管理系统都需要使用对应的SDK来查询和监听数据的变更 ...