From 2cc743cf4747143b593ca2c2384da92a86193bbd Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sun, 17 Nov 2019 19:06:28 -0800 Subject: [PATCH] With "borgmatic check", consider Borg warnings as errors (#247). --- NEWS | 5 +++++ borgmatic/borg/check.py | 2 +- borgmatic/commands/borgmatic.py | 4 +++- docs/how-to/monitor-your-backups.md | 15 ++++++++------- setup.py | 2 +- tests/unit/borg/test_check.py | 4 +++- tests/unit/commands/test_borgmatic.py | 1 + 7 files changed, 22 insertions(+), 11 deletions(-) diff --git a/NEWS b/NEWS index 53ff942..3d0f664 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,8 @@ +1.4.12 + * #247: With "borgmatic check", consider Borg warnings as errors. + * Dial back the display of inline error logs a bit, so failed command output doesn't appear + multiple times in the logs (well, except for the summary). + 1.4.11 * #241: When using the Healthchecks monitoring hook, include borgmatic logs in the payloads for completion and failure pings. diff --git a/borgmatic/borg/check.py b/borgmatic/borg/check.py index 8f8da23..78f1b38 100644 --- a/borgmatic/borg/check.py +++ b/borgmatic/borg/check.py @@ -126,7 +126,7 @@ def check_archives( + (repository,) ) - execute_command(full_command) + execute_command(full_command, error_on_warnings=True) if 'extract' in checks: extract.extract_last_archive_dry_run(repository, lock_wait, local_path, remote_path) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index a9a8def..53d9049 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -390,7 +390,9 @@ def make_error_log_records(message, error=None): except CalledProcessError as error: yield log_record(levelno=logging.CRITICAL, levelname='CRITICAL', msg=message) if error.output: - yield log_record(levelno=logging.CRITICAL, levelname='CRITICAL', msg=error.output) + yield logging.makeLogRecord( + dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg=error.output) + ) yield log_record(levelno=logging.CRITICAL, levelname='CRITICAL', msg=error) except (ValueError, OSError) as error: yield log_record(levelno=logging.CRITICAL, levelname='CRITICAL', msg=message) diff --git a/docs/how-to/monitor-your-backups.md b/docs/how-to/monitor-your-backups.md index b2ecdb8..baa78e3 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, 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 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: ```yaml hooks: @@ -91,9 +91,10 @@ here: * `output`: output of the command that failed (may be blank if an error occurred without running a command) -Note that 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 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 documentation](https://torsion.org/borgmatic/docs/how-to/add-preparation-and-cleanup-steps-to-backups/), especially the security information. diff --git a/setup.py b/setup.py index 696a5af..c2cc588 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import find_packages, setup -VERSION = '1.4.11' +VERSION = '1.4.12' setup( diff --git a/tests/unit/borg/test_check.py b/tests/unit/borg/test_check.py index 4675a5e..51d430f 100644 --- a/tests/unit/borg/test_check.py +++ b/tests/unit/borg/test_check.py @@ -9,7 +9,9 @@ from ..test_verbosity import insert_logging_mock def insert_execute_command_mock(command): - flexmock(module).should_receive('execute_command').with_args(command).once() + flexmock(module).should_receive('execute_command').with_args( + command, error_on_warnings=True + ).once() def insert_execute_command_never(): diff --git a/tests/unit/commands/test_borgmatic.py b/tests/unit/commands/test_borgmatic.py index 7eb20b4..8b3c835 100644 --- a/tests/unit/commands/test_borgmatic.py +++ b/tests/unit/commands/test_borgmatic.py @@ -129,6 +129,7 @@ def test_make_error_log_records_generates_output_logs_for_message_only(): def test_make_error_log_records_generates_output_logs_for_called_process_error(): flexmock(module).should_receive('log_record').replace_with(dict) + flexmock(module.logging).should_receive('makeLogRecord').replace_with(dict) logs = tuple( module.make_error_log_records(