#!/usr/bin/python # -*- coding: UTF-8 -*- import sys import MySQLdb if len(sys.argv) != 3: print ("usage: please enter host and password for central") port = 4010 db = MySQLdb.connect(host = sys.argv[1], user = "root", passwd = sys.argv[2], db = "central", port=port, charset='utf8' ) dbaasDB = MySQLdb.connect(host = sys.argv[1], user = "root", passwd = sys.argv[2], db = "dbaas", port=port, charset='utf8' ) cursor = db.cursor() dbaasCursor = dbaasDB.cursor() cursor.execute("select * from tenants where status = 'active'") lines = cursor.fetchall() for line in lines: tenantID = line[0] tenantDBName = "tenant_%d" % (tenantID,) print tenantDBName tenantDB = MySQLdb.connect(host = sys.argv[1], user = "root", passwd = sys.argv[2], db = tenantDBName, port=port, charset = 'utf8') tenantCursor = tenantDB.cursor() tenantCursor.execute("select * from clusters" ) clusters = tenantCursor.fetchall() for cluster in clusters: clusterID = cluster[0] print "cluster", clusterID tenantCursor.execute("select * from backups where cluster_id = %d" % (clusterID, )) backups = tenantCursor.fetchall() for backup in backups: dbaasCursor.execute("select * from backups left join backup_schedules on backups.backup_schedule_id = backup_schedules.id where backup_schedules.cluster_id = %d and backups.created_at = '%s' and backups.updated_at = '%s'" % (clusterID, backup[1], backup[2])) newBackup = dbaasCursor.fetchone() if newBackup == None: print "backup can't find in new db cluster id %d name %s" % (clusterID, backup[4]) continue backupCRName = backup[4] if backup[7] == 1: backupCRName = "%s-%d" % (backup[4], backup[0]) print "update backups set old_backup_name = '%s' where id = %d" % (backupCRName, newBackup[0]) dbaasCursor.execute("update backups set old_backup_name = '%s' where id = %d" % (backupCRName, newBackup[0])) tenantDB.close() dbaasDB.commit() dbaasDB.close() db.close()