| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 | 
							- import json
 
- import os
 
- import shutil
 
- import subprocess
 
- import sys
 
- import tempfile
 
- import pytest
 
- def write_configuration(
 
-     source_directory,
 
-     config_path,
 
-     repository_path,
 
-     borgmatic_source_directory,
 
-     postgresql_dump_format='custom',
 
-     mongodb_dump_format='archive',
 
- ):
 
-     '''
 
-     Write out borgmatic configuration into a file at the config path. Set the options so as to work
 
-     for testing. This includes injecting the given repository path, borgmatic source directory for
 
-     storing database dumps, dump format (for PostgreSQL), and encryption passphrase.
 
-     '''
 
-     config = f'''
 
- source_directories:
 
-     - {source_directory}
 
- repositories:
 
-     - path: {repository_path}
 
- borgmatic_source_directory: {borgmatic_source_directory}
 
- encryption_passphrase: "test"
 
- postgresql_databases:
 
-     - name: test
 
-       hostname: postgresql
 
-       username: postgres
 
-       password: test
 
-       format: {postgresql_dump_format}
 
-     - name: all
 
-       hostname: postgresql
 
-       username: postgres
 
-       password: test
 
-     - name: all
 
-       format: custom
 
-       hostname: postgresql
 
-       username: postgres
 
-       password: test
 
- mariadb_databases:
 
-     - name: test
 
-       hostname: mariadb
 
-       username: root
 
-       password: test
 
-     - name: all
 
-       hostname: mariadb
 
-       username: root
 
-       password: test
 
-     - name: all
 
-       format: sql
 
-       hostname: mariadb
 
-       username: root
 
-       password: test
 
- mysql_databases:
 
-     - name: test
 
-       hostname: mariadb
 
-       username: root
 
-       password: test
 
-     - name: all
 
-       hostname: mariadb
 
-       username: root
 
-       password: test
 
-     - name: all
 
-       format: sql
 
-       hostname: mariadb
 
-       username: root
 
-       password: test
 
- mongodb_databases:
 
-     - name: test
 
-       hostname: mongodb
 
-       username: root
 
-       password: test
 
-       authentication_database: admin
 
-       format: {mongodb_dump_format}
 
-     - name: all
 
-       hostname: mongodb
 
-       username: root
 
-       password: test
 
- sqlite_databases:
 
-     - name: sqlite_test
 
-       path: /tmp/sqlite_test.db
 
- '''
 
-     with open(config_path, 'w') as config_file:
 
-         config_file.write(config)
 
- def write_custom_restore_configuration(
 
-     source_directory,
 
-     config_path,
 
-     repository_path,
 
-     borgmatic_source_directory,
 
-     postgresql_dump_format='custom',
 
-     mongodb_dump_format='archive',
 
- ):
 
-     '''
 
-     Write out borgmatic configuration into a file at the config path. Set the options so as to work
 
-     for testing with custom restore options. This includes a custom restore_hostname, restore_port,
 
-     restore_username, restore_password and restore_path.
 
-     '''
 
-     config = f'''
 
- source_directories:
 
-     - {source_directory}
 
- repositories:
 
-     - path: {repository_path}
 
- borgmatic_source_directory: {borgmatic_source_directory}
 
- encryption_passphrase: "test"
 
- postgresql_databases:
 
-     - name: test
 
-       hostname: postgresql
 
-       username: postgres
 
-       password: test
 
-       format: {postgresql_dump_format}
 
-       restore_hostname: postgresql2
 
-       restore_port: 5433
 
-       restore_username: postgres2
 
-       restore_password: test2
 
- mariadb_databases:
 
-     - name: test
 
-       hostname: mariadb
 
-       username: root
 
-       password: test
 
-       restore_hostname: mariadb2
 
-       restore_port: 3307
 
-       restore_username: root
 
-       restore_password: test2
 
- mysql_databases:
 
-     - name: test
 
-       hostname: mariadb
 
-       username: root
 
-       password: test
 
-       restore_hostname: mariadb2
 
-       restore_port: 3307
 
-       restore_username: root
 
-       restore_password: test2
 
- mongodb_databases:
 
-     - name: test
 
-       hostname: mongodb
 
-       username: root
 
-       password: test
 
-       authentication_database: admin
 
-       format: {mongodb_dump_format}
 
-       restore_hostname: mongodb2
 
-       restore_port: 27018
 
-       restore_username: root2
 
-       restore_password: test2
 
- sqlite_databases:
 
-     - name: sqlite_test
 
-       path: /tmp/sqlite_test.db
 
-       restore_path: /tmp/sqlite_test2.db
 
- '''
 
-     with open(config_path, 'w') as config_file:
 
-         config_file.write(config)
 
