2018-10-01 02:30:04 +02:00
|
|
|
import json
|
|
|
|
import os
|
2018-10-04 07:36:25 +02:00
|
|
|
import shutil
|
2018-10-01 02:30:04 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2018-10-04 07:36:25 +02:00
|
|
|
import tempfile
|
2018-10-01 02:30:04 +02:00
|
|
|
|
|
|
|
|
2018-10-04 07:36:25 +02:00
|
|
|
def generate_configuration(config_path, repository_path):
|
|
|
|
'''
|
|
|
|
Generate borgmatic configuration into a file at the config path, and update the defaults so as
|
2018-12-11 07:20:57 +01:00
|
|
|
to work for testing (including injecting the given repository path and tacking on an encryption
|
|
|
|
passphrase).
|
2018-10-04 07:36:25 +02:00
|
|
|
'''
|
2023-06-25 00:35:10 +02:00
|
|
|
subprocess.check_call(f'borgmatic config generate --destination {config_path}'.split(' '))
|
2018-10-01 02:30:04 +02:00
|
|
|
config = (
|
2018-10-04 07:36:25 +02:00
|
|
|
open(config_path)
|
2018-10-01 02:30:04 +02:00
|
|
|
.read()
|
2022-08-22 08:29:13 +02:00
|
|
|
.replace('ssh://user@backupserver/./sourcehostname.borg', repository_path)
|
2023-03-24 21:04:03 +01:00
|
|
|
.replace('- path: /mnt/backup', '')
|
|
|
|
.replace('label: local', '')
|
2023-03-24 21:20:47 +01:00
|
|
|
.replace('- /home', f'- {config_path}')
|
2018-10-04 07:36:25 +02:00
|
|
|
.replace('- /etc', '')
|
2018-10-01 02:30:04 +02:00
|
|
|
.replace('- /var/log/syslog*', '')
|
2023-07-12 18:39:45 +02:00
|
|
|
+ 'encryption_passphrase: "test"'
|
2018-10-01 02:30:04 +02:00
|
|
|
)
|
2018-10-04 07:36:25 +02:00
|
|
|
config_file = open(config_path, 'w')
|
2018-10-01 02:30:04 +02:00
|
|
|
config_file.write(config)
|
|
|
|
config_file.close()
|
|
|
|
|
|
|
|
|
|
|
|
def test_borgmatic_command():
|
|
|
|
# Create a Borg repository.
|
2018-10-04 07:36:25 +02:00
|
|
|
temporary_directory = tempfile.mkdtemp()
|
|
|
|
repository_path = os.path.join(temporary_directory, 'test.borg')
|
2019-02-18 22:47:18 +01:00
|
|
|
extract_path = os.path.join(temporary_directory, 'extract')
|
|
|
|
|
|
|
|
original_working_directory = os.getcwd()
|
|
|
|
os.mkdir(extract_path)
|
|
|
|
os.chdir(extract_path)
|
2018-10-01 02:30:04 +02:00
|
|
|
|
2018-10-04 07:36:25 +02:00
|
|
|
try:
|
|
|
|
config_path = os.path.join(temporary_directory, 'test.yaml')
|
|
|
|
generate_configuration(config_path, repository_path)
|
|
|
|
|
2018-12-11 07:20:57 +01:00
|
|
|
subprocess.check_call(
|
2023-06-25 00:52:20 +02:00
|
|
|
f'borgmatic -v 2 --config {config_path} rcreate --encryption repokey'.split(' ')
|
2018-12-11 07:20:57 +01:00
|
|
|
)
|
|
|
|
|
2018-10-04 07:36:25 +02:00
|
|
|
# Run borgmatic to generate a backup archive, and then list it to make sure it exists.
|
2023-03-24 07:11:14 +01:00
|
|
|
subprocess.check_call(f'borgmatic --config {config_path}'.split(' '))
|
2018-10-04 07:36:25 +02:00
|
|
|
output = subprocess.check_output(
|
2023-03-24 07:11:14 +01:00
|
|
|
f'borgmatic --config {config_path} list --json'.split(' ')
|
2018-10-15 18:37:26 +02:00
|
|
|
).decode(sys.stdout.encoding)
|
2018-10-04 07:36:25 +02:00
|
|
|
parsed_output = json.loads(output)
|
2018-10-01 02:30:04 +02:00
|
|
|
|
2018-10-04 07:36:25 +02:00
|
|
|
assert len(parsed_output) == 1
|
|
|
|
assert len(parsed_output[0]['archives']) == 1
|
2019-02-18 22:47:18 +01:00
|
|
|
archive_name = parsed_output[0]['archives'][0]['archive']
|
|
|
|
|
|
|
|
# Extract the created archive into the current (temporary) directory, and confirm that the
|
|
|
|
# extracted file looks right.
|
|
|
|
output = subprocess.check_output(
|
2023-03-24 07:11:14 +01:00
|
|
|
f'borgmatic --config {config_path} extract --archive {archive_name}'.split(' '),
|
2019-02-18 22:47:18 +01:00
|
|
|
).decode(sys.stdout.encoding)
|
|
|
|
extracted_config_path = os.path.join(extract_path, config_path)
|
|
|
|
assert open(extracted_config_path).read() == open(config_path).read()
|
2018-10-13 22:09:12 +02:00
|
|
|
|
2020-05-15 19:12:49 +02:00
|
|
|
# Exercise the info action.
|
2018-10-13 22:09:12 +02:00
|
|
|
output = subprocess.check_output(
|
2023-03-24 07:11:14 +01:00
|
|
|
f'borgmatic --config {config_path} info --json'.split(' '),
|
2018-10-15 18:37:26 +02:00
|
|
|
).decode(sys.stdout.encoding)
|
2018-10-13 22:09:12 +02:00
|
|
|
parsed_output = json.loads(output)
|
|
|
|
|
|
|
|
assert len(parsed_output) == 1
|
|
|
|
assert 'repository' in parsed_output[0]
|
2018-10-04 07:36:25 +02:00
|
|
|
finally:
|
2019-02-18 22:47:18 +01:00
|
|
|
os.chdir(original_working_directory)
|
2018-10-04 07:36:25 +02:00
|
|
|
shutil.rmtree(temporary_directory)
|