diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index 7d81c498..51278bf4 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -1087,6 +1087,21 @@ properties: Password with which to connect to the restore database. Defaults to the "password" option. example: trustsome1 + mysql_dump_command: + type: string + description: | + Command to use instead of "mysqldump". This can be used + to run a specific mysql_dump version (e.g., one inside + a running docker container). Defaults to "mysqldump". + example: docker exec mysql_container mysqldump + mysql_command: + type: string + description: | + Command to run instead of "mysql". This + can be used to run a specific mysql + version (e.g., one inside a running docker + container). Defaults to "mysql". + example: docker exec mysql_container mysql format: type: string enum: ['sql'] diff --git a/borgmatic/hooks/mysql.py b/borgmatic/hooks/mysql.py index 8f1b83b7..086e7987 100644 --- a/borgmatic/hooks/mysql.py +++ b/borgmatic/hooks/mysql.py @@ -79,8 +79,9 @@ def execute_dump_command( ) return None + mysql_dump_command = database.get('mysql_dump_command') or 'mysqldump' dump_command = ( - ('mysqldump',) + (mysql_dump_command,) + (tuple(database['options'].split(' ')) if 'options' in database else ()) + (('--add-drop-database',) if database.get('add_drop_database', True) else ()) + (('--host', database['hostname']) if 'hostname' in database else ()) @@ -206,9 +207,9 @@ def restore_data_source_dump( password = connection_params['password'] or data_source.get( 'restore_password', data_source.get('password') ) - + mysql_restore_command = data_source.get('mysql_command') or 'mysql' restore_command = ( - ('mysql', '--batch') + (mysql_restore_command, '--batch') + ( tuple(data_source['restore_options'].split(' ')) if 'restore_options' in data_source diff --git a/tests/unit/hooks/test_mysql.py b/tests/unit/hooks/test_mysql.py index 6f778b30..46d17777 100644 --- a/tests/unit/hooks/test_mysql.py +++ b/tests/unit/hooks/test_mysql.py @@ -315,6 +315,42 @@ def test_execute_dump_command_runs_mysqldump_with_options(): ) +def test_execute_dump_command_runs_non_default_mysqldump(): + process = flexmock() + flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump') + flexmock(module.os.path).should_receive('exists').and_return(False) + flexmock(module.dump).should_receive('create_named_pipe_for_dump') + + flexmock(module).should_receive('execute_command').with_args( + ( + 'custom_mysqldump', # Custom MySQL dump command + '--add-drop-database', + '--databases', + 'foo', + '--result-file', + 'dump', + ), + extra_environment=None, + run_to_completion=False, + ).and_return(process).once() + + assert ( + module.execute_dump_command( + database={ + 'name': 'foo', + 'mysql_dump_command': 'custom_mysqldump', + }, # Custom MySQL dump command specified + log_prefix='log', + dump_path=flexmock(), + database_names=('foo',), + extra_environment=None, + dry_run=False, + dry_run_label='', + ) + == process + ) + + def test_execute_dump_command_with_duplicate_dump_skips_mysqldump(): flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump') flexmock(module.os.path).should_receive('exists').and_return(True) @@ -435,6 +471,34 @@ def test_restore_data_source_dump_runs_mysql_with_options(): ) +def test_restore_data_source_dump_runs_non_default_mysql_with_options(): + hook_config = [{'name': 'foo', 'mysql_command': 'custom_mysql', 'restore_options': '--harder'}] + extract_process = flexmock(stdout=flexmock()) + + flexmock(module).should_receive('execute_command_with_processes').with_args( + ('custom_mysql', '--batch', '--harder'), + processes=[extract_process], + output_log_level=logging.DEBUG, + input_file=extract_process.stdout, + extra_environment=None, + ).once() + + module.restore_data_source_dump( + hook_config, + {}, + 'test.yaml', + data_source=hook_config[0], + dry_run=False, + extract_process=extract_process, + connection_params={ + 'hostname': None, + 'port': None, + 'username': None, + 'password': None, + }, + ) + + def test_restore_data_source_dump_runs_mysql_with_hostname_and_port(): hook_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}] extract_process = flexmock(stdout=flexmock())