向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(%s,%s,%s)%(remoteAddr,user,password)
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 ......"
本文出自 “本少爷很闲” 博客,请务必保留此出处http://yingzi234.blog.51cto.com/3374280/1430377
原文地址:http://yingzi234.blog.51cto.com/3374280/1430377