def restoreMysql(name,port,sqlfile,**kw): createInstance(name,port,**kw) cnf=getCNF(name) mc=MYSQLDConfig(cnf) port=mysqld_vars['port'] cmd='mysql -h 127.0.0.1 -uroot -p %s < %s' %(port,sqlfile) p=Popen(cmd,stdout=PIPE,shell=True) stdin,stdout=p.communicate() p.returncode
def connMysql(name): cnf=getCNF(name) if os.path.exists(cnf): mc=MYSQLDConfig(cnf) port=int(mc.mysqld_vars['port']) host='127.0.0.1' user='******' conn=MySQLdb.connect(host=host,port=port,user=user) cur=conn.cursor() return cur
def createInstance(name, port): cnf = getCNF(name) exists_conf = readConfs() for conf in exists_conf: if conf.split('/')[-1][-4] == name: print >> sys.stderr, "Instance: %s in exists" % name sys.exit(-1) if checkPort(conf, port): print >> sys.stderr, "Port:%s in exists" % port sys.exit(-1) if not os.path.exists(cnf): c = _getDict(name, port) mc = MYSQLDConfig(cnf, **c) mc.save() datadir = os.path.join(MYSQL_DATA_DIR, name) if not os.path.exists(datadir): mysql_install(name) setOwner(datadir) mysql_run(name)
def backupMysql(name): import datetime now=datetime.datetime.now() ts=now.strftime('%F-%m-%d.%H:%M:%S') sqlfile=os.path.join(MYSQL_BACK_DIR,name,'%s.sql' %ts) dir=os.path.dirname(sqlfile) if not os.path.exists(dir): os.makedirs(dir) cnf=getCNF(name) mc=MYSQLDConfig(cnf) port=mc.mysqld_vars['port'] cmd="mysqldump -A -F -x --master-data=1 --host=127.0.0.1 --port=%s --user=root > %s" %(port,sqlfile) p=Popen(cmd,stdout=PIPE,shell=True) stdin,stdout=p.communicate() p.returncode
def diffMyVariables(name): dict={} cur=connMysql(name) vars=getMyVariables(cur) cnf=getCNF(name) mc=MYSQLDConfig(cnf) for k,v in vars.items(): if v.isdigit() and len(v)>4: c="%.2f" %float(int(v)/1000) +'M' dict[k]=c if v.isalpha(): dict[k]=v if v.islower(): dict[k]=v for k,v in mc.mysqld_vars.items(): k=k.replace('-','_') if k in vars and v !=vars[k]: print k,v,vars[k]
def checkPort(conf_file,port): mc=MYSQLDConfig(conf_file) if mc.mysqld_vars['port'] ==port: return True else: return False
def setMyVariables(name,k,v): cnf=getCNF(name) mc=MYSQLDConfig(cnf) mc.set_vars(k,v) mc.save()