Commit 784e9391 authored by Hartmut Holzgraefe's avatar Hartmut Holzgraefe Committed by Sergey Vojtovich

MDEV-13336 Add --ignore-database option to mysqldump

Allow to exclude certain databases from an --all-databases dump,
e.g. to be able to do

  mysqldump --all-databases --ignore-database=mysql

to dump everything but the system tables.
parent f0e4f94c
...@@ -61,6 +61,7 @@ enum options_client ...@@ -61,6 +61,7 @@ enum options_client
OPT_USE_THREADS, OPT_USE_THREADS,
OPT_IMPORT_USE_THREADS, OPT_IMPORT_USE_THREADS,
OPT_MYSQL_NUMBER_OF_QUERY, OPT_MYSQL_NUMBER_OF_QUERY,
OPT_IGNORE_DATABASE,
OPT_IGNORE_TABLE,OPT_INSERT_IGNORE,OPT_SHOW_WARNINGS,OPT_DROP_DATABASE, OPT_IGNORE_TABLE,OPT_INSERT_IGNORE,OPT_SHOW_WARNINGS,OPT_DROP_DATABASE,
OPT_TZ_UTC, OPT_CREATE_SLAP_SCHEMA, OPT_TZ_UTC, OPT_CREATE_SLAP_SCHEMA,
OPT_MYSQLDUMP_SLAVE_APPLY, OPT_MYSQLDUMP_SLAVE_APPLY,
......
...@@ -214,6 +214,8 @@ TYPELIB compatible_mode_typelib= {array_elements(compatible_mode_names) - 1, ...@@ -214,6 +214,8 @@ TYPELIB compatible_mode_typelib= {array_elements(compatible_mode_names) - 1,
HASH ignore_table; HASH ignore_table;
HASH ignore_database;
static struct my_option my_long_options[] = static struct my_option my_long_options[] =
{ {
{"all-databases", 'A', {"all-databases", 'A',
...@@ -376,6 +378,11 @@ static struct my_option my_long_options[] = ...@@ -376,6 +378,11 @@ static struct my_option my_long_options[] =
&opt_hex_blob, &opt_hex_blob, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, &opt_hex_blob, &opt_hex_blob, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"host", 'h', "Connect to host.", &current_host, {"host", 'h', "Connect to host.", &current_host,
&current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"ignore-database", OPT_IGNORE_DATABASE,
"Do not dump the specified database. To specify more than one database to ignore, "
"use the directive multiple times, once for each database. Only takes effect "
"when used together with --all-databases|-A",
0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"ignore-table", OPT_IGNORE_TABLE, {"ignore-table", OPT_IGNORE_TABLE,
"Do not dump the specified table. To specify more than one table to ignore, " "Do not dump the specified table. To specify more than one table to ignore, "
"use the directive multiple times, once for each table. Each table must " "use the directive multiple times, once for each table. Each table must "
...@@ -900,6 +907,10 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)), ...@@ -900,6 +907,10 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
case (int) OPT_TABLES: case (int) OPT_TABLES:
opt_databases=0; opt_databases=0;
break; break;
case (int) OPT_IGNORE_DATABASE:
if (my_hash_insert(&ignore_database, (uchar*)my_strdup(argument, MYF(0))))
exit(EX_EOM);
break;
case (int) OPT_IGNORE_TABLE: case (int) OPT_IGNORE_TABLE:
{ {
if (!strchr(argument, '.')) if (!strchr(argument, '.'))
...@@ -984,6 +995,9 @@ static int get_options(int *argc, char ***argv) ...@@ -984,6 +995,9 @@ static int get_options(int *argc, char ***argv)
load_defaults_or_exit("my", load_default_groups, argc, argv); load_defaults_or_exit("my", load_default_groups, argc, argv);
defaults_argv= *argv; defaults_argv= *argv;
if (my_hash_init(&ignore_database, charset_info, 16, 0, 0,
(my_hash_get_key) get_table_key, my_free, 0))
return(EX_EOM);
if (my_hash_init(&ignore_table, charset_info, 16, 0, 0, if (my_hash_init(&ignore_table, charset_info, 16, 0, 0,
(my_hash_get_key) get_table_key, my_free, 0)) (my_hash_get_key) get_table_key, my_free, 0))
return(EX_EOM); return(EX_EOM);
...@@ -1056,6 +1070,12 @@ static int get_options(int *argc, char ***argv) ...@@ -1056,6 +1070,12 @@ static int get_options(int *argc, char ***argv)
my_progname_short); my_progname_short);
return(EX_USAGE); return(EX_USAGE);
} }
if (ignore_database.records && !opt_alldbs) {
fprintf(stderr,
"%s: --ignore-database can only be used toghether with --all-databases.\n",
my_progname_short);
return(EX_USAGE);
}
if (strcmp(default_charset, charset_info->csname) && if (strcmp(default_charset, charset_info->csname) &&
!(charset_info= get_charset_by_csname(default_charset, !(charset_info= get_charset_by_csname(default_charset,
MY_CS_PRIMARY, MYF(MY_WME)))) MY_CS_PRIMARY, MYF(MY_WME))))
...@@ -1642,6 +1662,8 @@ static void free_resources() ...@@ -1642,6 +1662,8 @@ static void free_resources()
my_free(opt_password); my_free(opt_password);
my_free(current_host); my_free(current_host);
free_root(&glob_root, MYF(0)); free_root(&glob_root, MYF(0));
if (my_hash_inited(&ignore_database))
my_hash_free(&ignore_database);
if (my_hash_inited(&ignore_table)) if (my_hash_inited(&ignore_table))
my_hash_free(&ignore_table); my_hash_free(&ignore_table);
dynstr_free(&extended_row); dynstr_free(&extended_row);
...@@ -4429,6 +4451,14 @@ static int dump_tablespaces(char* ts_where) ...@@ -4429,6 +4451,14 @@ static int dump_tablespaces(char* ts_where)
DBUG_RETURN(0); DBUG_RETURN(0);
} }
/* Return 1 if we should copy the database */
my_bool include_database(const uchar *hash_key, size_t len)
{
return ! my_hash_search(&ignore_database, hash_key, len);
}
static int dump_all_databases() static int dump_all_databases()
{ {
MYSQL_ROW row; MYSQL_ROW row;
...@@ -4447,8 +4477,9 @@ static int dump_all_databases() ...@@ -4447,8 +4477,9 @@ static int dump_all_databases()
!my_strcasecmp(&my_charset_latin1, row[0], PERFORMANCE_SCHEMA_DB_NAME)) !my_strcasecmp(&my_charset_latin1, row[0], PERFORMANCE_SCHEMA_DB_NAME))
continue; continue;
if (dump_all_tables_in_db(row[0])) if (include_database(row[0], strlen(row[0])))
result=1; if (dump_all_tables_in_db(row[0]))
result=1;
} }
mysql_free_result(tableres); mysql_free_result(tableres);
if (seen_views) if (seen_views)
...@@ -4470,8 +4501,9 @@ static int dump_all_databases() ...@@ -4470,8 +4501,9 @@ static int dump_all_databases()
!my_strcasecmp(&my_charset_latin1, row[0], PERFORMANCE_SCHEMA_DB_NAME)) !my_strcasecmp(&my_charset_latin1, row[0], PERFORMANCE_SCHEMA_DB_NAME))
continue; continue;
if (dump_all_views_in_db(row[0])) if (include_database(row[0], strlen(row[0])))
result=1; if (dump_all_views_in_db(row[0]))
result=1;
} }
mysql_free_result(tableres); mysql_free_result(tableres);
} }
...@@ -6055,6 +6087,7 @@ int main(int argc, char **argv) ...@@ -6055,6 +6087,7 @@ int main(int argc, char **argv)
sf_leaking_memory=1; /* don't report memory leaks on early exits */ sf_leaking_memory=1; /* don't report memory leaks on early exits */
compatible_mode_normal_str[0]= 0; compatible_mode_normal_str[0]= 0;
default_charset= (char *)mysql_universal_client_charset; default_charset= (char *)mysql_universal_client_charset;
bzero((char*) &ignore_database, sizeof(ignore_database));
bzero((char*) &ignore_table, sizeof(ignore_table)); bzero((char*) &ignore_table, sizeof(ignore_table));
exit_code= get_options(&argc, &argv); exit_code= get_options(&argc, &argv);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment