惜风不起、唯有努力!
python备份mysql数据库

python备份mysql数据库

//下面是源代码

#!/usr/bin/python
#coding=utf8

import os
import sys
import time

#parameter01 = sys.argv[1]
#parameter02 = sys.argv[2]
#print(parameter01)
#print(parameter02)
def mkdir(path):
    path = path.strip()
    path = path.rstrip("\\")
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)
        print(path + ' 创建成功')
        return True
    else:
        print(path + ' 目录已存在')
        return False
mkpath = "/opt/dbtest"
mkdir(mkpath)
datetime = time.strftime('%Y_%m_%d-%H:%M')
bakpath = "/opt/dbtest/"
listd = ['zabbix','test','xxxx']
for db in listd:
    if db == 'zabbix':
        print(db)
        bakup = "mysqldump -uroot -p123456"+" " + db + "|gzip > /opt/dbtest/zabbix-db_"+datetime+".sql.gz"
        print(bakup)
        os.system(bakup)
    elif db == 'test':
        print(db)
        bakup = "mysqldump -uroot -p123456" + " " + db + "| gzip > /opt/dbtest/test-db_"+datetime+".sql.gz"
        os.system(bakup)
    else:
        print(db)

发表回复

您的电子邮箱地址不会被公开。