prune.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import subprocess
  2. from borgmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
  3. def _make_prune_flags(retention_config):
  4. '''
  5. Given a retention config dict mapping from option name to value, tranform it into an iterable of
  6. command-line name-value flag pairs.
  7. For example, given a retention config of:
  8. {'keep_weekly': 4, 'keep_monthly': 6}
  9. This will be returned as an iterable of:
  10. (
  11. ('--keep-weekly', '4'),
  12. ('--keep-monthly', '6'),
  13. )
  14. '''
  15. return (
  16. ('--' + option_name.replace('_', '-'), str(retention_config[option_name]))
  17. for option_name, value in retention_config.items()
  18. )
  19. def prune_archives(verbosity, repository, retention_config, remote_path=None):
  20. '''
  21. Given a verbosity flag, a local or remote repository path, a retention config dict, prune Borg
  22. archives according the the retention policy specified in that configuration.
  23. '''
  24. remote_path_flags = ('--remote-path', remote_path) if remote_path else ()
  25. verbosity_flags = {
  26. VERBOSITY_SOME: ('--info', '--stats',),
  27. VERBOSITY_LOTS: ('--debug', '--stats'),
  28. }.get(verbosity, ())
  29. full_command = (
  30. 'borg', 'prune',
  31. repository,
  32. ) + tuple(
  33. element
  34. for pair in _make_prune_flags(retention_config)
  35. for element in pair
  36. ) + remote_path_flags + verbosity_flags
  37. subprocess.check_call(full_command)