feat(backup): encrypted nightly backup of the bot service's state (#11) #15

Manually merged
bryan merged 3 commits from n11-2-pi-bots-backup into main 2026-11-10 18:56:11 +00:00
Owner

Part of #11 (N11.2): an encrypted nightly backup of the bot service's state to the NAS, with a failure alert.

What changes

  • services.pi-bots-backup (new, modules/services/pi-bots-backup.nix, scripts/pi-bots-backup.sh): a user LaunchAgent at 03:30 archives the bot service's state directory (SQLite key stores and journals through the online backup, quick_checked and folded out of WAL; sessions; sync positions) and its secrets directory into one tar, compresses it with zstd and encrypts it with age to the configured public keys on local disk. Only the public key is in the repository; the identity stays on the Studio and is used only to check each archive opens before it is published. Publishing follows hermes-nas-backup: live smbfs check, .partial-<stamp> then read-back checksum then rename, retention by stamp name (14). Every step on the share runs under timeout.
  • Failure alert through Prometheus, not a chat send: the run writes pi-bots-backup.prom for node_exporter's textfile collector; PiBotsBackupFailed, PiBotsBackupOverdue (no success for 26 h) and PiBotsBackupMissing (never reported).
  • services.nas-mounts (new, modules/services/nas-mounts.nix, scripts/nas-mount.sh): a user LaunchAgent keeps the backup share mounted (mount_smbfs -N, password from the login keychain, soft,nobrowse), at login, every 5 minutes and on network changes. It never mounts over files or another mount. Studio mounts it at ~/NAS/Bryan, and the Hermes backup and the NAS write probe move to that path.
  • Docs: modules/services/AGENTS.md, README.

Tests

  • python3 scripts/test_pi_bots_backup.py (42): the backup against stand-in state and shares (encrypted, opens only with its key, nothing readable on the share, WAL contents kept and databases standing alone, sockets/locks left out, symlinks and non-ASCII names kept, a killed run's working directory swept, unmounted/non-SMB/not-its-own-device/read-only/corrupting/hanging shares, retention never following links, locks, metrics), the mount script against a stand-in mount_smbfs, the module's agents and refusals, and promtool test rules for the three alerts.
  • test_nas_storage_health.py and test_hermes_nas_backup.py pass; darwin-rebuild build --flake .#studio builds.
  • Run for real from the built plists: the mount script mounted the share at the new path, and the backup published an archive that decrypts, with every database passing quick_check.

Review

A visible read-only Pi reviewer (Opus 5.5, medium) found three should-fixes (a killed run's working directory not swept; a symlink failing the member check; the device-ID guard untested) and eight nits, then one more should-fix after the first correction (non-ASCII names in the member check). All were fixed with tests that fail under mutation, except two nits left by agreement (a torn last line in a live session file, for the restore test; a harmless tar warning on sockets). Approved on 01acd25.

Part of #11 (N11.2): an encrypted nightly backup of the bot service's state to the NAS, with a failure alert. ## What changes - **`services.pi-bots-backup`** (new, `modules/services/pi-bots-backup.nix`, `scripts/pi-bots-backup.sh`): a user LaunchAgent at 03:30 archives the bot service's state directory (SQLite key stores and journals through the online backup, `quick_check`ed and folded out of WAL; sessions; sync positions) and its secrets directory into one tar, compresses it with zstd and encrypts it with `age` to the configured public keys on local disk. Only the public key is in the repository; the identity stays on the Studio and is used only to check each archive opens before it is published. Publishing follows `hermes-nas-backup`: live smbfs check, `.partial-<stamp>` then read-back checksum then rename, retention by stamp name (14). Every step on the share runs under `timeout`. - **Failure alert through Prometheus**, not a chat send: the run writes `pi-bots-backup.prom` for node_exporter's textfile collector; `PiBotsBackupFailed`, `PiBotsBackupOverdue` (no success for 26 h) and `PiBotsBackupMissing` (never reported). - **`services.nas-mounts`** (new, `modules/services/nas-mounts.nix`, `scripts/nas-mount.sh`): a user LaunchAgent keeps the backup share mounted (`mount_smbfs -N`, password from the login keychain, `soft,nobrowse`), at login, every 5 minutes and on network changes. It never mounts over files or another mount. Studio mounts it at `~/NAS/Bryan`, and the Hermes backup and the NAS write probe move to that path. - Docs: `modules/services/AGENTS.md`, README. ## Tests - `python3 scripts/test_pi_bots_backup.py` (42): the backup against stand-in state and shares (encrypted, opens only with its key, nothing readable on the share, WAL contents kept and databases standing alone, sockets/locks left out, symlinks and non-ASCII names kept, a killed run's working directory swept, unmounted/non-SMB/not-its-own-device/read-only/corrupting/hanging shares, retention never following links, locks, metrics), the mount script against a stand-in `mount_smbfs`, the module's agents and refusals, and `promtool test rules` for the three alerts. - `test_nas_storage_health.py` and `test_hermes_nas_backup.py` pass; `darwin-rebuild build --flake .#studio` builds. - Run for real from the built plists: the mount script mounted the share at the new path, and the backup published an archive that decrypts, with every database passing `quick_check`. ## Review A visible read-only Pi reviewer (Opus 5.5, medium) found three should-fixes (a killed run's working directory not swept; a symlink failing the member check; the device-ID guard untested) and eight nits, then one more should-fix after the first correction (non-ASCII names in the member check). All were fixed with tests that fail under mutation, except two nits left by agreement (a torn last line in a live session file, for the restore test; a harmless tar warning on sockets). Approved on `01acd25`.
services.pi-bots-backup archives the bot service's state and secrets
nightly, encrypted with age on the Studio before it reaches the NAS share,
published atomically with retention, and reports to Prometheus through a
textfile metric (PiBotsBackupFailed, PiBotsBackupOverdue,
PiBotsBackupMissing).

services.nas-mounts keeps the Bryan share mounted at ~/NAS/Bryan from the
login keychain; the Hermes backup and the NAS write probe move to it.
bryan manually merged commit 0c740ac3d7 into main 2026-11-10 18:56:11 +00:00
Sign in to join this conversation.
No reviewers
No labels
ready-for-agent
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bryan/nix-configs!15
No description provided.