Only trigger "on_error" hooks and monitoring failures for "prune", "create", and "check" actions, and not for other actions (#270).

This commit is contained in:
Dan Helfman 2019-12-20 13:58:02 -08:00
parent 6bfa0783b9
commit 911668f0c8
4 changed files with 15 additions and 11 deletions

2
NEWS
View file

@ -2,6 +2,8 @@
* #268: Override particular configuration options from the command-line via "--override" flag. See * #268: Override particular configuration options from the command-line via "--override" flag. See
the documentation for more information: the documentation for more information:
https://torsion.org/borgmatic/docs/how-to/make-per-application-backups/#configuration-overrides 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 * When pruning with verbosity level 1, list pruned and kept archives. Previously, this information
was only shown at verbosity level 2. was only shown at verbosity level 2.

View file

@ -52,9 +52,10 @@ def run_configuration(config_filename, config, arguments):
borg_environment.initialize(storage) borg_environment.initialize(storage)
encountered_error = None encountered_error = None
error_repository = '' error_repository = ''
prune_create_or_check = {'prune', 'create', 'check'}.intersection(arguments)
try: try:
if {'prune', 'create', 'check'}.intersection(arguments): if prune_create_or_check:
dispatch.call_hooks( dispatch.call_hooks(
'ping_monitor', 'ping_monitor',
hooks, hooks,
@ -139,7 +140,7 @@ def run_configuration(config_filename, config, arguments):
'{}: Error running post-backup hook'.format(config_filename), error '{}: Error running post-backup hook'.format(config_filename), error
) )
if encountered_error: if encountered_error and prune_create_or_check:
try: try:
command.execute_hook( command.execute_hook(
hooks.get('on_error'), hooks.get('on_error'),

View file

@ -57,10 +57,10 @@ tests](https://torsion.org/borgmatic/docs/how-to/extract-a-backup/).
## Error hooks ## Error hooks
When an error occurs during a backup or another action, borgmatic can run When an error occurs during a `prune`, `create`, or `check` action, borgmatic
configurable shell commands to fire off custom error notifications or take can run configurable shell commands to fire off custom error notifications or
other actions, so you can get alerted as soon as something goes wrong. Here's take other actions, so you can get alerted as soon as something goes wrong.
a not-so-useful example: Here's a not-so-useful example:
```yaml ```yaml
hooks: hooks:
@ -91,10 +91,11 @@ here:
* `output`: output of the command that failed (may be blank if an error * `output`: output of the command that failed (may be blank if an error
occurred without running a command) occurred without running a command)
Note that borgmatic runs the `on_error` hooks for any action in which an error Note that borgmatic runs the `on_error` hooks only for `prune`, `create`, or
occurs, not just the `create` action. But borgmatic does not run `on_error` `check` actions or hooks in which an error occurs, and not other actions.
hooks if an error occurs within a `before_everything` or `after_everything` borgmatic does not run `on_error` hooks if an error occurs within a
hook. For more about hooks, see the [borgmatic hooks `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/), documentation](https://torsion.org/borgmatic/docs/how-to/add-preparation-and-cleanup-steps-to-backups/),
especially the security information. especially the security information.

View file

@ -119,7 +119,7 @@ def test_run_configuration_logs_on_error_hook_error():
).and_return(expected_results[1:]) ).and_return(expected_results[1:])
flexmock(module).should_receive('run_actions').and_raise(OSError) flexmock(module).should_receive('run_actions').and_raise(OSError)
config = {'location': {'repositories': ['foo']}} 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)) results = list(module.run_configuration('test.yaml', config, arguments))