Add notify-backup-failure command
Sends a high-priority ntfy message that the nightly backup failed, naming the newest backup still on disk. It exits 1 if the message can't be delivered and 0 (with a note on stderr) when ntfy isn't configured. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6652a4d795
commit
8a9a3ca56d
4 files changed
+90
No files matched your search
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import fcntl
|
||||
import socket
|
||||
import sqlite3
|
||||
import sys
|
||||
import time
|
||||
@@ -158,3 +159,26 @@ def _check_status(cfg: Config, conn: sqlite3.Connection) -> int:
|
||||
db.save_status_alert(conn, name, level, since, level)
|
||||
print(f"kernelwatch: {level} notification sent for {name}")
|
||||
return failures
|
||||
|
||||
|
||||
def notify_backup_failure(cfg: Config) -> int:
|
||||
"""Tell the ntfy topic the nightly backup failed. Returns 1 if that couldn't be sent.
|
||||
|
||||
Run by systemd (OnFailure= on kernelwatch-backup.service), so it also covers
|
||||
failures the backup command can't report itself: a timeout, an out-of-memory
|
||||
kill, a crash. The cause is in `journalctl -u kernelwatch-backup`."""
|
||||
if not cfg.ntfy_url:
|
||||
print("kernelwatch: backup failed, but ntfy_url is not configured; no notification sent",
|
||||
file=sys.stderr)
|
||||
return 0
|
||||
latest = db.latest_backup(cfg.backup_dir)
|
||||
message = (f"The nightly kernelwatch database backup on {socket.gethostname()} failed.\n"
|
||||
f"Newest backup still on disk: {latest or 'none'}.\n"
|
||||
"Cause: journalctl -u kernelwatch-backup")
|
||||
try:
|
||||
publish(cfg, "kernelwatch: nightly backup failed", message, stale=True, tag="floppy_disk")
|
||||
except DeliveryError as exc:
|
||||
print(f"kernelwatch: backup failure notification failed: {exc}", file=sys.stderr)
|
||||
return 1
|
||||
print("kernelwatch: backup failure notification sent")
|
||||
return 0
|
||||
@@ -43,6 +43,10 @@ def cmd_test_alert(cfg, args) -> int:
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_notify_backup_failure(cfg, args) -> int:
|
||||
return alerts.notify_backup_failure(cfg)
|
||||
|
||||
|
||||
def cmd_backup(cfg, args) -> int:
|
||||
db.init(cfg.db_path)
|
||||
try:
|
||||
@@ -179,6 +183,9 @@ def main(argv: list[str] | None = None) -> int:
|
||||
p = sub.add_parser("backup", help="write a consistent copy of the database to PATH")
|
||||
p.add_argument("path")
|
||||
p.set_defaults(func=cmd_backup)
|
||||
sub.add_parser("notify-backup-failure", help="send the failed-backup notification "
|
||||
"(run by systemd when kernelwatch-backup.service fails)").set_defaults(
|
||||
func=cmd_notify_backup_failure)
|
||||
sub.add_parser("scheduled-backup", help="back up to backup_dir and prune old backups "
|
||||
"(run by kernelwatch-backup.timer)").set_defaults(func=cmd_scheduled_backup)
|
||||
|
||||
|
||||
@@ -96,6 +96,15 @@ def backup(path: str, dest: str) -> None:
|
||||
_BACKUP_NAME = re.compile(r"^kernelwatch-(\d{4})-(\d{2})-(\d{2})\.db$")
|
||||
|
||||
|
||||
def latest_backup(directory: str) -> str | None:
|
||||
"""Name of the newest dated backup in directory, or None."""
|
||||
try:
|
||||
names = [n for n in os.listdir(directory) if _BACKUP_NAME.match(n)]
|
||||
except OSError:
|
||||
return None
|
||||
return max(names, default=None)
|
||||
|
||||
|
||||
def rotate_backups(path: str, directory: str, keep_days: int, now: float | None = None) -> str:
|
||||
"""Back the database up to <directory>/kernelwatch-YYYY-MM-DD.db (UTC date),
|
||||
replacing that day's file, then delete dated backups that are keep_days or more
|
||||
|
||||
Reference in new issue
Block a user