赞
踩
# !/usr/bin/env python3
# -*- coding:utf-8 -*-
import pymysql
import time
from datetime import datetime
import os
import shutil
dbUser='db_order'
dbPasswd='db_order'
dbHost='118.89.139.104'
remoteDBUser='db_order'
remoteDBPasswd='db_order'
remoteDBHost='192.168.16.125'
dbCharset = 'utf8'
backupDir = '/home/developer/mysqlbackup'
backupDate = time.strftime("%Y%m%d")
backupDir = '/home/developer/mysqlbackup/' + backupDate
conn = None
cur = None
try:
if not os.path.exists(backupDir):
os.makedirs(backupDir)
print("backupDir exists:%s"%os.path.exists(backupDir))
conn = pymysql.connect(host=dbHost,port= 3306,user = dbUser,passwd=dbPasswd,db='db_order')
cur = conn.cursor()
cur.execute("show databases like 'db_%'")
databases = cur.fetchall()
print("databases:%s" %databases)
print('The database backup to start! %s' %time.strftime('%Y-%m-%d %H:%M:%S'))
for db in databases:
dbName = db[0]
print('databaseName:%s'%dbName)
fileName = '%s/%s_%s.sql' %(backupDir,dbName,backupDate)
print('fileName:%s'%fileName)
if os.path.exists(fileName):
os.remove(fileName)
os.system("mysqldump -h%s -u%s -p%s %s --default_character-set=%s > %s/%s_%s.sql" %(dbHost,dbUser,dbPasswd,dbName,dbCharset,backupDir,dbName,backupDate))
print('The database backup success! %s' %
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。