Python實現(xiàn)備份MySQL數(shù)據(jù)庫的方法示例
本文實例講述了Python實現(xiàn)備份MySQL數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:
#!/usr/bin/env python # -*- coding:utf-8 -*- #導(dǎo)入模塊 import MySQLdb import time import datetime import os """ Purpose: 備份數(shù)據(jù)庫 Created: 2015/5/12 Modified:2015/5/12 @author: guoyJoe """ dbUser='root' dbPasswd='root' dbHost='192.168.1.6' dbCharset = 'utf8' backupDir = '/u02/backup/mysql' backupDate = time.strftime("%Y%m%d") #查出MySQL中所有的數(shù)據(jù)庫名稱 sqlStr1 = "show databases like 'db%'" try: connDB= MySQLdb.connect("192.168.1.6","root","root","test" ) connDB.select_db('test') curSql1=connDB.cursor() curSql1.execute(sqlStr1) allDatabase = curSql1.fetchall() print 'The database backup to start! %s' %time.strftime('%Y-%m-%d %H:%M:%S') for db in allDatabase: dbName = db[0] fileName = '%s/%s_%s.sql' %(backupDir,backupDate,dbName) print 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,backupDate,dbName)) print 'The database backup success! %s' %time.strftime('%Y-%m-%d %H:%M:%S') #異常 except MySQLdb.Error,err_msg: print "MySQL error msg:",err_msg
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python+MySQL數(shù)據(jù)庫程序設(shè)計入門教程》、《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)學(xué)運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python logging.basicConfig不生效的原因及解決
今天小編就為大家分享一篇python logging.basicConfig不生效的原因及解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02對django2.0 關(guān)聯(lián)表的必填on_delete參數(shù)的含義解析
今天小編就為大家分享一篇對django2.0 關(guān)聯(lián)表的必填on_delete參數(shù)的含義解析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08python實現(xiàn)字符串加密 生成唯一固定長度字符串
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)字符串加密,生成唯一固定長度字符串,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03