内容简介:这篇文章主要介绍了Python实现定时备份mysql数据库并把备份数据库邮件发送的相关资料,需要的朋友可以参考下
一、先来看备份 mysql 数据库的命令
mysqldump -u root --password=root --database abcDataBase > c:/abc_backup.sql
二、写 Python 程序
BackupsDB.py
#!/usr/bin/python # -*- coding: UTF-8 -*- ''''' zhouzhongqing
备份数据库
'''
import os
import time
import sched
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
# 第一个参数确定任务的时间,返回从某个特定的时间到现在经历的秒数
# 第二个参数以某种人为的方式衡量时间
schedule = sched.scheduler(time.time, time.sleep);
def backupsDB():
# 如果是 linux 改下路径就可以了
cmdString = 'D:/php/phpStudy/MySQL/bin/mysqldump -u root --password=root --database abcDataBase > c:/abc_backup.sql';
os.system(cmdString);
def sendMail():
_user = "mall@xxxx.com"#发送者的邮箱
_pwd = "xxxx"#发送者的密码
_to = "1030907690@qq.com"#接收者的邮箱
# 如名字所示Multipart就是分多个部分
msg = MIMEMultipart()
msg["Subject"] = "商城数据库备份"
msg["From"] = _user
msg["To"] = _to
# ---这是文字部分---
part = MIMEText("商城数据库备份")
msg.attach(part)
# ---这是附件部分---
# 类型附件
part = MIMEApplication(open('c:/abc_backup.sql', 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename="abc_backup.sql")
msg.attach(part)
s = smtplib.SMTP("smtp.exmail.qq.com", timeout=30) # 连接smtp邮件服务器,端口默认是25
s.login(_user, _pwd) # 登陆服务器
s.sendmail(_user, _to, msg.as_string()) # 发送邮件
s.close();
def perform_command(cmd, inc):
# 安排inc秒后再次运行自己,即周期运行
schedule.enter(inc, 0, perform_command, (cmd, inc));
os.system(cmd);
backupsDB();
sendMail();
def timming_exe(cmd, inc=60):
# enter用来安排某事件的发生时间,从现在起第n秒开始启动
schedule.enter(inc, 0, perform_command, (cmd, inc))
# 持续运行,直到计划时间队列变成空为止
schedule.run()
if __name__ == '__main__':
print("show time after 10 seconds:");
timming_exe("echo %time%", 56400);#每间隔56400秒备份发送邮件
#46400 基本上是半天
然后命令
py BackupsDB.py
运行程序就可以了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Python实现定时备份mysql数据库并把备份数据库邮件发送
- Zabbix 数据库备份
- MySQL数据库的备份案例
- MSSQL实践-数据库备份加密
- Linux下定时备份数据库
- Oracle数据库逻辑备份与恢复
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Web Designer's Idea Book, Vol. 2
Patrick McNeil / How / 2010-9-19 / USD 30.00
Web Design Inspiration at a Glance Volume 2 of The Web Designer's Idea Book includes more than 650 new websites arranged thematically, so you can easily find inspiration for your work. Auth......一起来看看 《The Web Designer's Idea Book, Vol. 2》 这本书的介绍吧!