- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import MySQLdb
- import os
- import sys
- import time
- currentDate=time.strftime('%Y%m%d',time.localtime())
- def getConn(host,user,password,port):
- conn=MySQLdb.connect(host=host,user=user,passwd=password,port=int(port))
- cur=conn.cursor()
- return cur
- #def execSql(cur,sql):
- # cur.execute(sql)
- # data=cur.fetchall()
- # return data
- def getDabase(cur):
- sql='''select table_schema,table_name from information_schema.tables where table_schema !="mysql" and table_schema !="information" and table_schema !="performance_schema" and table_schema !="information_schema"'''
- cur.execute(sql)
- data=cur.fetchall()
- return data
- def mysqlDump(basedir,host,user,password,port):
- dump=basedir +' '+'-h' +' '+host+' '+'-u'+' '+user+' '+'-p'+password+' '+'-P'+' '+str(port)
- for i in a:
- databases=i[0]
- table=i[1]
- dump1=dump+' '+databases+' '+table +' '+'>'+databases+'_'+table+'_'+currentDate+'.'+'sql'
- time.sleep(1)
- os.system(dump1)
- if __name__=="__main__":
- cur=getConn("192.168.1.114",'dlan','root123',3306)
- #sql="select user,host from mysql.user"
- #aa=execSql(cur,sql)
- a=getDabase(cur)
- aa=mysqlDump('/usr/local/mysql/bin/mysqldump','192.168.1.114','dlan','root123',3306)
来源: http://www.bubuko.com/infodetail-1870001.html