2019-12-11 01:41:01 +01:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
|
2020-05-15 19:12:49 +02:00
|
|
|
import pytest
|
|
|
|
|
2019-12-11 01:41:01 +01:00
|
|
|
|
2020-05-18 20:31:29 +02:00
|
|
|
def write_configuration(
|
2022-10-03 21:58:13 +02:00
|
|
|
source_directory,
|
|
|
|
config_path,
|
|
|
|
repository_path,
|
|
|
|
borgmatic_source_directory,
|
|
|
|
postgresql_dump_format='custom',
|
2023-02-21 00:18:51 +01:00
|
|
|
mongodb_dump_format='archive',
|
2020-05-18 20:31:29 +02:00
|
|
|
):
|
2019-12-11 01:41:01 +01:00
|
|
|
'''
|
|
|
|
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
|
2020-05-18 20:31:29 +02:00
|
|
|
storing database dumps, dump format (for PostgreSQL), and encryption passphrase.
|
2019-12-11 01:41:01 +01:00
|
|
|
'''
|
2022-10-03 21:58:13 +02:00
|
|
|
config = f'''
|
2019-12-11 01:41:01 +01:00
|
|
|
location:
|
|
|
|
source_directories:
|
2022-10-03 21:58:13 +02:00
|
|
|
- {source_directory}
|
2019-12-11 01:41:01 +01:00
|
|
|
repositories:
|
2022-10-03 21:58:13 +02:00
|
|
|
- {repository_path}
|
|
|
|
borgmatic_source_directory: {borgmatic_source_directory}
|
2019-12-11 01:41:01 +01:00
|
|
|
|
|
|
|
storage:
|
|
|
|
encryption_passphrase: "test"
|
|
|
|
|
|
|
|
hooks:
|
|
|
|
postgresql_databases:
|
|
|
|
- name: test
|
2019-12-11 01:43:43 +01:00
|
|
|
hostname: postgresql
|
2019-12-11 01:41:01 +01:00
|
|
|
username: postgres
|
|
|
|
password: test
|
2022-10-03 21:58:13 +02:00
|
|
|
format: {postgresql_dump_format}
|
2020-04-25 00:32:53 +02:00
|
|
|
- name: all
|
|
|
|
hostname: postgresql
|
|
|
|
username: postgres
|
|
|
|
password: test
|
2023-01-26 08:31:07 +01:00
|
|
|
- name: all
|
|
|
|
format: custom
|
|
|
|
hostname: postgresql
|
|
|
|
username: postgres
|
|
|
|
password: test
|
2019-12-12 01:43:01 +01:00
|
|
|
mysql_databases:
|
|
|
|
- name: test
|
|
|
|
hostname: mysql
|
|
|
|
username: root
|
|
|
|
password: test
|
2020-04-22 21:17:22 +02:00
|
|
|
- name: all
|
|
|
|
hostname: mysql
|
|
|
|
username: root
|
|
|
|
password: test
|
2023-01-26 08:31:07 +01:00
|
|
|
- name: all
|
|
|
|
format: sql
|
|
|
|
hostname: mysql
|
|
|
|
username: root
|
|
|
|
password: test
|
2021-12-26 01:00:58 +01:00
|
|
|
mongodb_databases:
|
|
|
|
- name: test
|
|
|
|
hostname: mongodb
|
|
|
|
username: root
|
|
|
|
password: test
|
2022-01-05 01:26:38 +01:00
|
|
|
authentication_database: admin
|
2023-02-21 00:18:51 +01:00
|
|
|
format: {mongodb_dump_format}
|
2021-12-26 01:00:58 +01:00
|
|
|
- name: all
|
|
|
|
hostname: mongodb
|
|
|
|
username: root
|
|
|
|
password: test
|
2023-03-02 19:25:16 +01:00
|
|
|
sqlite_databases:
|
|
|
|
- name: sqlite_test
|
2023-03-03 20:59:01 +01:00
|
|
|
path: /tmp/sqlite_test.db
|
2022-10-03 21:58:13 +02:00
|
|
|
'''
|
2019-12-11 01:41:01 +01:00
|
|
|
|
2021-12-26 01:00:58 +01:00
|
|
|
with open(config_path, 'w') as config_file:
|
|
|
|
config_file.write(config)
|
2019-12-11 01:41:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
2022-10-03 21:58:13 +02:00
|
|
|
# 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'))
|
|
|
|
|
2019-12-11 01:41:01 +01:00
|
|
|
original_working_directory = os.getcwd()
|
|
|
|
|
|
|
|
try:
|
|
|
|
config_path = os.path.join(temporary_directory, 'test.yaml')
|
2022-10-03 21:58:13 +02:00
|
|
|
write_configuration(
|
|
|
|
temporary_directory, config_path, repository_path, borgmatic_source_directory
|
|
|
|
)
|
2019-12-11 01:41:01 +01:00
|
|
|
|
|
|
|
subprocess.check_call(
|
2021-12-29 22:18:50 +01:00
|
|
|
['borgmatic', '-v', '2', '--config', config_path, 'init', '--encryption', 'repokey']
|
2019-12-11 01:41:01 +01:00
|
|
|
)
|
|
|
|
|
2020-05-15 19:12:49 +02:00
|
|
|
# Run borgmatic to generate a backup archive including a database dump.
|
2021-12-29 22:18:50 +01:00
|
|
|
subprocess.check_call(['borgmatic', 'create', '--config', config_path, '-v', '2'])
|
2019-12-11 01:41:01 +01:00
|
|
|
|
|
|
|
# Get the created archive name.
|
|
|
|
output = subprocess.check_output(
|
2021-12-29 22:18:50 +01:00
|
|
|
['borgmatic', '--config', config_path, 'list', '--json']
|
2019-12-11 01:41:01 +01:00
|
|
|
).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(
|
2021-12-29 22:18:50 +01:00
|
|
|
['borgmatic', '--config', config_path, 'restore', '--archive', archive_name]
|
2019-12-11 01:41:01 +01:00
|
|
|
)
|
|
|
|
finally:
|
|
|
|
os.chdir(original_working_directory)
|
|
|
|
shutil.rmtree(temporary_directory)
|
2020-05-15 19:12:49 +02:00
|
|
|
|
|
|
|
|
2020-05-18 20:31:29 +02:00
|
|
|
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(
|
2022-10-03 21:58:13 +02:00
|
|
|
temporary_directory,
|
2020-05-18 20:31:29 +02:00
|
|
|
config_path,
|
|
|
|
repository_path,
|
|
|
|
borgmatic_source_directory,
|
|
|
|
postgresql_dump_format='directory',
|
2023-02-21 00:18:51 +01:00
|
|
|
mongodb_dump_format='directory',
|
2020-05-18 20:31:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
subprocess.check_call(
|
2021-12-29 22:18:50 +01:00
|
|
|
['borgmatic', '-v', '2', '--config', config_path, 'init', '--encryption', 'repokey']
|
2020-05-18 20:31:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Run borgmatic to generate a backup archive including a database dump.
|
2021-12-29 22:18:50 +01:00
|
|
|
subprocess.check_call(['borgmatic', 'create', '--config', config_path, '-v', '2'])
|
2020-05-18 20:31:29 +02:00
|
|
|
|
|
|
|
# Restore the database from the archive.
|
|
|
|
subprocess.check_call(
|
2021-12-29 22:18:50 +01:00
|
|
|
['borgmatic', '--config', config_path, 'restore', '--archive', 'latest']
|
2020-05-18 20:31:29 +02:00
|
|
|
)
|
|
|
|
finally:
|
|
|
|
os.chdir(original_working_directory)
|
|
|
|
shutil.rmtree(temporary_directory)
|
|
|
|
|
|
|
|
|
2020-05-15 19:12:49 +02:00
|
|
|
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')
|
2022-10-03 21:58:13 +02:00
|
|
|
write_configuration(
|
|
|
|
temporary_directory, config_path, repository_path, borgmatic_source_directory
|
|
|
|
)
|
2020-05-15 19:12:49 +02:00
|
|
|
|
|
|
|
subprocess.check_call(
|
2021-12-29 22:18:50 +01:00
|
|
|
['borgmatic', '-v', '2', '--config', config_path, 'init', '--encryption', 'repokey']
|
2020-05-15 19:12:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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',
|
2023-03-24 07:11:14 +01:00
|
|
|
"hooks.postgresql_databases=[{'name': 'nope'}]", # noqa: FS003
|
2020-05-15 19:12:49 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
os.chdir(original_working_directory)
|
|
|
|
shutil.rmtree(temporary_directory)
|