Beispiel #1
0
# Check to see if attempting to use --rpl on the same server
if (opt.rpl_mode or opt.rpl_user) and source_values == dest_values:
    parser.error("You cannot use the --rpl option for copying on the "
                 "same server.")

# Check replication options
check_rpl_options(parser, opt)
    
# Build list of databases to copy
db_list = []
for db in args:
    grp = re.match("(\w+)(?:\:(\w+))?", db)
    if not grp:
        parser.error("Cannot parse database list. Error on '%s'." % db)
    db_entry = grp.groups()
    db_list.append(db_entry)

try:
    # record start time
    if opt.verbosity >= 3:
        start_test = time.time()
    dbcopy.copy_db(source_values, dest_values, db_list, options)
    if opt.verbosity >= 3:
        print_elapsed_time(start_test)
except UtilError, e:
    print "ERROR:", e.errmsg
    exit(1)

exit()
Beispiel #2
0
                    'db_list': [db],
                    'options': options
                }
                copy_db_tasks.append(copy_task)

            # Create process pool.
            workers_pool = multiprocessing.Pool(
                processes=options['multiprocess']
            )

            # Concurrently copy databases.
            workers_pool.map_async(dbcopy.multiprocess_db_copy_task,
                                   copy_db_tasks)
            workers_pool.close()
            workers_pool.join()
        else:
            # Copy all specified databases (no database level concurrency).
            # Note: on POSIX systems multiprocessing is applied at the object
            # level (not database).
            dbcopy.copy_db(source_values, dest_values, db_list, options)

        # Print elapsed time.
        if opt.verbosity >= 3:
            print_elapsed_time(start_copy_time)
    except UtilError:
        _, err, _ = sys.exc_info()
        print("ERROR: {0}".format(err.errmsg))
        sys.exit(1)

    sys.exit()
 def migrate_up(self, db_name, last_version):
     (ver, md5, comment) = self.read_meta(db_name, int(last_version))
     if self.verify_checksum(db_name, last_version, md5) is False:
         logging.warning(
             "The current schema doesn't match the last applied migration")
     version = self.new_migration_version(db_name)
     if not os.path.exists("%s/%04d-up.mig" % (db_name, int(version))):
         logging.info("No migration available")
     else:
         logging.info(u"Preparing migration to version %04d" % int(version))
         if os.path.exists("%s/%04d-down.mig" % (db_name, int(version))):
             os.remove("%s/%04d-down.mig" % (db_name, int(version)))
         (ver, md5, comment) = self.read_meta(db_name, int(version))
         query_options = {'skip_data': True, 'force': True}
         db_list = []
         grp = re.match("(\w+)(?:\:(\w+))?",
                        "%s:%s_%s" % (db_name, self.tmp_prefix, db_name))
         db_entry = grp.groups()
         db_list.append(db_entry)
         source_values = parse_connection(server_connection)
         destination_values = parse_connection(server_connection)
         with capture() as stepback:
             dbcopy.copy_db(source_values, destination_values, db_list,
                            query_options)
         self.online_schema_change(
             db_name, version, "%s/%04d-up.mig" % (db_name, int(version)))
         if self.verify_checksum(db_name, version, md5) is True:
             logging.info("Applied changes match the requested schema")
         else:
             logging.error(
                 "Something didn't run as expected, db schema doesn't match !"
             )
             self.change_migration_status(db_name, version,
                                          'invalid checksum')
         query_options = {
             'run_all_tests': True,
             'reverse': True,
             'verbosity': None,
             'no_object_check': False,
             'no_data': True,
             'quiet': True,
             'difftype': 'sql',
             'width': 75,
             'changes-for': 'server1',
             'skip_grants': True,
             'skip_gtid': True
         }
         with capture() as stepback:
             res = dbcompare.database_compare(
                 source_values, destination_values, db_name,
                 "%s_%s" % (self.tmp_prefix, db_name), query_options)
         str = stepback.getvalue().splitlines(True)
         to_add = 0
         file_down = open("%s/%04d-down.tmp" % (db_name, int(version)), 'a')
         for line in str:
             if line[0] not in ['#', '\n', '+', '-', '@']:
                 # this if is required currently due to missing foreign keys in dbcopy
                 if not re.match("\s+DROP FOREIGN KEY", line):
                     #line = re.sub(" %s\." % db_name, " ", line, 1, re.IGNORECASE)
                     line = re.sub(" %s\." % db_name + '(?i)', " ", line, 1)
                     file_down.write("%s\n" % line.strip())
             elif re.match("# WARNING: Objects in", line):
                 if re.match("# WARNING: Objects in \w+\.tmp_online_mig_",
                             line):
                     to_add = 2
                 else:
                     to_add = 1
             else:
                 grp = re.match("#\s+TABLE\: (\w+)", line)
                 if grp:
                     if to_add == 2:
                         query = "SHOW CREATE TABLE tmp_online_mig_%s.%s;" % (
                             db_name, grp.group(1))
                         res = self.server.exec_query(query)
                         file_down.write("%s\n" % res[0][1])
                     elif to_add == 1:
                         file_down.write("DROP TABLE %s;\n" % grp.group(1))
         file_down.close()
         file_down_tmp = "%s/%04d-down.tmp" % (db_name, int(version))
         self.create_migration_file(db_name, file_down_tmp, version, "down")
         query = "DROP DATABASE %s_%s" % (self.tmp_prefix, db_name)
         res = self.server.exec_query(query)
         #os.remove(file_down_tmp)
         file_schema = "%s/%04d-schema.img" % (db_name, int(version))
         self.create_schema_img(db_name, file_schema)
 def migrate_up(self, db_name, last_version):
     (ver, md5, comment) = self.read_meta(db_name, int(last_version))
     if self.verify_checksum(db_name, last_version, md5) is False:
         logging.warning("The current schema doesn't match the last applied migration")
     version = self.new_migration_version(db_name)
     if not os.path.exists("%s/%04d-up.mig" % (db_name, int(version))):
         logging.info("No migration available")
     else:
         logging.info(u"Preparing migration to version %04d" % int(version))
         if os.path.exists("%s/%04d-down.mig" % (db_name, int(version))):
             os.remove("%s/%04d-down.mig" % (db_name, int(version)))
         (ver, md5, comment) = self.read_meta(db_name, int(version))
         query_options = {'skip_data': True, 'force': True}
         db_list = []
         grp = re.match("(\w+)(?:\:(\w+))?", "%s:%s_%s" % (db_name, self.tmp_prefix, db_name))
         db_entry = grp.groups()
         db_list.append(db_entry)
         source_values = parse_connection(server_connection)
         destination_values = parse_connection(server_connection)
         with capture() as stepback:
             dbcopy.copy_db(source_values, destination_values, db_list, query_options)
         self.online_schema_change(db_name, version, "%s/%04d-up.mig" % (db_name, int(version)))
         if self.verify_checksum(db_name, version, md5) is True:
             logging.info("Applied changes match the requested schema")
         else:
             logging.error("Something didn't run as expected, db schema doesn't match !")
             self.change_migration_status(db_name, version, 'invalid checksum')
         query_options = {
             'run_all_tests': True, 'reverse': True, 'verbosity': None,
             'no_object_check': False, 'no_data': True, 'quiet': True,
             'difftype': 'sql', 'width': 75, 'changes-for': 'server1',
             'skip_grants': True}
         with capture() as stepback:
             res = dbcompare.database_compare(source_values, destination_values, db_name, "%s_%s" % (self.tmp_prefix, db_name), query_options)
         str = stepback.getvalue().splitlines(True)
         to_add = 0
         file_down = open("%s/%04d-down.tmp" % (db_name, int(version)), 'a')
         for line in str:
             if line[0] not in ['#', '\n', '+', '-', '@']:
                 # this if is required currently due to missing foreign keys in dbcopy
                 if not re.match("\s+DROP FOREIGN KEY", line):
                     #line = re.sub(" %s\." % db_name, " ", line, 1, re.IGNORECASE)
                     line = re.sub(" %s\." % db_name + '(?i)', " ", line, 1)
                     file_down.write("%s\n" % line.strip())
             elif re.match("# WARNING: Objects in", line):
                 if re.match("# WARNING: Objects in \w+\.tmp_online_mig_", line):
                     to_add = 2
                 else:
                     to_add = 1
             else:
                 grp = re.match("#\s+TABLE\: (\w+)", line)
                 if grp:
                     if to_add == 2:
                         query = "SHOW CREATE TABLE tmp_online_mig_%s.%s;" % (db_name, grp.group(1))
                         res = self.server.exec_query(query)
                         file_down.write("%s\n" % res[0][1])
                     elif to_add == 1:
                         file_down.write("DROP TABLE %s;\n" % grp.group(1))
         file_down.close()
         file_down_tmp = "%s/%04d-down.tmp" % (db_name, int(version))
         self.create_migration_file(db_name, file_down_tmp, version, "down")
         query = "DROP DATABASE %s_%s" % (self.tmp_prefix, db_name)
         res = self.server.exec_query(query)
         #os.remove(file_down_tmp)
         file_schema = "%s/%04d-schema.img" % (db_name, int(version))
         self.create_schema_img(db_name, file_schema)
    "user"   : conn.get("user"),
    "passwd" : "root",
    "host"   : conn.get("host"),
    "port"   : opt.new_port,
    "unix_socket" : os.path.join(opt.new_data, "mysql.sock")
}

