您好,登錄后才能下訂單哦!
該腳本針對批量導(dǎo)入數(shù)據(jù)sql文件,data目錄存放所要導(dǎo)入的sql文件,list.txt存放要導(dǎo)入的列表信息。
腳本內(nèi)容如下:
#!/usr/bin/env python # -*- coding:utf-8 -*- import os, sys, logging, datetime # 日志文件配置 if not os.path.isdir('logs'): os.mkdir('logs') logging.basicConfig(level=logging.INFO, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', 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__) # 從本地配置文件中讀取主機列表信息 def readinfo(confile): info_list = [] if os.path.isfile(confile): with open(confile, 'r+') 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"本地配置文件%s不存在!" % confile) sys.exit(1) def readsqlfile(data): sql_list = [] # 判斷本地的sql文件是否存在目錄是否存在。 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"本地%s目錄中沒有要導(dǎo)入的sql文件!"%data) else: logger.error(u"%s 不是一個目錄。"%data) return sql_list # 循環(huán)info_list和sql_list將sql導(dǎo)入每一個服務(wù)器 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 -h'%s' %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 導(dǎo)入sql:%s 完畢!"%(ser['id'],ser['host'],ser['name'],sql_file) succ_list.append(msg) logger.info(msg) else: msg = "%s -- %s -- %s 導(dǎo)入sql:%s 失?。? % (ser['id'], ser['host'], ser['name'], sql_file) fail_list.append(msg) logger.error(msg) sys.exit(1) return succ_list,fail_list def yesorno(confile,data): print "數(shù)據(jù)庫列表如下:" info_list = readinfo(confile) for ser in info_list: print "%s --- %s ---- %s"%(ser['id'],ser['host'],ser['name']) iput = raw_input("你是否要導(dǎo)入sql文件 %s,請輸入yes or no:" % (os.listdir(data))) while True: if iput.lower() in ['y','yes']: print "你選擇了繼續(xù)執(zhí)行程序動作!" break elif iput.lower() in ['n','no']: print "你選擇了退出程序動作!" sys.exit(1) else: print "你輸入了非法的字符,腳本并未執(zhí)行導(dǎo)入sql文件的任何動作,程序即將退出。" sys.exit(1) if __name__ == '__main__': # 基本信息配置變量 data = r'data' # 存放本地sql文件的目錄 confile = 'list.txt' # 存放服務(wù)器列表信息,格式如: id host dbname : 1 192.168.2.20 game_name_cn1 # MySQL變量 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'總共執(zhí)行成功%s條. \r\n 總共執(zhí)行失敗%s條.'%(len(succ), len(fail))) endtime = datetime.datetime.now() logger.info(u"\r\nTotal time: %s ms" % ((endtime - startime).microseconds / 1000))
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。