向python进军。。。。。。
运行所需环境:python,MySQLdb
贴下自己写的关于sql备份的python脚本:
#!/usr/bin/env python
import MySQLdb
import os
import time
dataList=[]
remoteAddr='xxxx'
user='root'
password='xxxx'
dbConnect = MySQLdb.connect(host="xxxx",user="xxxx",passwd="xxxx")
cursor=dbConnect.cursor()
sql="show databases"
cursor.execute(sql)
dbConnect.commit()
data=cursor.fetchall()
for list in data:
# print list[0]
dataList.append(list[0])
#print data
print dataList
dataList.remove("information_schema")
dataList.remove("mysql")
dataList.remove("test")
#print dataList
dbConnect.close()
time=time.strftime("%Y-%m-%d")
backup_dir="/tmp/xiao/%s" %time
if not os.path.exists(backup_dir):
os.makedirs(backup_dir)
print "Backup directory created......"
for mysql in dataList:
mysqldump="mysqldump -u%s -p%s -h%s %s>%s/%s.sql"%(user,password,remoteAddr,mysql,backup_dir)
os.system(mysqldump)
print "All databases is backuped ......"