Skip to content

ability to include (and not only exclude) databases #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions phpmyadmin_sql_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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'],
Expand Down