diff --git a/NEWS b/NEWS index 0ad2787..2229bbb 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,8 @@ 2.0.0.dev0 * #557: Support for Borg 2 while still working with Borg 1. If you install Borg 2, you'll need to - manually "borg transfer" or "borgmatic transfer" any existing Borg 1 repositories before use. + manually "borg transfer" or "borgmatic transfer" any existing Borg 1 repositories before use. See + the Borg 2.0 changelog summary for more information about Borg 2: + https://www.borgbackup.org/releases/borg-2.0.html * #565: Fix handling of "repository" and "data" consistency checks to prevent invalid Borg flags. * #566: Modify "mount" and "extract" actions to require the "--repository" flag when multiple repositories are configured. diff --git a/borgmatic/borg/prune.py b/borgmatic/borg/prune.py index 9b2f2b4..60898a1 100644 --- a/borgmatic/borg/prune.py +++ b/borgmatic/borg/prune.py @@ -1,12 +1,12 @@ import logging -from borgmatic.borg import environment +from borgmatic.borg import environment, feature from borgmatic.execute import execute_command logger = logging.getLogger(__name__) -def _make_prune_flags(retention_config): +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. @@ -23,11 +23,9 @@ def _make_prune_flags(retention_config): ) ''' config = retention_config.copy() - - if 'prefix' not in config: - config['prefix'] = '{hostname}-' - elif not config['prefix']: - config.pop('prefix') + prefix = config.pop('prefix', '{hostname}-') + if prefix: + config['glob_archives'] = f'{prefix}*' return ( ('--' + option_name.replace('_', '-'), str(value)) for option_name, value in config.items() @@ -39,6 +37,7 @@ def prune_archives( repository, storage_config, retention_config, + local_borg_version, local_path='borg', remote_path=None, stats=False, @@ -55,7 +54,7 @@ def prune_archives( full_command = ( (local_path, 'prune') - + tuple(element for pair in _make_prune_flags(retention_config) for element in pair) + + tuple(element for pair in make_prune_flags(retention_config) for element in pair) + (('--remote-path', remote_path) if remote_path else ()) + (('--umask', str(umask)) if umask else ()) + (('--lock-wait', str(lock_wait)) if lock_wait else ()) @@ -65,6 +64,11 @@ def prune_archives( + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + (('--dry-run',) if dry_run else ()) + (tuple(extra_borg_options.split(' ')) if extra_borg_options else ()) + + ( + ('--repo',) + if feature.available(feature.Feature.SEPARATE_REPOSITORY_ARCHIVE, local_borg_version) + else () + ) + (repository,) ) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index d9901a2..a4e5714 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -277,6 +277,7 @@ def run_actions( repository, storage, retention, + local_borg_version, local_path=local_path, remote_path=remote_path, stats=arguments['prune'].stats, diff --git a/tests/unit/borg/test_prune.py b/tests/unit/borg/test_prune.py index f4f34ed..1ca794d 100644 --- a/tests/unit/borg/test_prune.py +++ b/tests/unit/borg/test_prune.py @@ -21,20 +21,20 @@ def insert_execute_command_mock(prune_command, output_log_level): BASE_PRUNE_FLAGS = (('--keep-daily', '1'), ('--keep-weekly', '2'), ('--keep-monthly', '3')) -def test_make_prune_flags_returns_flags_from_config_plus_default_prefix(): +def test_make_prune_flags_returns_flags_from_config_plus_default_prefix_glob(): retention_config = OrderedDict((('keep_daily', 1), ('keep_weekly', 2), ('keep_monthly', 3))) - result = module._make_prune_flags(retention_config) + result = module.make_prune_flags(retention_config) - assert tuple(result) == BASE_PRUNE_FLAGS + (('--prefix', '{hostname}-'),) + assert tuple(result) == BASE_PRUNE_FLAGS + (('--glob-archives', '{hostname}-*'),) def test_make_prune_flags_accepts_prefix_with_placeholders(): retention_config = OrderedDict((('keep_daily', 1), ('prefix', 'Documents_{hostname}-{now}'))) - result = module._make_prune_flags(retention_config) + result = module.make_prune_flags(retention_config) - expected = (('--keep-daily', '1'), ('--prefix', 'Documents_{hostname}-{now}')) + expected = (('--keep-daily', '1'), ('--glob-archives', 'Documents_{hostname}-{now}*')) assert tuple(result) == expected @@ -42,7 +42,7 @@ def test_make_prune_flags_accepts_prefix_with_placeholders(): def test_make_prune_flags_treats_empty_prefix_as_no_prefix(): retention_config = OrderedDict((('keep_daily', 1), ('prefix', ''))) - result = module._make_prune_flags(retention_config) + result = module.make_prune_flags(retention_config) expected = (('--keep-daily', '1'),) @@ -52,7 +52,7 @@ def test_make_prune_flags_treats_empty_prefix_as_no_prefix(): def test_make_prune_flags_treats_none_prefix_as_no_prefix(): retention_config = OrderedDict((('keep_daily', 1), ('prefix', None))) - result = module._make_prune_flags(retention_config) + result = module.make_prune_flags(retention_config) expected = (('--keep-daily', '1'),) @@ -64,59 +64,97 @@ PRUNE_COMMAND = ('borg', 'prune', '--keep-daily', '1', '--keep-weekly', '2', '-- def test_prune_archives_calls_borg_with_parameters(): retention_config = flexmock() - flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return( + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( BASE_PRUNE_FLAGS ) + flexmock(module.feature).should_receive('available').and_return(False) insert_execute_command_mock(PRUNE_COMMAND + ('repo',), logging.INFO) module.prune_archives( - dry_run=False, repository='repo', storage_config={}, retention_config=retention_config + dry_run=False, + repository='repo', + storage_config={}, + retention_config=retention_config, + local_borg_version='1.2.3', + ) + + +def test_prune_archives_with_borg_features_calls_borg_with_repo_flag(): + retention_config = flexmock() + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( + BASE_PRUNE_FLAGS + ) + flexmock(module.feature).should_receive('available').and_return(True) + insert_execute_command_mock(PRUNE_COMMAND + ('--repo', 'repo'), logging.INFO) + + module.prune_archives( + dry_run=False, + repository='repo', + storage_config={}, + retention_config=retention_config, + local_borg_version='1.2.3', ) def test_prune_archives_with_log_info_calls_borg_with_info_parameter(): retention_config = flexmock() - flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return( + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( BASE_PRUNE_FLAGS ) + flexmock(module.feature).should_receive('available').and_return(False) insert_execute_command_mock(PRUNE_COMMAND + ('--info', 'repo'), logging.INFO) insert_logging_mock(logging.INFO) module.prune_archives( - repository='repo', storage_config={}, dry_run=False, retention_config=retention_config + repository='repo', + storage_config={}, + dry_run=False, + retention_config=retention_config, + local_borg_version='1.2.3', ) def test_prune_archives_with_log_debug_calls_borg_with_debug_parameter(): retention_config = flexmock() - flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return( + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( BASE_PRUNE_FLAGS ) + flexmock(module.feature).should_receive('available').and_return(False) insert_execute_command_mock(PRUNE_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO) insert_logging_mock(logging.DEBUG) module.prune_archives( - repository='repo', storage_config={}, dry_run=False, retention_config=retention_config + repository='repo', + storage_config={}, + dry_run=False, + retention_config=retention_config, + local_borg_version='1.2.3', ) def test_prune_archives_with_dry_run_calls_borg_with_dry_run_parameter(): retention_config = flexmock() - flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return( + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( BASE_PRUNE_FLAGS ) + flexmock(module.feature).should_receive('available').and_return(False) insert_execute_command_mock(PRUNE_COMMAND + ('--dry-run', 'repo'), logging.INFO) module.prune_archives( - repository='repo', storage_config={}, dry_run=True, retention_config=retention_config + repository='repo', + storage_config={}, + dry_run=True, + retention_config=retention_config, + local_borg_version='1.2.3', ) def test_prune_archives_with_local_path_calls_borg_via_local_path(): retention_config = flexmock() - flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return( + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( BASE_PRUNE_FLAGS ) + flexmock(module.feature).should_receive('available').and_return(False) insert_execute_command_mock(('borg1',) + PRUNE_COMMAND[1:] + ('repo',), logging.INFO) module.prune_archives( @@ -124,15 +162,17 @@ def test_prune_archives_with_local_path_calls_borg_via_local_path(): repository='repo', storage_config={}, retention_config=retention_config, + local_borg_version='1.2.3', local_path='borg1', ) def test_prune_archives_with_remote_path_calls_borg_with_remote_path_parameters(): retention_config = flexmock() - flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return( + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( BASE_PRUNE_FLAGS ) + flexmock(module.feature).should_receive('available').and_return(False) insert_execute_command_mock(PRUNE_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO) module.prune_archives( @@ -140,15 +180,17 @@ def test_prune_archives_with_remote_path_calls_borg_with_remote_path_parameters( repository='repo', storage_config={}, retention_config=retention_config, + local_borg_version='1.2.3', remote_path='borg1', ) def test_prune_archives_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level(): retention_config = flexmock() - flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return( + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( BASE_PRUNE_FLAGS ) + flexmock(module.feature).should_receive('available').and_return(False) insert_execute_command_mock(PRUNE_COMMAND + ('--stats', 'repo'), logging.WARNING) module.prune_archives( @@ -156,15 +198,17 @@ def test_prune_archives_with_stats_calls_borg_with_stats_parameter_and_warning_o repository='repo', storage_config={}, retention_config=retention_config, + local_borg_version='1.2.3', stats=True, ) def test_prune_archives_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level(): retention_config = flexmock() - flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return( + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( BASE_PRUNE_FLAGS ) + flexmock(module.feature).should_receive('available').and_return(False) insert_logging_mock(logging.INFO) insert_execute_command_mock(PRUNE_COMMAND + ('--stats', '--info', 'repo'), logging.INFO) @@ -173,15 +217,17 @@ def test_prune_archives_with_stats_and_log_info_calls_borg_with_stats_parameter_ repository='repo', storage_config={}, retention_config=retention_config, + local_borg_version='1.2.3', stats=True, ) def test_prune_archives_with_files_calls_borg_with_list_parameter_and_warning_output_log_level(): retention_config = flexmock() - flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return( + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( BASE_PRUNE_FLAGS ) + flexmock(module.feature).should_receive('available').and_return(False) insert_execute_command_mock(PRUNE_COMMAND + ('--list', 'repo'), logging.WARNING) module.prune_archives( @@ -189,15 +235,17 @@ def test_prune_archives_with_files_calls_borg_with_list_parameter_and_warning_ou repository='repo', storage_config={}, retention_config=retention_config, + local_borg_version='1.2.3', files=True, ) def test_prune_archives_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level(): retention_config = flexmock() - flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return( + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( BASE_PRUNE_FLAGS ) + flexmock(module.feature).should_receive('available').and_return(False) insert_logging_mock(logging.INFO) insert_execute_command_mock(PRUNE_COMMAND + ('--info', '--list', 'repo'), logging.INFO) @@ -206,6 +254,7 @@ def test_prune_archives_with_files_and_log_info_calls_borg_with_list_parameter_a repository='repo', storage_config={}, retention_config=retention_config, + local_borg_version='1.2.3', files=True, ) @@ -213,9 +262,10 @@ def test_prune_archives_with_files_and_log_info_calls_borg_with_list_parameter_a def test_prune_archives_with_umask_calls_borg_with_umask_parameters(): storage_config = {'umask': '077'} retention_config = flexmock() - flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return( + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( BASE_PRUNE_FLAGS ) + flexmock(module.feature).should_receive('available').and_return(False) insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'), logging.INFO) module.prune_archives( @@ -223,15 +273,17 @@ def test_prune_archives_with_umask_calls_borg_with_umask_parameters(): repository='repo', storage_config=storage_config, retention_config=retention_config, + local_borg_version='1.2.3', ) def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_parameters(): storage_config = {'lock_wait': 5} retention_config = flexmock() - flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return( + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( BASE_PRUNE_FLAGS ) + flexmock(module.feature).should_receive('available').and_return(False) insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO) module.prune_archives( @@ -239,14 +291,16 @@ def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_parameters(): repository='repo', storage_config=storage_config, retention_config=retention_config, + local_borg_version='1.2.3', ) def test_prune_archives_with_extra_borg_options_calls_borg_with_extra_options(): retention_config = flexmock() - flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return( + flexmock(module).should_receive('make_prune_flags').with_args(retention_config).and_return( BASE_PRUNE_FLAGS ) + flexmock(module.feature).should_receive('available').and_return(False) insert_execute_command_mock(PRUNE_COMMAND + ('--extra', '--options', 'repo'), logging.INFO) module.prune_archives( @@ -254,4 +308,5 @@ def test_prune_archives_with_extra_borg_options_calls_borg_with_extra_options(): repository='repo', storage_config={'extra_borg_options': {'prune': '--extra --options'}}, retention_config=retention_config, + local_borg_version='1.2.3', )