diff --git a/phpmyadmin_sql_backup.py b/phpmyadmin_sql_backup.py index fc99b25..df90e63 100644 --- a/phpmyadmin_sql_backup.py +++ b/phpmyadmin_sql_backup.py @@ -48,10 +48,13 @@ def open_frame_if_phpmyadmin_3(g): def download_sql_backup(url, user, password, dry_run=False, overwrite_existing=False, prepend_date=True, basename=None, - output_directory=os.getcwd(), exclude_dbs=None, compression='none', prefix_format=None, + output_directory=os.getcwd(), exclude_dbs=None, include_dbs=None, compression='none', prefix_format=None, timeout=60, http_auth=None, server_name=None, **kwargs): prefix_format = prefix_format or DEFAULT_PREFIX_FORMAT + if exclude_dbs and include_dbs: + raise ValueError('can\'t specify both exclude_dbs and include_dbs') exclude_dbs = exclude_dbs.split(',') or [] + include_dbs = include_dbs.split(',') or [] encoding = '' if compression == 'gzip' else 'gzip' g = grab.Grab(encoding=encoding, timeout=timeout) @@ -74,7 +77,11 @@ def download_sql_backup(url, user, password, dry_run=False, overwrite_existing=F g.go(export_url) dbs_available = [option.attrib['value'] for option in g.doc.form.inputs['db_select[]']] - dbs_to_dump = [db_name for db_name in dbs_available if db_name not in exclude_dbs] + if include_dbs: + dbs_to_dump = [db_name for db_name in dbs_available if db_name in include_dbs] + else: + dbs_to_dump = [db_name for db_name in dbs_available if db_name not in exclude_dbs] + print('databases to dump:', dbs_to_dump) if not dbs_to_dump: print('Warning: no databases to dump (databases available: "{}")'.format('", "'.join(dbs_available)), file=sys.stderr) @@ -126,6 +133,8 @@ def download_sql_backup(url, user, password, dry_run=False, overwrite_existing=F 'see the --prefix-format option for custom formatting') parser.add_argument('-e', '--exclude-dbs', default='', help='comma-separated list of database names to exclude from the dump') + parser.add_argument('--include-dbs', default='', + help='comma-separated list of database names to include from the dump. Else, all databases are included') parser.add_argument('-s', '--server-name', default=None, help='mysql server hostname to supply if enabled as field on login page') parser.add_argument('--compression', default='none', choices=['none', 'zip', 'gzip'],