From 5f87ea3ec5146f07ce639580d1752344646ff925 Mon Sep 17 00:00:00 2001 From: Nain Date: Thu, 16 Mar 2023 13:15:49 -0400 Subject: [PATCH] Add "--repository" flag to the "create" action --- borgmatic/actions/create.py | 6 +++ borgmatic/commands/arguments.py | 4 ++ tests/unit/actions/test_create.py | 74 ++++++++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/borgmatic/actions/create.py b/borgmatic/actions/create.py index c882032a..96a48521 100644 --- a/borgmatic/actions/create.py +++ b/borgmatic/actions/create.py @@ -2,6 +2,7 @@ import json import logging import borgmatic.borg.create +import borgmatic.config.validate import borgmatic.hooks.command import borgmatic.hooks.dispatch import borgmatic.hooks.dump @@ -28,6 +29,11 @@ def run_create( If create_arguments.json is True, yield the JSON output from creating the archive. ''' + if create_arguments.repository and not borgmatic.config.validate.repositories_match( + repository, create_arguments.repository + ): + return + borgmatic.hooks.command.execute_hook( hooks.get('before_backup'), hooks.get('umask'), diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index 6991db58..d5dc6af4 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -393,6 +393,10 @@ def make_parsers(): add_help=False, ) create_group = create_parser.add_argument_group('create arguments') + create_group.add_argument( + '--repository', + help='Path of specific existing repository to backup to (must be already specified in a borgmatic configuration file)', + ) create_group.add_argument( '--progress', dest='progress', diff --git a/tests/unit/actions/test_create.py b/tests/unit/actions/test_create.py index 915f0ae1..04d7abc0 100644 --- a/tests/unit/actions/test_create.py +++ b/tests/unit/actions/test_create.py @@ -5,14 +5,84 @@ from borgmatic.actions import create as module def test_run_create_executes_and_calls_hooks(): flexmock(module.logger).answer = lambda message: None - flexmock(module.borgmatic.borg.create).should_receive('create_archive') + flexmock(module.borgmatic.borg.create).should_receive('create_archive').once() flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2) flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return({}) flexmock(module.borgmatic.hooks.dispatch).should_receive( 'call_hooks_even_if_unconfigured' ).and_return({}) create_arguments = flexmock( - progress=flexmock(), stats=flexmock(), json=flexmock(), list_files=flexmock() + repository=flexmock(), + progress=flexmock(), + stats=flexmock(), + json=flexmock(), + list_files=flexmock(), + ) + global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) + + list( + module.run_create( + config_filename='test.yaml', + repository='repo', + location={}, + storage={}, + hooks={}, + hook_context={}, + local_borg_version=None, + create_arguments=create_arguments, + global_arguments=global_arguments, + dry_run_label='', + local_path=None, + remote_path=None, + ) + ) + + +def test_run_create_runs_with_select_repository(): + flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.config.validate).should_receive( + 'repositories_match' + ).once().and_return(True) + flexmock(module.borgmatic.borg.create).should_receive('create_archive').once() + create_arguments = flexmock( + repository=flexmock(), + progress=flexmock(), + stats=flexmock(), + json=flexmock(), + list_files=flexmock(), + ) + global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) + + list( + module.run_create( + config_filename='test.yaml', + repository='repo', + location={}, + storage={}, + hooks={}, + hook_context={}, + local_borg_version=None, + create_arguments=create_arguments, + global_arguments=global_arguments, + dry_run_label='', + local_path=None, + remote_path=None, + ) + ) + + +def test_run_create_bails_if_repository_does_not_match(): + flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.config.validate).should_receive( + 'repositories_match' + ).once().and_return(False) + flexmock(module.borgmatic.borg.create).should_receive('create_archive').never() + create_arguments = flexmock( + repository=flexmock(), + progress=flexmock(), + stats=flexmock(), + json=flexmock(), + list_files=flexmock(), ) global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)