日期:2014-05-16 浏览次数:20647 次
copyright:ldf
?
import os
if __name__ == '__main__':
?? //使用管道执行mysql命令获取UserDB数据库所有表
??? p = os.popen( "mysql --database=UserDB? -e 'show tables'" )
??? tables= ["user_log", "monster_info", "user_pet"]
?
//遍历所有的刚才列出所有的表列表,相当于java?List
??? data = [ line.strip() for line in p.readlines() ]
????
???//删除第一行mysql client
??? data=data[1:]
???
??? dumpTables=[]
?
//开始找我们需要的表,剔除不需要同步的表
??? for table in data:
??????? if not table.startswith("user"):
??????????? doNotDump = True
??????????? for doNotTable in tables:
??????????????? if doNotTable == table:
??????????????????? doNotDump = False
??????????????????? break;
???????????
??????????? if doNotDump:
??????????????? dumpTables.insert(0,table)??
???????????????
??? //删除sysdb.sql
??? os.system("rm dbinfo.sql");
???
//导出我们需要同步的表sql
??? for table in dumpTables:
??????? command =? "mysqldump --opt? -d UserDB? "
??????? command += table + " >> dbinfo.sql"
??????? os.system(command)
????
//打包导出的表
os.system("tar -cjf dbinfo.tar.bz2 ./dbinfo.sql")
????
//上传打包的tar到线上服务器
os.system("scp ./dbinfo.tar.bz2? root@xxx.xx1.206.100:/root/tempdir/")
????
//解压线上tar包
?? os.system("ssh root@xxx.xx1.206.100 'tar -xjf /root/tempdir/dbinfo.tar.bz2' ")
????//执行sql导入
os.system("ssh root@xxx.xx1.206.100 'mysql UserDB? < dbinfo.sql'")