From 893fca281621807ae3ab754f80e46f1cdb0c4a7a Mon Sep 17 00:00:00 2001 From: networkjanitor Date: Sat, 1 Aug 2020 16:08:32 +0200 Subject: [PATCH] Add before_extract and after_extract hooks --- borgmatic/commands/borgmatic.py | 16 ++++++++++++++++ borgmatic/config/schema.yaml | 16 ++++++++++++++++ tests/unit/commands/test_borgmatic.py | 11 +++++++++++ 3 files changed, 43 insertions(+) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index a8825311a..2b47e64db 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -91,6 +91,14 @@ def run_configuration(config_filename, config, arguments): 'pre-check', global_arguments.dry_run, ) + if 'extract' in arguments: + command.execute_hook( + hooks.get('before_extract'), + hooks.get('umask'), + config_filename, + 'pre-check', + global_arguments.dry_run, + ) if prune_create_or_check: dispatch.call_hooks( 'ping_monitor', @@ -165,6 +173,14 @@ def run_configuration(config_filename, config, arguments): 'post-check', global_arguments.dry_run, ) + if 'extract' in arguments: + command.execute_hook( + hooks.get('after_extract'), + hooks.get('umask'), + config_filename, + 'post-check', + global_arguments.dry_run, + ) if prune_create_or_check: dispatch.call_hooks( 'ping_monitor', diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index a30c60b44..8637153a6 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -473,6 +473,14 @@ map: before consistency checks, run once per configuration file. example: - echo "Starting checks." + before_extract: + seq: + - type: str + desc: | + List of one or more shell commands or scripts to execute + before extracting a backup, run once per configuration file. + example: + - echo "Starting extracting." after_backup: seq: - type: str @@ -497,6 +505,14 @@ map: after consistency checks, run once per configuration file. example: - echo "Finished checks." + after_extract: + seq: + - type: str + desc: | + List of one or more shell commands or scripts to execute + after extracting a backup, run once per configuration file. + example: + - echo "Finished extracting." on_error: seq: - type: str diff --git a/tests/unit/commands/test_borgmatic.py b/tests/unit/commands/test_borgmatic.py index 3ecf8c247..e9453d265 100644 --- a/tests/unit/commands/test_borgmatic.py +++ b/tests/unit/commands/test_borgmatic.py @@ -54,6 +54,17 @@ def test_run_configuration_calls_hooks_for_check_action(): list(module.run_configuration('test.yaml', config, arguments)) +def test_run_configuration_calls_hooks_for_extract_action(): + flexmock(module.borg_environment).should_receive('initialize') + flexmock(module.command).should_receive('execute_hook').twice() + flexmock(module.dispatch).should_receive('call_hooks').never() + flexmock(module).should_receive('run_actions').and_return([]) + config = {'location': {'repositories': ['foo']}} + arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'extract': flexmock()} + + list(module.run_configuration('test.yaml', config, arguments)) + + def test_run_configuration_does_not_trigger_hooks_for_list_action(): flexmock(module.borg_environment).should_receive('initialize') flexmock(module.command).should_receive('execute_hook').never()