# Build dictionary of options
options = {
    "quiet" : True,
    "force" : True
}

print "# Copying databases..."
try:
    dbcopy.copy_db(conn, dest_values, db_list, options)
except exception.UtilError, e:
    print "ERROR:", e.errmsg
    exit(1)

# Build dictionary of options
options = {
    "overwrite" : True,
    "quiet"     : True,
    "globals"   : True
}

print "# Cloning the users..."
for user in user_list:
    try:
        res = userclone.clone_user(conn, dest_values, user,
Beispiel #6
0
# Set connection values
dest_values = {
    "user": conn.get("user"),
    "passwd": "root",
    "host": conn.get("host"),
    "port": opt.new_port,
    "unix_socket": os.path.join(opt.new_data, "mysql.sock")
}

# Build dictionary of options
options = {"quiet": True, "force": True}

print "# Copying databases..."
try:
    dbcopy.copy_db(conn, dest_values, db_list, options)
except exception.UtilError, e:
    print "ERROR:", e.errmsg
    exit(1)

# Build dictionary of options
options = {"overwrite": True, "quiet": True, "globals": True}

print "# Cloning the users..."
for user in user_list:
    try:
        res = userclone.clone_user(conn, dest_values, user, (user, ), options)
    except exception.UtilError, e:
        print "ERROR:", e.errmsg
        exit(1)