A wrapper script for Borg backup software that creates and prunes backups
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_borgmatic.py 2.2KB

  1. import json
  2. import os
  3. import shutil
  4. import subprocess
  5. import sys
  6. import tempfile
  7. def generate_configuration(config_path, repository_path):
  8. '''
  9. Generate borgmatic configuration into a file at the config path, and update the defaults so as
  10. to work for testing (including injecting the given repository path and tacking on an encryption
  11. passphrase).
  12. '''
  13. subprocess.check_call(
  14. 'generate-borgmatic-config --destination {}'.format(config_path).split(' ')
  15. )
  16. config = (
  17. open(config_path)
  18. .read()
  19. .replace('user@backupserver:sourcehostname.borg', repository_path)
  20. .replace('- /home', '- {}'.format(config_path))
  21. .replace('- /etc', '')
  22. .replace('- /var/log/syslog*', '')
  23. + 'storage:\n encryption_passphrase: "test"'
  24. )
  25. config_file = open(config_path, 'w')
  26. config_file.write(config)
  27. config_file.close()
  28. def test_borgmatic_command():
  29. # Create a Borg repository.
  30. temporary_directory = tempfile.mkdtemp()
  31. repository_path = os.path.join(temporary_directory, 'test.borg')
  32. try:
  33. config_path = os.path.join(temporary_directory, 'test.yaml')
  34. generate_configuration(config_path, repository_path)
  35. subprocess.check_call(
  36. 'borgmatic -v 2 --config {} --init --encryption repokey'.format(config_path).split(' ')
  37. )
  38. # Run borgmatic to generate a backup archive, and then list it to make sure it exists.
  39. subprocess.check_call('borgmatic --config {}'.format(config_path).split(' '))
  40. output = subprocess.check_output(
  41. 'borgmatic --config {} --list --json'.format(config_path).split(' ')
  42. ).decode(sys.stdout.encoding)
  43. parsed_output = json.loads(output)
  44. assert len(parsed_output) == 1
  45. assert len(parsed_output[0]['archives']) == 1
  46. # Also exercise the info flag.
  47. output = subprocess.check_output(
  48. 'borgmatic --config {} --info --json'.format(config_path).split(' ')
  49. ).decode(sys.stdout.encoding)
  50. parsed_output = json.loads(output)
  51. assert len(parsed_output) == 1
  52. assert 'repository' in parsed_output[0]
  53. finally:
  54. shutil.rmtree(temporary_directory)