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

运行程序就可以了。

转载于:https://my.oschina.net/u/3828224/blog/1805254

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值