rename_table

#-*-coding:utf-8 -*-
#!/usr/bin/python

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import os
import time
import MySQLdb
from datetime import datetime

def selectTable(conn,defdb,tbname):
    global plname,tablelist
    try:
        cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
        #conn.select_db('INFORMATION_SCHEMA')
        sql="SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=\'%s\' and TABLE_NAME = \'%s\';" % (defdb,tbname)
        #print sql
        cur.execute(sql)
        result = cur.fetchone()
        #print result
        conn.commit()
        cur.close()
    except MySQLdb.Error,e:
        print "Mysql Error %d:%s"%(e.args[0],e.args[1])
    return result

def rename_table(conn,zoneid,defdb):
    stri=""
    global plname,tablelist
    try:
        cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
        conn.select_db(defdb)
        sql=" SELECT operateTime from b_flowheromoney order by operateTime limit 1"  
        cur.execute(sql)
        conn.commit()
        result=cur.fetchone()
        if result is not None:
            print result['operateTime']
            oldtime=result['operateTime']
            date_space=int(time.time())-oldtime
            if date_space >60*60*24*10 and date_space >0 and oldtime>60*60:
                print "新舊錶間隔大於10天" 
                for one in tablelist:
                    if selectTable(conn,defdb,"%s_bak" % one) is not None:
                        print "開始 rename table %s to %s_bak" % (one,one)
                        cur.execute("truncate table %s_bak" % (one))
                        conn.commit()
                    cur.execute("drop table if exists  %s_bak") % one
                    conn.commit()
                    cur.execute("rename table %s to %s_bak" % (one))
                    conn.commit()

            else:
                print "rename table 不超過10天,不操作"
        else:
             print "新表沒有超過10天,不操作備份"
        cur.close()

    except MySQLdb.Error,e:
        print e
        print "MySQL Error %d:%s" %  (e.args[0],e.args[1])  

if __name__=="__main__":
    tablelist=['b_flowheromoney','b_flowmmcache','b_flowequip','b_flowheroexp','b_flowtool','b_flowherostrength','b_flowheroattr','b_flowfabao','m_logininfo','m_loginout']
    plname=sys.argv[1]
    zoneid=sys.argv[2]
    gsport=sys.argv[3]
    if os.path.exists("/usr/jyserver/%s/server/config/dbs.properties" % str(gsport)):
        with open("/usr/jyserver/%s/server/config/dbs.properties" % str(gsport)) as f:
            for line in f:
                print line
                zoneid=int(line.split("=")[0])
                print zoneid
                address=line.split("/")[2].split(":")[0]
                print address
                database_name=line.split("&")[0].split("/")[3]
                print database_name
                if not zoneid is None and not address is None:
                    defdb="xmxy_%s_%s" % (plname,zoneid)
                    conn = MySQLdb.connect(host="%s" % address,user='xxxx',passwd='xxxx',port=3306,charset='utf8')  

                    rename_table(conn,zoneid,defdb)
                else:
                    print "zoneid and address is null"
    else:
        print "table is not exists"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章