diff --git a/NEWS b/NEWS index 4fd9cdeb..c0a22967 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,8 @@ command-line flags for database commands. * #1193: For the MariaDB and MySQL database hooks, add a "socket_path" option for Unix socket database connections. + * #1193: For the MariaDB and MySQL database hooks, creates a consistent snapshot by dumping all + tables in a single transaction. * #1194: Fix for an incorrect diff command shown when running the "generate config" action with a source configuration file. * #1195: Fix a regression in the ZFS, LVM, and Btrfs hooks in which snapshotted paths ignored diff --git a/borgmatic/hooks/data_source/mariadb.py b/borgmatic/hooks/data_source/mariadb.py index b0c0d0cf..625acb6d 100644 --- a/borgmatic/hooks/data_source/mariadb.py +++ b/borgmatic/hooks/data_source/mariadb.py @@ -227,6 +227,7 @@ def execute_dump_command( ) + extra_options + (('--add-drop-database',) if database.get('add_drop_database', True) else ()) + + ('--single-transaction',) + (('--host', hostname) if hostname else ()) + (('--port', str(database['port'])) if 'port' in database else ()) + (('--protocol', 'tcp') if hostname or 'port' in database else ()) diff --git a/borgmatic/hooks/data_source/mysql.py b/borgmatic/hooks/data_source/mysql.py index eaefecc5..a7932495 100644 --- a/borgmatic/hooks/data_source/mysql.py +++ b/borgmatic/hooks/data_source/mysql.py @@ -158,6 +158,7 @@ def execute_dump_command( ) + extra_options + (('--add-drop-database',) if database.get('add_drop_database', True) else ()) + + ('--single-transaction',) + (('--host', hostname) if hostname else ()) + (('--port', str(database['port'])) if 'port' in database else ()) + (('--protocol', 'tcp') if hostname or 'port' in database else ()) diff --git a/docs/how-to/backup-your-databases.md b/docs/how-to/backup-your-databases.md index 762cebf6..1fc1523e 100644 --- a/docs/how-to/backup-your-databases.md +++ b/docs/how-to/backup-your-databases.md @@ -659,9 +659,14 @@ configuration. Here's an example with MariaDB: ```yaml mariadb_databases: - name: posts - options: "--single-transaction --quick" + options: "--single-transaction" ``` +New in version 2.0.13 borgmatic +passes `--single-transaction` to MariaDB/MySQL by default, and you no longer +need to set that in `options:`. + + ### borgmatic hangs during backup See Limitations above about `read_special`. You may need to exclude certain diff --git a/tests/unit/hooks/data_source/test_mariadb.py b/tests/unit/hooks/data_source/test_mariadb.py index ec7daf52..d53b6f81 100644 --- a/tests/unit/hooks/data_source/test_mariadb.py +++ b/tests/unit/hooks/data_source/test_mariadb.py @@ -837,6 +837,7 @@ def test_execute_dump_command_runs_mariadb_dump(): 'mariadb-dump', '--defaults-extra-file=/dev/fd/99', '--add-drop-database', + '--single-transaction', '--databases', 'foo', '--result-file', @@ -880,6 +881,7 @@ def test_execute_dump_command_with_environment_password_transport_skips_defaults ( 'mariadb-dump', '--add-drop-database', + '--single-transaction', '--user', 'root', '--databases', @@ -929,6 +931,7 @@ def test_execute_dump_command_runs_mariadb_dump_without_add_drop_database(): ( 'mariadb-dump', '--defaults-extra-file=/dev/fd/99', + '--single-transaction', '--databases', 'foo', '--result-file', @@ -979,6 +982,7 @@ def test_execute_dump_command_runs_mariadb_dump_with_hostname_and_port(): 'mariadb-dump', '--defaults-extra-file=/dev/fd/99', '--add-drop-database', + '--single-transaction', '--host', 'database.example.org', '--port', @@ -1033,6 +1037,7 @@ def test_execute_dump_command_runs_mariadb_dump_with_tls(): 'mariadb-dump', '--defaults-extra-file=/dev/fd/99', '--add-drop-database', + '--single-transaction', '--ssl', '--databases', 'foo', @@ -1082,6 +1087,7 @@ def test_execute_dump_command_runs_mariadb_dump_without_tls(): 'mariadb-dump', '--defaults-extra-file=/dev/fd/99', '--add-drop-database', + '--single-transaction', '--skip-ssl', '--databases', 'foo', @@ -1131,6 +1137,7 @@ def test_execute_dump_command_runs_mariadb_dump_with_username_and_password(): 'mariadb-dump', '--defaults-extra-file=/dev/fd/99', '--add-drop-database', + '--single-transaction', '--databases', 'foo', '--result-file', @@ -1180,6 +1187,7 @@ def test_execute_dump_command_runs_mariadb_dump_with_options(): '--defaults-extra-file=/dev/fd/99', '--stuff=such', '--add-drop-database', + '--single-transaction', '--databases', 'foo', '--result-file', @@ -1229,6 +1237,7 @@ def test_execute_dump_command_runs_non_default_mariadb_dump_with_options(): '--defaults-extra-file=/dev/fd/99', '--stuff=such', '--add-drop-database', + '--single-transaction', '--databases', 'foo', '--result-file', diff --git a/tests/unit/hooks/data_source/test_mysql.py b/tests/unit/hooks/data_source/test_mysql.py index e4ff0670..213efcea 100644 --- a/tests/unit/hooks/data_source/test_mysql.py +++ b/tests/unit/hooks/data_source/test_mysql.py @@ -717,6 +717,7 @@ def test_execute_dump_command_runs_mysqldump(): 'mysqldump', '--defaults-extra-file=/dev/fd/99', '--add-drop-database', + '--single-transaction', '--databases', 'foo', '--result-file', @@ -764,6 +765,7 @@ def test_execute_dump_command_with_environment_password_transport_skips_defaults ( 'mysqldump', '--add-drop-database', + '--single-transaction', '--user', 'root', '--databases', @@ -813,6 +815,7 @@ def test_execute_dump_command_runs_mysqldump_without_add_drop_database(): ( 'mysqldump', '--defaults-extra-file=/dev/fd/99', + '--single-transaction', '--databases', 'foo', '--result-file', @@ -863,6 +866,7 @@ def test_execute_dump_command_runs_mysqldump_with_hostname_and_port(): 'mysqldump', '--defaults-extra-file=/dev/fd/99', '--add-drop-database', + '--single-transaction', '--host', 'database.example.org', '--port', @@ -917,6 +921,7 @@ def test_execute_dump_command_runs_mysqldump_with_tls(): 'mysqldump', '--defaults-extra-file=/dev/fd/99', '--add-drop-database', + '--single-transaction', '--ssl', '--databases', 'foo', @@ -966,6 +971,7 @@ def test_execute_dump_command_runs_mysqldump_without_tls(): 'mysqldump', '--defaults-extra-file=/dev/fd/99', '--add-drop-database', + '--single-transaction', '--skip-ssl', '--databases', 'foo', @@ -1015,6 +1021,7 @@ def test_execute_dump_command_runs_mysqldump_with_username_and_password(): 'mysqldump', '--defaults-extra-file=/dev/fd/99', '--add-drop-database', + '--single-transaction', '--databases', 'foo', '--result-file', @@ -1064,6 +1071,7 @@ def test_execute_dump_command_runs_mysqldump_with_options(): '--defaults-extra-file=/dev/fd/99', '--stuff=such', '--add-drop-database', + '--single-transaction', '--databases', 'foo', '--result-file', @@ -1112,6 +1120,7 @@ def test_execute_dump_command_runs_non_default_mysqldump(): 'custom_mysqldump', # Custom MySQL dump command '--defaults-extra-file=/dev/fd/99', '--add-drop-database', + '--single-transaction', '--databases', 'foo', '--result-file',