check.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import os
  2. import subprocess
  3. from borgmatic.borg import extract
  4. from borgmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
  5. DEFAULT_CHECKS = ('repository', 'archives')
  6. def _parse_checks(consistency_config):
  7. '''
  8. Given a consistency config with a "checks" list, transform it to a tuple of named checks to run.
  9. For example, given a retention config of:
  10. {'checks': ['repository', 'archives']}
  11. This will be returned as:
  12. ('repository', 'archives')
  13. If no "checks" option is present, return the DEFAULT_CHECKS. If the checks value is the string
  14. "disabled", return an empty tuple, meaning that no checks should be run.
  15. '''
  16. checks = consistency_config.get('checks', [])
  17. if checks == ['disabled']:
  18. return ()
  19. return tuple(check for check in checks if check.lower() not in ('disabled', '')) or DEFAULT_CHECKS
  20. def _make_check_flags(checks, check_last=None):
  21. '''
  22. Given a parsed sequence of checks, transform it into tuple of command-line flags.
  23. For example, given parsed checks of:
  24. ('repository',)
  25. This will be returned as:
  26. ('--repository-only',)
  27. Additionally, if a check_last value is given, a "--last" flag will be added.
  28. '''
  29. last_flag = ('--last', str(check_last)) if check_last else ()
  30. if checks == DEFAULT_CHECKS:
  31. return last_flag
  32. return tuple(
  33. '--{}-only'.format(check) for check in checks
  34. if check in DEFAULT_CHECKS
  35. ) + last_flag
  36. def check_archives(verbosity, repository, consistency_config, remote_path=None):
  37. '''
  38. Given a verbosity flag, a local or remote repository path, a consistency config dict, and a
  39. command to run, check the contained Borg archives for consistency.
  40. If there are no consistency checks to run, skip running them.
  41. '''
  42. checks = _parse_checks(consistency_config)
  43. check_last = consistency_config.get('check_last', None)
  44. if set(checks).intersection(set(DEFAULT_CHECKS)):
  45. remote_path_flags = ('--remote-path', remote_path) if remote_path else ()
  46. verbosity_flags = {
  47. VERBOSITY_SOME: ('--info',),
  48. VERBOSITY_LOTS: ('--debug',),
  49. }.get(verbosity, ())
  50. full_command = (
  51. 'borg', 'check',
  52. repository,
  53. ) + _make_check_flags(checks, check_last) + remote_path_flags + verbosity_flags
  54. # The check command spews to stdout/stderr even without the verbose flag. Suppress it.
  55. stdout = None if verbosity_flags else open(os.devnull, 'w')
  56. subprocess.check_call(full_command, stdout=stdout, stderr=subprocess.STDOUT)
  57. if 'extract' in checks:
  58. extract.extract_last_archive_dry_run(verbosity, repository, remote_path)