From 65472c8de27e59a54e22f24f48fed0bf5d517cd7 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Tue, 26 May 2020 08:59:04 -0700 Subject: [PATCH] Fix error message when there are no MySQL databases to dump for "all" databases (#319). --- NEWS | 1 + borgmatic/hooks/mysql.py | 6 ++++-- tests/unit/hooks/test_mysql.py | 13 +++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 79b225bd0..8f61e4283 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,7 @@ formats, the "directory" dump format does not stream directly to/from Borg. * #316: Fix hang when streaming a database dump to Borg with implicit duplicate source directories by deduplicating them first. + * #319: Fix error message when there are no MySQL databases to dump for "all" databases. * Improve documentation around the installation process. Specifically, making borgmatic commands runnable via the system PATH and offering a global install option. diff --git a/borgmatic/hooks/mysql.py b/borgmatic/hooks/mysql.py index 2c7c1f0f3..d3599e647 100644 --- a/borgmatic/hooks/mysql.py +++ b/borgmatic/hooks/mysql.py @@ -73,9 +73,11 @@ def dump_databases(databases, log_prefix, location_config, dry_run): make_dump_path(location_config), requested_name, database.get('hostname') ) extra_environment = {'MYSQL_PWD': database['password']} if 'password' in database else None - dump_command_names = database_names_to_dump( + dump_database_names = database_names_to_dump( database, extra_environment, log_prefix, dry_run_label ) + if not dump_database_names: + raise ValueError('Cannot find any MySQL databases to dump.') dump_command = ( ('mysqldump',) @@ -86,7 +88,7 @@ def dump_databases(databases, log_prefix, location_config, dry_run): + (('--user', database['username']) if 'username' in database else ()) + (tuple(database['options'].split(' ')) if 'options' in database else ()) + ('--databases',) - + dump_command_names + + dump_database_names # Use shell redirection rather than execute_command(output_file=open(...)) to prevent # the open() call on a named pipe from hanging the main borgmatic process. + ('>', dump_filename) diff --git a/tests/unit/hooks/test_mysql.py b/tests/unit/hooks/test_mysql.py index 6d983c0a0..8e457a554 100644 --- a/tests/unit/hooks/test_mysql.py +++ b/tests/unit/hooks/test_mysql.py @@ -198,6 +198,19 @@ def test_dump_databases_runs_mysqldump_for_all_databases(): assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process] +def test_dump_databases_errors_for_missing_all_databases(): + databases = [{'name': 'all'}] + process = flexmock() + flexmock(module).should_receive('make_dump_path').and_return('') + flexmock(module.dump).should_receive('make_database_dump_filename').and_return( + 'databases/localhost/all' + ) + flexmock(module).should_receive('database_names_to_dump').and_return(()) + + with pytest.raises(ValueError): + assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process] + + def test_restore_database_dump_runs_mysql_to_restore(): database_config = [{'name': 'foo'}] extract_process = flexmock(stdout=flexmock())