Python實現(xiàn)定時備份mysql數(shù)據(jù)庫并把備份數(shù)據(jù)庫郵件發(fā)送
一、先來看備份mysql數(shù)據(jù)庫的命令
mysqldump -u root --password=root --database abcDataBase > c:/abc_backup.sql
二、寫Python程序
BackupsDB.py
#!/usr/bin/python # -*- coding: UTF-8 -*- ''''' zhouzhongqing
備份數(shù)據(jù)庫
''' 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 # 第一個參數(shù)確定任務(wù)的時間,返回從某個特定的時間到現(xiàn)在經(jīng)歷的秒數(shù) # 第二個參數(shù)以某種人為的方式衡量時間 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"#發(fā)送者的郵箱 _pwd = "xxxx"#發(fā)送者的密碼 _to = "1030907690@qq.com"#接收者的郵箱 # 如名字所示Multipart就是分多個部分 msg = MIMEMultipart() msg["Subject"] = "商城數(shù)據(jù)庫備份" msg["From"] = _user msg["To"] = _to # ---這是文字部分--- part = MIMEText("商城數(shù)據(jù)庫備份") 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郵件服務(wù)器,端口默認(rèn)是25 s.login(_user, _pwd) # 登陸服務(wù)器 s.sendmail(_user, _to, msg.as_string()) # 發(fā)送郵件 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用來安排某事件的發(fā)生時間,從現(xiàn)在起第n秒開始啟動 schedule.enter(inc, 0, perform_command, (cmd, inc)) # 持續(xù)運行,直到計劃時間隊列變成空為止 schedule.run() if __name__ == '__main__': print("show time after 10 seconds:"); timming_exe("echo %time%", 56400);#每間隔56400秒備份發(fā)送郵件 #46400 基本上是半天
然后命令
py BackupsDB.py
運行程序就可以了。
總結(jié)
以上所述是小編給大家介紹的Python實現(xiàn)定時備份mysql數(shù)據(jù)庫并把備份數(shù)據(jù)庫郵件發(fā)送,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Python使用oslo.vmware管理ESXI虛擬機的示例參考
oslo.vmware是OpenStack通用框架中的一部分,主要用于實現(xiàn)對虛擬機的管理任務(wù),借助oslo.vmware模塊我們可以管理Vmware ESXI集群環(huán)境。2021-06-06淺析Python 實現(xiàn)一個自動化翻譯和替換的工具
這篇文章主要介紹了Python 實現(xiàn)一個自動化翻譯和替換的工具,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04使用Python實現(xiàn)PDF與SVG互轉(zhuǎn)
SVG(可縮放矢量圖形)和PDF(便攜式文檔格式)是兩種常見且廣泛使用的文件格式,本文將詳細(xì)介紹如何使用?Python?實現(xiàn)?SVG?和?PDF?之間的相互轉(zhuǎn)換,感興趣的可以了解下2025-02-02Python實現(xiàn)清理微信僵尸粉功能示例【基于itchat模塊】
這篇文章主要介紹了Python實現(xiàn)清理微信僵尸粉功能,結(jié)合實例形式分析了Python使用itchat模塊刪除微信僵尸粉的相關(guān)原理、操作技巧與注意事項,需要的朋友可以參考下2020-05-05