python 备份Mysql数据库

 

经常借鉴网上的知识,但是能用的实在是少的

但是  。我写的办法都是亲测可用的   大家可以放心使用   共创和谐网络环境

 

# coding=utf-8
# !/usr/bin/python
# author:shoooghooog
import os
import time
import schedule
from datetime import datetime

# MySQL database details to which backup to be done. Make sure below user having enough privileges to take databases backup.
# To take multiple databases backup, create any file like /backup/dbnames.txt and put databses names one on each line and assignd to DB_NAME variable.
DB_HOST = ''
DB_USER = ''
DB_USER_PASSWORD = ''
# DB_NAME = '/backup/dbnames.txt'
DB_NAME = ''
BACKUP_PATH = '/backup/'


def backup():
    # Getting current datetime to create seprate backup folder like "12012013-071334".
    DATETIME = time.strftime('%m%d%Y-%H%M%S')

    TODAYBACKUPPATH = BACKUP_PATH + DATETIME

    # Checking if backup folder already exists or not. If not exists will create it.
    print("creating backup folder")
    if not os.path.exists(TODAYBACKUPPATH):
        os.makedirs(TODAYBACKUPPATH)

    # Code for checking if you want to take single database backup or assinged multiple backups in DB_NAME.
    print("checking for databases names file.")
    if os.path.exists(DB_NAME):
        file1 = open(DB_NAME)
        multi = 1
        print("Databases file found...")
        print("Starting backup of all dbs listed in file " + DB_NAME)
    else:
        print("Databases file not found...")
        print("Starting backup of database " + DB_NAME)
        multi = 0

    # Starting actual database backup process.
    if multi:
        in_file = open(DB_NAME, "r")
        flength = len(in_file.readlines())
        in_file.close()
        p = 1
        dbfile = open(DB_NAME, "r")

        while p <= flength:
            db = dbfile.readline()  # reading database name from file
            db = db[:-1]  # deletes extra line
            dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"
            os.system(dumpcmd)
            p = p + 1
        dbfile.close()
    else:
        db = DB_NAME
        dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"
        os.system(dumpcmd)

    print("Backup script completed")
    print("Your backups has been created in '" + TODAYBACKUPPATH + "' directory")
    print('Backups Time : %s' % datetime.now().strftime("%Y-%m-%d %H:%M:%S"))


"""
    schedule.every(1).minutes.do(job)
    schedule.every().hour.do(job)
    schedule.every().day.at("10:30").do(job)
    schedule.every(5).to(10).days.do(job)
    schedule.every().monday.do(job)
    schedule.every().wednesday.at("13:15").do(job)
"""

schedule.every().day.at("12:00").do(backup)

if __name__ == '__main__':
    while True:
        schedule.run_pending()
        timetime = time.strftime('%Y:%m:%d-%H:%M:%S')
        print("北京时间:%s"%timetime)
        time.sleep(30)

一目了然呀 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值