Notify via ntfy when the nightly backup fails #18

Merged
patrick merged 2 commits from feat/backup-failure-alert into main 2026-10-30 15:16:58 +00:00
6 changed files with 65 additions and 1 deletions
Showing only changes of commit 48960e6faf - Show all commits

No files matched your search

+12
View File
@@ -465,6 +465,7 @@ sudo kernelwatch remove-user USER
sudo kernelwatch list-users
sudo kernelwatch backup PATH write a consistent copy of the database (safe while running)
sudo kernelwatch scheduled-backup dated copy in backup_dir, then prune old ones (what the timer runs)
sudo kernelwatch notify-backup-failure send the failed-backup notification now (also runs on failure)
sudo kernelwatch check-alerts check for stale, recovered, reboot and update alerts now
sudo kernelwatch test-alert send a test notification to the configured ntfy topic
```
@@ -505,6 +506,17 @@ journalctl -u kernelwatch-backup # a failed run shows up here
sudo systemctl start kernelwatch-backup # back up now
```
If a run fails, whether from an error, the 10-minute timeout, an out-of-memory
kill or a crash, systemd starts `kernelwatch-backup-failed.service`
(`OnFailure=`), which sends a high-priority message to your ntfy topic naming the
newest backup still on disk. The cause is in `journalctl -u kernelwatch-backup`.
Without `ntfy_url` it only logs that it had nothing to send to. If ntfy itself is
unreachable at that moment, the failure shows up only in the journal
(`journalctl -u kernelwatch-backup-failed`). It can't tell you that the timer
never ran at all, for example because the host was down; that needs a heartbeat
monitor. You can check delivery with
`sudo kernelwatch notify-backup-failure`, which sends the same message.
These copies sit on the dashboard host, so they protect against a corrupted or
mistakenly changed database but not against losing the machine. Copy
`/var/lib/kernelwatch/backups/` elsewhere with whatever you already back up with.
+2 -1
View File
@@ -45,7 +45,8 @@ install -d -m 0750 -o kernelwatch -g kernelwatch "$root/var/lib/kernelwatch"
install -m 0644 "$here/kernelwatch.service" "$root/etc/systemd/system/"
install -m 0644 "$here/kernelwatch-alerts.service" "$here/kernelwatch-alerts.timer" \
"$here/kernelwatch-backup.service" "$here/kernelwatch-backup.timer" "$root/etc/systemd/system/"
"$here/kernelwatch-backup.service" "$here/kernelwatch-backup.timer" \
"$here/kernelwatch-backup-failed.service" "$root/etc/systemd/system/"
systemctl daemon-reload
systemctl enable kernelwatch.service
systemctl restart kernelwatch.service
+38
View File
@@ -0,0 +1,38 @@
[Unit]
Description=Notify that the kernelwatch backup failed
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
User=kernelwatch
Group=kernelwatch
Environment=KERNELWATCH_CONFIG=/etc/kernelwatch/server.toml
ExecStart=/usr/bin/python3 -I /opt/kernelwatch/kernelwatch-server notify-backup-failure
TimeoutStartSec=2min
StateDirectory=kernelwatch
StateDirectoryMode=0750
CapabilityBoundingSet=
AmbientCapabilities=
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
PrivateDevices=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectKernelLogs=yes
ProtectControlGroups=yes
ProtectClock=yes
ProtectHostname=yes
ProtectProc=invisible
RestrictNamespaces=yes
RestrictRealtime=yes
RestrictSUIDSGID=yes
LockPersonality=yes
MemoryDenyWriteExecute=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
SystemCallArchitectures=native
SystemCallFilter=@system-service
UMask=0077
+3
View File
@@ -1,5 +1,8 @@
[Unit]
Description=Back up the kernelwatch database
# Any failure, including the timeout, an OOM kill or a crash, sends an ntfy
# message (a no-op if ntfy isn't configured).
OnFailure=kernelwatch-backup-failed.service
[Service]
Type=oneshot
+1
View File
@@ -18,6 +18,7 @@ report_retention_days = 30
# and deletes copies this many days old or older (0 keeps them all). Keep the
# directory under /var/lib/kernelwatch; the unit can't write elsewhere. Copy the
# files off this machine yourself: they don't protect against losing it.
# A failed backup run sends an ntfy message (if ntfy_url is set).
backup_dir = "/var/lib/kernelwatch/backups"
backup_keep_days = 7
+9
View File
@@ -329,6 +329,15 @@ class ServerInstallTest(InstallCase):
for command in commands:
self.assertRegex(help_text, rf"\b{re.escape(command)}\b")
def test_on_failure_targets_are_shipped_units(self):
"""A typo'd OnFailure= is silently ignored by systemd, so check the names."""
units = {n for n in os.listdir(SERVER_DIR) if n.endswith((".service", ".timer"))}
targets = set()
for unit in units:
targets.update(re.findall(r"^OnFailure=(\S+)$", read(os.path.join(SERVER_DIR, unit)), re.M))
self.assertIn("kernelwatch-backup-failed.service", targets)
self.assertEqual(sorted(targets - units), [])
def test_existing_user_is_not_recreated(self):
self.install(KW_ID_RC="0")
self.assertFalse([c for c in self.calls() if c.startswith("useradd")])