Script for python to import database
The script is aimed at bulk importing data sql files, the data directory stores the sql files to be imported, and list.txt stores the list information to be imported.
The script reads as follows:
#! / usr/bin/env python#-*-coding:utf-8-*-import os, sys, logging, datetime# log file configuration if not os.path.isdir ('logs'): os.mkdir (' logs') logging.basicConfig (level=logging.INFO, format='% (asctime) s% (name)-12s% (levelname)-8s% (message) slots, datefmt='%Y-%m-%d% H% M' Filename='logs/importable.log' Filemode='a') console = logging.StreamHandler () console.setLevel (logging.INFO) formatter = logging.Formatter ('% (message) s') console.setFormatter (formatter) logging.getLogger ('') .addHandler (console) logger = logging.getLogger (_ name__) # read host list information from the local configuration file def readinfo (confile): info_list = [] if os.path.isfile (confile): with open (confile) As f: for line in f.readlines (): if not line.startswith ("#"): dict = {} dict ['id'] = line.split () [0] dict [' host'] = line.split () [1] dict ['name '] = line.split () [2] info_list.append (dict) return info_list else: logger.error (u "Local profile% s does not exist!" % confile) sys.exit (1) def readsqlfile (data): sql_list = [] # determines whether the local sql file exists or the directory exists. If os.path.isdir (data): for sql_file in os.listdir (data): if sql_file: sql_list.append (os.path.join (data,sql_file)) else: logger.error (u "there are no sql files to import in the local% s directory!" % data) else: logger.error (u "s is not a directory." % data) return sql_list# loop info_list and sql_list import sql into each server def importhost (info_list, sql_list): succ_list = [] fail_list = [] for ser in info_list: for sql_file in sql_list: cmd = "% s/mysql-u% s-p% s-hacks% s <% s"% (mysql_path, mysql_user, mysql_passwd Ser ['host'], ser [' name'], sql_file) status = os.system (cmd) if status = 0: msg = "% s -% s -% s import sql:%s finished!" % (ser ['id'], ser [' host'], ser ['name'], sql_file) succ_list.append (msg) logger.info (msg) else: msg = "% s -% s -% s failed to import sql:%s!" % (ser ['id'], ser [' host'], ser ['name'], sql_file) fail_list.append (msg) logger.error (msg) sys.exit (1) return succ_list,fail_listdef yesorno (confile) Data): print "database list is as follows:" info_list = readinfo (confile) for ser in info_list: print "% s--% s--% s"% (ser ['id'], ser [' host'], ser ['name']) iput = raw_input ("do you want to import sql file% s Please type yes or no: "% (os.listdir (data)) while True: if iput.lower () in ['yellows grammatical']: print" you have chosen to continue with the program action! " Break elif iput.lower () in ['nymphomagenol']: print "you chose to exit the program action!" Sys.exit (1) else: print "you entered an illegal character, the script did not perform any action to import the sql file, and the program is about to exit." Sys.exit (1) if _ _ name__ ='_ _ main__': # basic information configuration variable data = rroomdata' # directory where local sql files are stored confile = 'list.txt' # stores server list information Format such as: id host dbname: 1 192.168.2.20 game_name_cn1 # MySQL variable mysql_path ='/ usr/local/mysql/bin' mysql_user = 'root' mysql_passwd =' xirexrt,mf' yesorno (confile,data) startime = datetime.datetime.now () sql_list = readsqlfile (data) info_list = readinfo (confile) succ,fail= importhost (info_list Sql_list) logger.info (u' executed a total of% s successfully. \ r\ nTotal execution failed% s.% (len (succ), len (fail)) endtime = datetime.datetime.now () logger.info (u "\ r\ nTotal time:% s ms"% ((endtime-startime) .microseconds / 1000)