diff --git a/NEWS b/NEWS index f27b9b1e2..275dcf3bd 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,8 @@ * #268: Override particular configuration options from the command-line via "--override" flag. See the documentation for more information: https://torsion.org/borgmatic/docs/how-to/make-per-application-backups/#configuration-overrides + * #270: Only trigger "on_error" hooks and monitoring failures for "prune", "create", and "check" + actions, and not for other actions. * When pruning with verbosity level 1, list pruned and kept archives. Previously, this information was only shown at verbosity level 2. diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index ce3719d80..b39668ad1 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -52,9 +52,10 @@ def run_configuration(config_filename, config, arguments): borg_environment.initialize(storage) encountered_error = None error_repository = '' + prune_create_or_check = {'prune', 'create', 'check'}.intersection(arguments) try: - if {'prune', 'create', 'check'}.intersection(arguments): + if prune_create_or_check: dispatch.call_hooks( 'ping_monitor', hooks, @@ -139,7 +140,7 @@ def run_configuration(config_filename, config, arguments): '{}: Error running post-backup hook'.format(config_filename), error ) - if encountered_error: + if encountered_error and prune_create_or_check: try: command.execute_hook( hooks.get('on_error'), diff --git a/docs/how-to/monitor-your-backups.md b/docs/how-to/monitor-your-backups.md index f2f105dab..6f6f15055 100644 --- a/docs/how-to/monitor-your-backups.md +++ b/docs/how-to/monitor-your-backups.md @@ -57,10 +57,10 @@ tests](https://torsion.org/borgmatic/docs/how-to/extract-a-backup/). ## Error hooks -When an error occurs during a backup or another action, borgmatic can run -configurable shell commands to fire off custom error notifications or take -other actions, so you can get alerted as soon as something goes wrong. Here's -a not-so-useful example: +When an error occurs during a `prune`, `create`, or `check` action, borgmatic +can run configurable shell commands to fire off custom error notifications or +take other actions, so you can get alerted as soon as something goes wrong. +Here's a not-so-useful example: ```yaml hooks: @@ -91,10 +91,11 @@ here: * `output`: output of the command that failed (may be blank if an error occurred without running a command) -Note that borgmatic runs the `on_error` hooks for any action in which an error -occurs, not just the `create` action. But borgmatic does not run `on_error` -hooks if an error occurs within a `before_everything` or `after_everything` -hook. For more about hooks, see the [borgmatic hooks +Note that borgmatic runs the `on_error` hooks only for `prune`, `create`, or +`check` actions or hooks in which an error occurs, and not other actions. +borgmatic does not run `on_error` hooks if an error occurs within a +`before_everything` or `after_everything` hook. For more about hooks, see the +[borgmatic hooks documentation](https://torsion.org/borgmatic/docs/how-to/add-preparation-and-cleanup-steps-to-backups/), especially the security information. diff --git a/tests/unit/commands/test_borgmatic.py b/tests/unit/commands/test_borgmatic.py index a2b98f37d..ccc637540 100644 --- a/tests/unit/commands/test_borgmatic.py +++ b/tests/unit/commands/test_borgmatic.py @@ -119,7 +119,7 @@ def test_run_configuration_logs_on_error_hook_error(): ).and_return(expected_results[1:]) flexmock(module).should_receive('run_actions').and_raise(OSError) config = {'location': {'repositories': ['foo']}} - arguments = {'global': flexmock(dry_run=False)} + arguments = {'global': flexmock(dry_run=False), 'create': flexmock()} results = list(module.run_configuration('test.yaml', config, arguments))