From 6945bd8f254648e37874c3c4f000b49f13d31f33 Mon Sep 17 00:00:00 2001 From: David Gow Date: Fri, 6 Oct 2017 14:26:24 -0700 Subject: [PATCH] Add an --exclude-databases option to mysqldump This change adds a new option --exclude-databases, which allows one to list databases to be excluded from --all-databases. For example, to dump all databases except `mysql`: mysqldump --all-databases --exclude-databases mysql This re-uses the normal option-parsing logic, so the list of databases does not need to be adjacent to the --exclude-databases option: they parse the same way as the list of databases when the --databases option is used. This can be used, for example, to exclude the `mysql` database from dumps, which is useful when importing into environments (particularly managed environments) where that is not permitted, or where system table layouts are incompatible. --- client/mysqldump.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/client/mysqldump.c b/client/mysqldump.c index 09324daccd9..a7c6f48b60c 100644 --- a/client/mysqldump.c +++ b/client/mysqldump.c @@ -163,6 +163,8 @@ static char *shared_memory_base_name=0; static uint opt_protocol= 0; static char *opt_plugin_dir= 0, *opt_default_auth= 0; +static my_bool opt_exclude_databases= 0; + DYNAMIC_ARRAY ignore_error; static int parse_ignore_error(); @@ -327,6 +329,12 @@ static struct my_option my_long_options[] = {"events", 'E', "Dump events.", &opt_events, &opt_events, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, + {"exclude-databases", 0, + "Exclude the listed databases from a dump taken with --all-databases. " + "Note that all arguments passed are treated as database names to exclude, " + "not table names", + &opt_exclude_databases, &opt_exclude_databases, 0, GET_BOOL, NO_ARG, 0, 0, + 0, 0, 0, 0}, {"extended-insert", 'e', "Use multiple-row INSERT syntax that include several VALUES lists.", &extended_insert, &extended_insert, 0, GET_BOOL, NO_ARG, @@ -579,7 +587,7 @@ static int init_dumping_views(char *); static int init_dumping_tables(char *); static int init_dumping(char *, int init_func(char*)); static int dump_databases(char **); -static int dump_all_databases(); +static int dump_all_databases(char **); static char *quote_name(const char *name, char *buff, my_bool force); char check_if_ignore_table(const char *table_name, char *table_type); static char *primary_key_fields(const char *table_name); @@ -1080,11 +1088,19 @@ static int get_options(int *argc, char ***argv) my_progname); return(EX_USAGE); } + if (opt_exclude_databases && !opt_alldbs) + { + fprintf(stderr, + "%s: --exclude-databases can't be used without --all-databases\n", + my_progname); + return(EX_USAGE); + } if (strcmp(default_charset, charset_info->csname) && !(charset_info= get_charset_by_csname(default_charset, MY_CS_PRIMARY, MYF(MY_WME)))) exit(1); - if ((*argc < 1 && !opt_alldbs) || (*argc > 0 && opt_alldbs)) + if ((*argc < 1 && !opt_alldbs) || + (*argc > 0 && opt_alldbs && !opt_exclude_databases)) { short_usage(); return EX_USAGE; @@ -4149,6 +4165,19 @@ static char *getTableName(int reset) dump all logfile groups and tablespaces */ +static my_bool is_database_in_list(char *db, char **dblist) +{ + int i; + for (i = 0; dblist[i]; ++i) + { + if (!my_strcasecmp(charset_info, db, dblist[i])) + { + return 1; + } + } + return 0; +} + static int dump_all_tablespaces() { return dump_tablespaces(NULL); @@ -4439,7 +4468,7 @@ is_ndbinfo(MYSQL* mysql, const char* dbname) } -static int dump_all_databases() +static int dump_all_databases(char **excluded_databases) { MYSQL_ROW row; MYSQL_RES *tableres; @@ -4464,6 +4493,10 @@ static int dump_all_databases() if (is_ndbinfo(mysql, row[0])) continue; + if (opt_exclude_databases && + is_database_in_list(row[0], excluded_databases)) + continue; + if (dump_all_tables_in_db(row[0])) result=1; } @@ -4494,6 +4527,10 @@ static int dump_all_databases() if (is_ndbinfo(mysql, row[0])) continue; + if (opt_exclude_databases && + is_database_in_list(row[0], excluded_databases)) + continue; + if (dump_all_views_in_db(row[0])) result=1; } @@ -6131,7 +6168,7 @@ int main(int argc, char **argv) { if (!opt_alltspcs && !opt_notspcs) dump_all_tablespaces(); - dump_all_databases(); + dump_all_databases(argv); } else { -- 2.14.2.920.gcf0c67979c-goog