2017-11-03 05:22:40 +00:00
|
|
|
import logging
|
2017-08-05 23:21:39 +00:00
|
|
|
|
2019-06-13 03:56:20 +00:00
|
|
|
from borgmatic.execute import execute_command
|
2017-08-05 23:21:39 +00:00
|
|
|
|
2019-06-17 18:53:08 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2017-11-03 05:22:40 +00:00
|
|
|
|
|
|
|
|
2017-08-05 23:21:39 +00:00
|
|
|
def _make_prune_flags(retention_config):
|
|
|
|
'''
|
|
|
|
Given a retention config dict mapping from option name to value, tranform it into an iterable of
|
|
|
|
command-line name-value flag pairs.
|
|
|
|
|
|
|
|
For example, given a retention config of:
|
|
|
|
|
|
|
|
{'keep_weekly': 4, 'keep_monthly': 6}
|
|
|
|
|
|
|
|
This will be returned as an iterable of:
|
|
|
|
|
|
|
|
(
|
|
|
|
('--keep-weekly', '4'),
|
|
|
|
('--keep-monthly', '6'),
|
|
|
|
)
|
|
|
|
'''
|
2019-07-27 21:15:24 +00:00
|
|
|
config = retention_config.copy()
|
2019-07-27 21:04:13 +00:00
|
|
|
|
|
|
|
if 'prefix' not in config:
|
|
|
|
config['prefix'] = '{hostname}-'
|
|
|
|
elif not config['prefix']:
|
|
|
|
config.pop('prefix')
|
2017-10-30 03:14:18 +00:00
|
|
|
|
2017-08-05 23:21:39 +00:00
|
|
|
return (
|
2019-07-27 21:04:13 +00:00
|
|
|
('--' + option_name.replace('_', '-'), str(value)) for option_name, value in config.items()
|
2017-08-05 23:21:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-09-30 05:45:00 +00:00
|
|
|
def prune_archives(
|
2018-12-06 22:58:14 +00:00
|
|
|
dry_run,
|
|
|
|
repository,
|
|
|
|
storage_config,
|
|
|
|
retention_config,
|
|
|
|
local_path='borg',
|
|
|
|
remote_path=None,
|
|
|
|
stats=False,
|
2020-01-11 15:38:07 +00:00
|
|
|
files=False,
|
2018-09-30 05:45:00 +00:00
|
|
|
):
|
2017-08-05 23:21:39 +00:00
|
|
|
'''
|
2018-09-08 20:53:37 +00:00
|
|
|
Given dry-run flag, a local or remote repository path, a storage config dict, and a
|
2018-05-26 23:09:08 +00:00
|
|
|
retention config dict, prune Borg archives according to the retention policy specified in that
|
2018-02-19 23:51:04 +00:00
|
|
|
configuration.
|
2017-08-05 23:21:39 +00:00
|
|
|
'''
|
2018-06-17 22:12:43 +00:00
|
|
|
umask = storage_config.get('umask', None)
|
2018-02-19 23:51:04 +00:00
|
|
|
lock_wait = storage_config.get('lock_wait', None)
|
2019-12-04 23:48:10 +00:00
|
|
|
extra_borg_options = storage_config.get('extra_borg_options', {}).get('prune', '')
|
2017-08-05 23:21:39 +00:00
|
|
|
|
|
|
|
full_command = (
|
2019-09-12 22:27:04 +00:00
|
|
|
(local_path, 'prune')
|
2018-09-30 05:45:00 +00:00
|
|
|
+ tuple(element for pair in _make_prune_flags(retention_config) for element in pair)
|
2018-06-17 22:12:43 +00:00
|
|
|
+ (('--remote-path', remote_path) if remote_path else ())
|
|
|
|
+ (('--umask', str(umask)) if umask else ())
|
|
|
|
+ (('--lock-wait', str(lock_wait)) if lock_wait else ())
|
2020-01-22 23:23:49 +00:00
|
|
|
+ (('--stats',) if stats and not dry_run else ())
|
2020-01-11 15:38:07 +00:00
|
|
|
+ (('--info',) if logger.getEffectiveLevel() == logging.INFO else ())
|
2020-01-22 23:23:49 +00:00
|
|
|
+ (('--list',) if files else ())
|
2020-01-22 18:03:26 +00:00
|
|
|
+ (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ())
|
2018-06-17 22:12:43 +00:00
|
|
|
+ (('--dry-run',) if dry_run else ())
|
2019-12-04 23:48:10 +00:00
|
|
|
+ (tuple(extra_borg_options.split(' ')) if extra_borg_options else ())
|
2019-09-12 22:27:04 +00:00
|
|
|
+ (repository,)
|
2018-06-17 22:12:43 +00:00
|
|
|
)
|
2017-08-05 23:21:39 +00:00
|
|
|
|
2020-01-22 21:28:24 +00:00
|
|
|
if (stats or files) and logger.getEffectiveLevel() == logging.WARNING:
|
|
|
|
output_log_level = logging.WARNING
|
|
|
|
else:
|
|
|
|
output_log_level = logging.INFO
|
|
|
|
|
|
|
|
execute_command(full_command, output_log_level=output_log_level, error_on_warnings=False)
|