export_tar.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import logging
  2. import os
  3. from borgmatic.borg import environment
  4. from borgmatic.execute import DO_NOT_CAPTURE, execute_command
  5. logger = logging.getLogger(__name__)
  6. def export_tar_archive(
  7. dry_run,
  8. repository,
  9. archive,
  10. paths,
  11. destination_path,
  12. storage_config,
  13. local_path='borg',
  14. remote_path=None,
  15. tar_filter=None,
  16. files=False,
  17. strip_components=None,
  18. ):
  19. '''
  20. Given a dry-run flag, a local or remote repository path, an archive name, zero or more paths to
  21. export from the archive, a destination path to export to, a storage configuration dict, optional
  22. local and remote Borg paths, an optional filter program, whether to include per-file details,
  23. and an optional number of path components to strip, export the archive into the given
  24. destination path as a tar-formatted file.
  25. If the destination path is "-", then stream the output to stdout instead of to a file.
  26. '''
  27. umask = storage_config.get('umask', None)
  28. lock_wait = storage_config.get('lock_wait', None)
  29. full_command = (
  30. (local_path, 'export-tar')
  31. + (('--remote-path', remote_path) if remote_path else ())
  32. + (('--umask', str(umask)) if umask else ())
  33. + (('--lock-wait', str(lock_wait)) if lock_wait else ())
  34. + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ())
  35. + (('--list',) if files else ())
  36. + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ())
  37. + (('--dry-run',) if dry_run else ())
  38. + (('--tar-filter', tar_filter) if tar_filter else ())
  39. + (('--strip-components', str(strip_components)) if strip_components else ())
  40. + ('::'.join((repository if ':' in repository else os.path.abspath(repository), archive)),)
  41. + (destination_path,)
  42. + (tuple(paths) if paths else ())
  43. )
  44. if files and logger.getEffectiveLevel() == logging.WARNING:
  45. output_log_level = logging.WARNING
  46. else:
  47. output_log_level = logging.INFO
  48. if dry_run:
  49. logging.info('{}: Skipping export to tar file (dry run)'.format(repository))
  50. return
  51. execute_command(
  52. full_command,
  53. output_file=DO_NOT_CAPTURE if destination_path == '-' else None,
  54. output_log_level=output_log_level,
  55. borg_local_path=local_path,
  56. extra_environment=environment.make_environment(storage_config),
  57. )