- def write_simple_custom_restore_configuration(
 
-     source_directory,
 
-     config_path,
 
-     repository_path,
 
-     borgmatic_source_directory,
 
-     postgresql_dump_format='custom',
 
- ):
 
-     '''
 
-     Write out borgmatic configuration into a file at the config path. Set the options so as to work
 
-     for testing with custom restore options, but this time using CLI arguments. This includes a
 
-     custom restore_hostname, restore_port, restore_username and restore_password as we only test
 
-     these options for PostgreSQL.
 
-     '''
 
-     config = f'''
 
- source_directories:
 
-     - {source_directory}
 
- repositories:
 
-     - path: {repository_path}
 
- borgmatic_source_directory: {borgmatic_source_directory}
 
- encryption_passphrase: "test"
 
- postgresql_databases:
 
-     - name: test
 
-       hostname: postgresql
 
-       username: postgres
 
-       password: test
 
-       format: {postgresql_dump_format}
 
- '''
 
-     with open(config_path, 'w') as config_file:
 
-         config_file.write(config)
 
- def test_database_dump_and_restore():
 
-     # Create a Borg repository.
 
-     temporary_directory = tempfile.mkdtemp()
 
-     repository_path = os.path.join(temporary_directory, 'test.borg')
 
-     borgmatic_source_directory = os.path.join(temporary_directory, '.borgmatic')
 
-     # Write out a special file to ensure that it gets properly excluded and Borg doesn't hang on it.
 
-     os.mkfifo(os.path.join(temporary_directory, 'special_file'))
 
-     original_working_directory = os.getcwd()
 
-     try:
 
-         config_path = os.path.join(temporary_directory, 'test.yaml')
 
-         write_configuration(
 
-             temporary_directory, config_path, repository_path, borgmatic_source_directory
 
-         )
 
-         subprocess.check_call(
 
-             ['borgmatic', '-v', '2', '--config', config_path, 'rcreate', '--encryption', 'repokey']
 
-         )
 
-         # Run borgmatic to generate a backup archive including a database dump.
 
-         subprocess.check_call(['borgmatic', 'create', '--config', config_path, '-v', '2'])
 
-         # Get the created archive name.
 
-         output = subprocess.check_output(
 
-             ['borgmatic', '--config', config_path, 'list', '--json']
 
-         ).decode(sys.stdout.encoding)
 
-         parsed_output = json.loads(output)
 
-         assert len(parsed_output) == 1
 
-         assert len(parsed_output[0]['archives']) == 1
 
-         archive_name = parsed_output[0]['archives'][0]['archive']
 
-         # Restore the database from the archive.
 
-         subprocess.check_call(
 
-             ['borgmatic', '-v', '2', '--config', config_path, 'restore', '--archive', archive_name]
 
-         )
 
-     finally:
 
-         os.chdir(original_working_directory)
 
-         shutil.rmtree(temporary_directory)
 
- def test_database_dump_and_restore_with_restore_cli_arguments():
 
-     # Create a Borg repository.
 
-     temporary_directory = tempfile.mkdtemp()
 
-     repository_path = os.path.join(temporary_directory, 'test.borg')
 
-     borgmatic_source_directory = os.path.join(temporary_directory, '.borgmatic')
 
-     original_working_directory = os.getcwd()
 
-     try:
 
-         config_path = os.path.join(temporary_directory, 'test.yaml')
 
-         write_simple_custom_restore_configuration(
 
-             temporary_directory, config_path, repository_path, borgmatic_source_directory
 
-         )
 
-         subprocess.check_call(
 
-             ['borgmatic', '-v', '2', '--config', config_path, 'rcreate', '--encryption', 'repokey']
 
-         )
 
-         # Run borgmatic to generate a backup archive including a database dump.
 
-         subprocess.check_call(['borgmatic', 'create', '--config', config_path, '-v', '2'])
 
-         # Get the created archive name.
 
-         output = subprocess.check_output(
 
-             ['borgmatic', '--config', config_path, 'list', '--json']
 
-         ).decode(sys.stdout.encoding)
 
-         parsed_output = json.loads(output)
 
-         assert len(parsed_output) == 1
 
-         assert len(parsed_output[0]['archives']) == 1
 
-         archive_name = parsed_output[0]['archives'][0]['archive']
 
-         # Restore the database from the archive.
 
-         subprocess.check_call(
 
-             [
 
-                 'borgmatic',
 
-                 '-v',
 
-                 '2',
 
-                 '--config',
 
-                 config_path,
 
-                 'restore',
 
-                 '--archive',
 
-                 archive_name,
 
-                 '--hostname',
 
-                 'postgresql2',
 
-                 '--port',
 
-                 '5433',
 
-                 '--username',
 
-                 'postgres2',
 
-                 '--password',
 
-                 'test2',
 
-             ]
 
-         )
 
