日期:2014-05-16  浏览次数:20508 次

MySQL 5.5 DBA工具 多进程dump 多进程load 多进程备份还原 python 脚本

需要安装 python MySQL-python gzip : yum install python MySQL-python gzip -y

原文链接:http://blog.csdn.net/ylqmf/article/details/7993701

dump 脚本

'''
Created on 2012-8-20
mysql dump to load
@author: tudou@b2c.xiaomi.com
'''
import os,time,MySQLdb,multiprocessing

mysql_bak='/tmp/mysqlbak'
mysql_base='/opt/soft/mysql_5.5.25'
unix_socket='/tmp/mysql.sock'
dump_user='root'
dump_pwd='123456'
dump_database=['test','mysql']

def start_process():
    print ('MySQLdump starting', multiprocessing.current_process().name)

class mysqldump(object):
    def __init__(self,conf):
        self.conf=conf
        self.dumpdir=mysql_bak
        
    def dump(self):
        #create dir
        self.dumpdir += '/'+str(time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time())))
        for dir in dump_database:
            os.system('mkdir -p '+self.dumpdir+'/'+dir)
            os.system('mkdir -p '+self.dumpdir+'/'+dir+'/schema')
        os.system('chmod 777 -R '+self.dumpdir)
        #get create table
        for dir in dump_database:
            self.getschemainfo(dir)
        #dump per table
        self.getdbinfo()
    
    def getschemainfo(self,dbconf):
        os.system(mysql_base+'/bin/mysqldump -d --add-drop-table -u'+dump_user+' -p'+dump_pwd+' -S'+unix_socket+' '+dbconf+' > '+self.dumpdir+'/'+dbconf+'/schema/schemainfo' )
        os.system('gzip '+self.dumpdir+'/'+dbconf+'/schema/schemainfo')
        os.system(mysql_base+'/bin/mysqldump -tdRE -u'+dump_user+' -p'+dump_pwd+' -S'+unix_socket+' '+dbconf+' > '+self.dumpdir+'/'+dbconf+'/schema/objectinfo' )
        os.system('gzip '+self.dumpdir+'/'+dbconf+'/schema/objectinfo')
    
    def getdbinfo(self):
        con=db(self.conf)
        sql="SELECT TABLE_SCHEMA,TABLE_NAME FROM information_schema.`TABLES` WHERE TABLE_SCHEMA IN ('"+ "','".join(dump_database) +"')"
        re = list(con.execute(sql))
        #inputs=list()
        pool_size = multiprocessing.cpu_count()
        pool = multiprocessing.Pool(processes=pool_size,initializer=start_process,)
        for tb in re:
            #inputs.append({'dbname':tb[0],'tablename':tb[1]})
            pool.apply_async(self.dumplay({'dbname':tb[0],'tablename':tb[1]}))
            #self.dumplay({'dbname':tb[0],'tablename':tb[1]})
        #print inputs
        pool.close() # no more tasks
        pool.join()  # wrap up current tasks
        
    def dumplay(self,dbconf):
        loadname=self.dumpdir+"/"+dbconf['dbname']+"/"+dbconf['tablename']+".sql"
        con=db(self.conf)
        sql="SELECT * FROM `"+dbconf['dbname']+"`.`"+dbconf['tablename']+"` INTO OUTFILE '"+loadname+"'"
        #print sql
        con.executeNoQuery(sql)
        self.dogzip(loadname)
        
    def dogzip(self,fileconf):
        os.system('gzip '+fileconf)
        
'''

'''
class MySQLHelper(object):
    @staticmethod
    def getConn(conf):
        pot = 3306
        if(conf.has_key('port')):
            pot=conf['port']
        dbname='test'
        if(conf.has_key('db')):
            dbname=conf['db']
        
        if(conf.has_key('socket')):
            return MySQLdb.connect(host=conf['host'],unix_socket=conf['socket'],user=conf['user'],passwd=conf['pwd'],db=dbname)
        else:
            return MySQLdb.connect(host=conf['host'],port=pot,user=conf['user'],passwd=conf['pwd'],db=dbname)
'''

'''
class db (object):
    def __init__(self,conf):
        self.conn=None
        self.conn=MySQLHelper.getConn(conf)
    
    def execute(self,sql,mod=''):
        if(mod=='dict'):
            cursor=self.conn.cursor(MySQLdb.cursors.DictCursor)
        else:
            cursor=self.conn.cursor()
        cursor.execute(sql)
        set=cursor.fetchall()
        return set
    
    def executeNoQuery(self,sql,param={}):
        cursor=self.