码迷,mamicode.com
首页 > 数据库 > 详细

用python生成一个导出数据库的bat脚本文件

时间:2016-05-30 19:47:31      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

# 环境: python3.x  
  
def getExportDbSql(db, index):                  # 获取导出一个数据库实例的sql语句  
    sql = mysqldump -u%s -p%s -h%s -P%d --default-character-set=utf8 --databases mu_ins_s%s > %s.s%d.mu_ins_%d.sql %(db[user], db[pwd], db[host], db[port], index, db[server], index, index)  
    return sql  
  
  
def createDbBackupFile(fname, dbList):          # 生成数据库导出的语句保存到文件  
    if not fname or not dbList:  
        return False  
  
    f = open(fname, w)  
    if f:  
        f.write(echo @off\n\n)  
        for db in dbList:  
            for index in db[indexList]:  
                f.write(REM %s.s%d\n %(db[server], index))  
                f.write(%s\n\n %getExportDbSql(db, index))  
            f.write(\n)  
  
        f.close()  
        return True  
  
    return False  
  
  
def initDb(user, pwd, host, port, server_name, indexList):  # 生成db字典对象并返回  
    db = {}  
    db[user] = user  
    db[pwd] = pwd  
    db[host] = host  
    db[port] = port  
    db[server] = server_name  
    db[indexList] = indexList  
  
    return db  
  
  
def displayDb(db):  
    print(user =, db[user])  
    print(pwd =, db[pwd])  
    print(host =, db[host])  
    print(port =, db[port])  
    print(server =, db[server])  
    print(indexList =, db[indexList])  
    print(\n)  
  
def displayList(list):  
    for item in list:  
        displayDb(item)  
  
if __name__ == __main__:  
    db1 = initDb(root, 123456, 127.0.0.1, 3306, th1, [10000, 1, 3])  
    db2 = initDb(root, 123456, 127.0.0.1, 3306, th2, [10000, 1])  
      
    dbList = []  
    dbList.append(db1)  
    dbList.append(db2)  
  
    #displayList(dbList)  
      
    createDbBackupFile(export00.bat, dbList) 
# 何问起 hovertree.com

推荐:http://www.cnblogs.com/roucheng/p/pythonyunsuan.html

用python生成一个导出数据库的bat脚本文件

标签:

原文地址:http://www.cnblogs.com/roucheng/p/pythonbat.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!