用python產生一個匯出資料庫的bat指令檔

來源:互聯網
上載者:User

標籤:

# 環境: 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指令檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.