def ReloadTables(dbspec, num_writers, dump_dir, mysql_bin_dir, wipe_innodb):
  todo = GetTableNamesFromDumpFiles(dump_dir)

  if wipe_innodb:
    logging.info("Dropping innodb tables...")
    for db, table in todo:
      logging.debug('Drop %s.%s' % (db, table))
      dbspec.execute("DROP TABLE %s.%s" % (db, table))

    logging.info("Stopping mysql...")
    dbspec.stopMySql()

    logging.info("Deleting innodb data files...")
    retcode = subprocess.call("sudo rm -f %s/innodb_data*; "
                              "sudo rm -f %s/innodb_logs/*"
                              % (FLAGS.mysql_root, FLAGS.mysql_root),
                              shell=True)

    if retcode != 0:
      logging.fatal("Error deleting innodb datafiles")

    logging.info("Starting mysql...")
    dbspec.startMySql()
    time.sleep(5)

  logging.info("Restoring tables...")
  pool = command_pool.CommandPool(num_writers)
  if len(mysql_bin_dir):
    cmdpath = "%s/mysql" % mysql_bin_dir
  else:
    cmdpath = "mysql"

  for db, table in todo:
    cmd = (("zcat %s/%s-%s.sql.gz | %s -u%s -p%s -h%s -A %s") %
           (dump_dir, db, table, cmdpath,
            dbspec.user, dbspec.password, dbspec.host, db,
            ))
    logging.debug('Submit %s' % cmd)
    pool.submit(cmd, (db, table))

  if not pool.run():
    for failure in pool.failures:
      logging.error("Restore of %s.%s failed: %s" %
                    (failure.data[0], failure.data[1], failure.returncode))
      logging.error("Output: %s\n" % failure.output.read())
    logging.fatal("One or more restores failed; aborting.")
def GetTableNamesFromDumpFiles(dump_dir):
  logging.info("Get table names from dump files in %s" % dump_dir)
  dump_files = glob.glob(os.path.join(dump_dir, '*.sql.gz'))
  todo = []
  for pathname in dump_files:
    filename = pathname[len(dump_dir) + 1:]
    logging.debug('Found filename %s' % filename)

    match_object = DUMP_RE.match(filename)
    if not match_object or len(match_object.groups()) != 2:
      logging.fatal('Match failed for %s' % filename)

    db = match_object.groups()[0]
    table = match_object.groups()[1]
    logging.info('Found %s.%s' % (db, table))
    todo.append((db, table))
  return todo