extract.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import sys
  2. import subprocess
  3. from borgmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
  4. def extract_last_archive_dry_run(verbosity, repository, remote_path=None):
  5. '''
  6. Perform an extraction dry-run of just the most recent archive. If there are no archives, skip
  7. the dry-run.
  8. '''
  9. remote_path_flags = ('--remote-path', remote_path) if remote_path else ()
  10. verbosity_flags = {
  11. VERBOSITY_SOME: ('--info',),
  12. VERBOSITY_LOTS: ('--debug',),
  13. }.get(verbosity, ())
  14. full_list_command = (
  15. 'borg', 'list',
  16. '--short',
  17. repository,
  18. ) + remote_path_flags + verbosity_flags
  19. list_output = subprocess.check_output(full_list_command).decode(sys.stdout.encoding)
  20. last_archive_name = list_output.strip().split('\n')[-1]
  21. if not last_archive_name:
  22. return
  23. list_flag = ('--list',) if verbosity == VERBOSITY_LOTS else ()
  24. full_extract_command = (
  25. 'borg', 'extract',
  26. '--dry-run',
  27. '{repository}::{last_archive_name}'.format(
  28. repository=repository,
  29. last_archive_name=last_archive_name,
  30. ),
  31. ) + remote_path_flags + verbosity_flags + list_flag
  32. subprocess.check_call(full_extract_command)