diff --git a/NEWS b/NEWS index fe7a74a3..99a3be21 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,8 @@ * #1303: For the MariaDB hook, include only a subset of system data when dumping the "mysql" system database (or "all" databases), so the dump is actually restorable. See the documentation for more information: https://torsion.org/borgmatic/reference/configuration/data-sources/mariadb/ + * #1309: Add a "--quick-stats" flag and corresponding "quick_statistics" option for showing only + abbreviated statistics for the "create" action. Borg 1.4.5+ only. * Update the KeePassXC credential hook to support KeePassXC's secret service integration. See the documentation for more information: https://torsion.org/borgmatic/reference/configuration/credentials/keepassxc/ diff --git a/borgmatic/borg/create.py b/borgmatic/borg/create.py index 8e3e9820..1ed7b4dd 100644 --- a/borgmatic/borg/create.py +++ b/borgmatic/borg/create.py @@ -374,7 +374,9 @@ def create_archive( if json: output_log_level = None - elif config.get('list_details') or (config.get('statistics') and not dry_run): + elif config.get('list_details') or ( + (config.get('statistics') or config.get('quick_statistics')) and not dry_run + ): output_log_level = logging.ANSWER else: output_log_level = logging.INFO @@ -386,6 +388,11 @@ def create_archive( create_flags += ( (('--info',) if logger.getEffectiveLevel() == logging.INFO and not json else ()) + (('--stats',) if config.get('statistics') and not json and not dry_run else ()) + + ( + ('--quick-stats',) + if config.get('quick_statistics') and not json and not dry_run + else () + ) + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) and not json else ()) + (('--progress',) if config.get('progress') else ()) + (('--json',) if json else ()) diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index bce78774..13740769 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -896,6 +896,13 @@ def make_parsers(schema, unparsed_arguments): # noqa: PLR0915 action='store_true', help='Display statistics of archive', ) + create_group.add_argument( + '--quick-stats', + dest='quick_statistics', + default=None, + action='store_true', + help='Display statistics of archive, skipping repository-wide "All archives" and chunk index statistics [Borg 1.4.5+ only]', + ) create_group.add_argument( '--list', '--files', diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index 09f014b2..3d18c0a5 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -1091,6 +1091,15 @@ properties: Corresponds to the "--stats" flag on those actions. Defaults to false. example: true + quick_statistics: + type: boolean + description: | + Display statistics for an archive when running supported actions, + skipping the repository-wide "All archives" and chunk index + statistics to save some time. Corresponds to the "--quick-stats" + flag on those actions. Defaults to false. (This option is supported + for Borg 1.4.5+ only.) + example: true list_details: type: boolean description: | diff --git a/tests/unit/borg/test_create.py b/tests/unit/borg/test_create.py index 2e21e648..199385ff 100644 --- a/tests/unit/borg/test_create.py +++ b/tests/unit/borg/test_create.py @@ -1477,6 +1477,44 @@ def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats(): 'source_directories': ['foo', 'bar'], 'repositories': ['repo'], 'exclude_patterns': None, + 'statistics': True, + }, + patterns=[Pattern('foo'), Pattern('bar')], + local_borg_version='1.2.3', + global_arguments=flexmock(), + borgmatic_runtime_directory='/borgmatic/run', + ) + + +def test_create_archive_with_quick_stats_and_dry_run_calls_borg_without_quick_stats(): + # --dry-run and --quick-stats are mutually exclusive, see: + # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description + flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels') + flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER + flexmock(module).should_receive('make_base_create_command').and_return( + (('borg', 'create', '--dry-run'), REPO_ARCHIVE, flexmock()), + ) + flexmock(module.environment).should_receive('make_environment') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) + flexmock(module).should_receive('execute_command').with_args( + ('borg', 'create', '--dry-run', '--info', *REPO_ARCHIVE), + output_log_level=logging.INFO, + output_file=None, + borg_local_path='borg', + borg_exit_codes=None, + working_directory=None, + environment=None, + ) + insert_logging_mock(logging.INFO) + + module.create_archive( + dry_run=True, + repository_path='repo', + config={ + 'source_directories': ['foo', 'bar'], + 'repositories': ['repo'], + 'exclude_patterns': None, + 'quick_statistics': True, }, patterns=[Pattern('foo'), Pattern('bar')], local_borg_version='1.2.3', @@ -1806,6 +1844,42 @@ def test_create_archive_with_stats_and_json_calls_borg_without_stats_flag(): 'source_directories': ['foo*'], 'repositories': ['repo'], 'exclude_patterns': None, + 'statistics': True, + }, + patterns=[Pattern('foo'), Pattern('bar')], + local_borg_version='1.2.3', + global_arguments=flexmock(), + borgmatic_runtime_directory='/borgmatic/run', + json=True, + ) + + assert json_output == '[]' + + +def test_create_archive_with_quick_stats_and_json_calls_borg_without_quick_stats_flag(): + flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels') + flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER + flexmock(module).should_receive('make_base_create_command').and_return( + (('borg', 'create'), REPO_ARCHIVE, flexmock()), + ) + flexmock(module.environment).should_receive('make_environment') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) + flexmock(module).should_receive('execute_command_and_capture_output').with_args( + ('borg', 'create', '--json', *REPO_ARCHIVE), + working_directory=None, + environment=None, + borg_local_path='borg', + borg_exit_codes=None, + ).and_yield('[]') + + json_output = module.create_archive( + dry_run=False, + repository_path='repo', + config={ + 'source_directories': ['foo*'], + 'repositories': ['repo'], + 'exclude_patterns': None, + 'quick_statistics': True, }, patterns=[Pattern('foo'), Pattern('bar')], local_borg_version='1.2.3',