compact.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import logging
  2. from borgmatic.borg import environment, flags
  3. from borgmatic.execute import execute_command
  4. logger = logging.getLogger(__name__)
  5. def compact_segments(
  6. dry_run,
  7. repository_path,
  8. storage_config,
  9. local_borg_version,
  10. local_path='borg',
  11. remote_path=None,
  12. progress=False,
  13. cleanup_commits=False,
  14. threshold=None,
  15. ):
  16. '''
  17. Given dry-run flag, a local or remote repository path, a storage config dict, and the local
  18. Borg version, compact the segments in a repository.
  19. '''
  20. umask = storage_config.get('umask', None)
  21. lock_wait = storage_config.get('lock_wait', None)
  22. extra_borg_options = storage_config.get('extra_borg_options', {}).get('compact', '')
  23. full_command = (
  24. (local_path, 'compact')
  25. + (('--remote-path', remote_path) if remote_path else ())
  26. + (('--umask', str(umask)) if umask else ())
  27. + (('--lock-wait', str(lock_wait)) if lock_wait else ())
  28. + (('--progress',) if progress else ())
  29. + (('--cleanup-commits',) if cleanup_commits else ())
  30. + (('--threshold', str(threshold)) if threshold else ())
  31. + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ())
  32. + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ())
  33. + (tuple(extra_borg_options.split(' ')) if extra_borg_options else ())
  34. + flags.make_repository_flags(repository_path, local_borg_version)
  35. )
  36. if dry_run:
  37. logging.info(f'{repository_path}: Skipping compact (dry run)')
  38. return
  39. execute_command(
  40. full_command,
  41. output_log_level=logging.INFO,
  42. borg_local_path=local_path,
  43. extra_environment=environment.make_environment(storage_config),
  44. )