-     finally:
 
-         os.chdir(original_working_directory)
 
-         shutil.rmtree(temporary_directory)
 
- def test_database_dump_and_restore_with_restore_configuration_options():
 
-     # Create a Borg repository.
 
-     temporary_directory = tempfile.mkdtemp()
 
-     repository_path = os.path.join(temporary_directory, 'test.borg')
 
-     borgmatic_source_directory = os.path.join(temporary_directory, '.borgmatic')
 
-     original_working_directory = os.getcwd()
 
-     try:
 
-         config_path = os.path.join(temporary_directory, 'test.yaml')
 
-         write_custom_restore_configuration(
 
-             temporary_directory, config_path, repository_path, borgmatic_source_directory
 
-         )
 
-         subprocess.check_call(
 
-             ['borgmatic', '-v', '2', '--config', config_path, 'rcreate', '--encryption', 'repokey']
 
-         )
 
-         # Run borgmatic to generate a backup archive including a database dump.
 
-         subprocess.check_call(['borgmatic', 'create', '--config', config_path, '-v', '2'])
 
-         # Get the created archive name.
 
-         output = subprocess.check_output(
 
-             ['borgmatic', '--config', config_path, 'list', '--json']
 
-         ).decode(sys.stdout.encoding)
 
-         parsed_output = json.loads(output)
 
-         assert len(parsed_output) == 1
 
-         assert len(parsed_output[0]['archives']) == 1
 
-         archive_name = parsed_output[0]['archives'][0]['archive']
 
-         # Restore the database from the archive.
 
-         subprocess.check_call(
 
-             ['borgmatic', '-v', '2', '--config', config_path, 'restore', '--archive', archive_name]
 
-         )
 
-     finally:
 
-         os.chdir(original_working_directory)
 
-         shutil.rmtree(temporary_directory)
 
- def test_database_dump_and_restore_with_directory_format():
 
-     # Create a Borg repository.
 
-     temporary_directory = tempfile.mkdtemp()
 
-     repository_path = os.path.join(temporary_directory, 'test.borg')
 
-     borgmatic_source_directory = os.path.join(temporary_directory, '.borgmatic')
 
-     original_working_directory = os.getcwd()
 
-     try:
 
-         config_path = os.path.join(temporary_directory, 'test.yaml')
 
-         write_configuration(
 
-             temporary_directory,
 
-             config_path,
 
-             repository_path,
 
-             borgmatic_source_directory,
 
-             postgresql_dump_format='directory',
 
-             mongodb_dump_format='directory',
 
-         )
 
-         subprocess.check_call(
 
-             ['borgmatic', '-v', '2', '--config', config_path, 'rcreate', '--encryption', 'repokey']
 
-         )
 
-         # Run borgmatic to generate a backup archive including a database dump.
 
-         subprocess.check_call(['borgmatic', 'create', '--config', config_path, '-v', '2'])
 
-         # Restore the database from the archive.
 
-         subprocess.check_call(
 
-             ['borgmatic', '--config', config_path, 'restore', '--archive', 'latest']
 
-         )
 
-     finally:
 
-         os.chdir(original_working_directory)
 
-         shutil.rmtree(temporary_directory)
 
- def test_database_dump_with_error_causes_borgmatic_to_exit():
 
-     # Create a Borg repository.
 
-     temporary_directory = tempfile.mkdtemp()
 
-     repository_path = os.path.join(temporary_directory, 'test.borg')
 
-     borgmatic_source_directory = os.path.join(temporary_directory, '.borgmatic')
 
-     original_working_directory = os.getcwd()
 
-     try:
 
-         config_path = os.path.join(temporary_directory, 'test.yaml')
 
-         write_configuration(
 
-             temporary_directory, config_path, repository_path, borgmatic_source_directory
 
-         )
 
-         subprocess.check_call(
 
-             ['borgmatic', '-v', '2', '--config', config_path, 'rcreate', '--encryption', 'repokey']
 
-         )
 
-         # Run borgmatic with a config override such that the database dump fails.
 
-         with pytest.raises(subprocess.CalledProcessError):
 
-             subprocess.check_call(
 
-                 [
 
-                     'borgmatic',
 
-                     'create',
 
-                     '--config',
 
-                     config_path,
 
-                     '-v',
 
-                     '2',
 
-                     '--override',
 
-                     "hooks.postgresql_databases=[{'name': 'nope'}]",  # noqa: FS003
 
-                 ]
 
-             )
 
-     finally:
 
-         os.chdir(original_working_directory)
 
-         shutil.rmtree(temporary_directory)
 
 
  |