python数据库定时备份,并删除以前备份的数据

python定时备份多个数据库,并删除以前备份的数据

# -*- coding: utf-8 -*-
# Import required python libraries
# pancx 2019-04-10
import os
import time
import datetime
import shutil
# MySQL database details to which backup to be done. Make sure below user having enough privileges to take databases backup.

#数据库ip
DB_HOST = "cdb-.tcdb.com"
#数据库端口
DB_PORT = "10111"
#数据库用户名
DB_USER = "root"
#数据库密码
DB_USER_PASSWORD = "\'(ink^zm+36Xd=&18!\'"
#备份的数据库
DB_NAMES = "order,act,ggg"
#备份到的路径
BACKUP_PATH = "/sqlbak/"

# Getting current datetime to create seprate backup folder like "20190412".
DATETIME = time.strftime('%Y%m%d')
TODAYBACKUPPATH = BACKUP_PATH + DATETIME

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

today = datetime.datetime.now()
for item in os.listdir(BACKUP_PATH):
    try:
        foldername = os.path.split(item)[1]
        day = datetime.datetime.strptime(foldername, "%Y%m%d")
        diff = today - day
        if diff.days >= 60:
            print('- - - del folder two months ago: ' + BACKUP_PATH + item)
            shutil.rmtree(BACKUP_PATH + item)
    except:
        pass

# Code for checking if you want to take single database backup or assinged multiple backups in DB_NAME.
DBS = DB_NAMES.split(",")
for DB_NAME in DBS:
    # Starting actual database backup process.
    DB_PATH = TODAYBACKUPPATH + "/" + DB_NAME + ".sql.gz"

    print("- - - baking '" + DB_NAME + ".sql'")
    dumpcmd = "mysqldump -h" + DB_HOST + " -P" + DB_PORT + " -u" + DB_USER + " -p" + DB_USER_PASSWORD + " --databases " + DB_NAME + " --set-gtid-purged=OFF |gzip > " + DB_PATH
    os.system(dumpcmd)

print("- - - Your backups has been created in '" + TODAYBACKUPPATH + "' directory")

# mysqldump: [Warning] Using a password on the command line interface can be insecure. 在命令行输入密码,就会提示这些安全警告信息(警告信息可忽略)

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值