Add automated Cosmos PostgreSQL backups #124

Merged
nimmo merged 1 commit from feat/cosmos-postgres-backups into main 2026-07-30 23:20:14 +01:00
Owner

Related to #123

Summary

  • add a reusable systemd service and daily timer for logical PostgreSQL backups
  • discover all non-template connectable databases at runtime
  • store globals, compressed per-database SQL dumps, and a manifest in atomically promoted backup sets
  • retain seven days locally and expose completed sets to Backrest
  • enable the service and failure monitoring on Cosmos
  • document the backup and restore model

Validation

  • nix flake check
  • nix build --no-link .#nixosConfigurations.cosmos.config.system.build.toplevel

Remaining before merge

  • deploy to Cosmos
  • confirm the timer/service and first backup set
  • verify Backrest can archive the path
  • perform a restore test against a disposable PostgreSQL instance
Related to #123 ## Summary - add a reusable systemd service and daily timer for logical PostgreSQL backups - discover all non-template connectable databases at runtime - store globals, compressed per-database SQL dumps, and a manifest in atomically promoted backup sets - retain seven days locally and expose completed sets to Backrest - enable the service and failure monitoring on Cosmos - document the backup and restore model ## Validation - `nix flake check` - `nix build --no-link .#nixosConfigurations.cosmos.config.system.build.toplevel` ## Remaining before merge - deploy to Cosmos - confirm the timer/service and first backup set - verify Backrest can archive the path - perform a restore test against a disposable PostgreSQL instance
Add automated Cosmos PostgreSQL backups
All checks were successful
Build and Push Attic Cache / test (pull_request) Successful in 42s
Build and Push Attic Cache / build (pull_request) Has been skipped
AI pull-request review / review (pull_request) Successful in 4m34s
e0b7133b4b
Author
Owner

Automated code review

Reviewed commit: e0b7133b4b87cfb1bcfd2e146d8c70d95c974fe1

Verdict: Requires further work

Resolve the blocking or important findings and investigate failed deterministic checks before merging.

Overall assessment

The PR introduces a NixOS module for daily logical PostgreSQL backups on the Cosmos host, provisioning a systemd service/timer and a shell script that stages, compresses, and atomically commits backup sets to a Backrest-managed directory. The implementation follows NixOS conventions and correctly scopes permissions, but contains minor redundancies and an edge-case failure path that should be addressed.

Blocking findings

None.

Important findings

  • Unsafe glob expansion on empty staging directory causes script failure (modules/services/postgresql-backup.nix:97; high confidence)
    The script uses set -Eeuo pipefail and executes chmod 0640 "$staging_dir"/*. If zero databases are found, the glob literal is passed to chmod, causing a non-zero exit that triggers set -e and aborts the backup.
    Impact: The backup service will fail on hosts or maintenance windows where no connectable, non-template databases exist, leaving the staging directory uncommitted and potentially triggering alerting.
    Suggested fix: Enable shopt -s nullglob before the chmod commands, or guard the operation with [ -n "$(ls -A "$staging_dir")" ] || true to safely handle empty directories.

Suggestions

  • Redundant directory creation in shell script (modules/services/postgresql-backup.nix:38; high confidence)
    The script executes install -d -m 0750 -o root -g users "$backup_root" while systemd.tmpfiles.rules already provisions ${cfg.backupPath} with identical permissions at boot.
    Impact: Minor redundancy; the explicit directory creation runs unnecessarily on every backup execution and slightly obscures the separation of concerns between NixOS provisioning and runtime logic.
    Suggested fix: Remove the install -d line from the shell script and rely entirely on systemd.tmpfiles.rules for directory provisioning.

  • Globals dump includes role passwords with restoration caveats (modules/services/postgresql-backup.nix:41; medium confidence)
    runuser -u postgres -- pg_dumpall --globals-only dumps pg_authid entries containing role passwords. The README notes restoring roles first, then databases.
    Impact: Restoring these globals into an existing cluster can overwrite current passwords or cause OID mismatches if the cluster was reinitialized. Safe restoration typically requires a fresh cluster or careful role reconciliation.
    Suggested fix: Explicitly document in the restore procedure that globals.sql.zst should only be applied to a freshly initialized cluster, or consider adding --no-passwords to the globals dump if password management is handled separately via SOPS or application configuration.

Tests and validation

  • No configured deterministic checks.

Questions

  • Does the serviceFailureMonitor require specific alerting configuration to route failures from postgresql-backup?
  • Should the backup script verify PostgreSQL connectivity or version compatibility before attempting dumps to avoid silent failures during cluster upgrades?

Review limitations

  • Review is limited to the provided diff and documentation. Runtime behavior, actual database state, and Backrest repository integration were not tested.
  • Nix evaluation and nix flake check results were not executed; findings assume standard NixOS module resolution and shell execution semantics.

Diff coverage

  • README.md: reviewed — included in a context-limited batch
  • hosts/cosmos/default.nix: reviewed — included in a context-limited batch
  • modules/services/postgresql-backup.nix: reviewed — included in a context-limited batch
Review metadata
  • Reviewer: forgejo-ai-review v1
  • Model: qwen3.6:35b-a3b-q8_0
  • Guidance loaded: .forgejo/ai-review.md, .forgejo/ai-review.yml, AGENTS.md, README.md
  • Commits considered: 1
  • Previous automated review: none found
  • Findings: 3
<!-- forgejo-ai-review:v1 --> ## Automated code review Reviewed commit: `e0b7133b4b87cfb1bcfd2e146d8c70d95c974fe1` ## Verdict: Requires further work Resolve the blocking or important findings and investigate failed deterministic checks before merging. ### Overall assessment The PR introduces a NixOS module for daily logical PostgreSQL backups on the Cosmos host, provisioning a systemd service/timer and a shell script that stages, compresses, and atomically commits backup sets to a Backrest-managed directory. The implementation follows NixOS conventions and correctly scopes permissions, but contains minor redundancies and an edge-case failure path that should be addressed. ### Blocking findings None. ### Important findings - **Unsafe glob expansion on empty staging directory causes script failure** (modules/services/postgresql-backup.nix:97; high confidence) The script uses `set -Eeuo pipefail` and executes `chmod 0640 "$staging_dir"/*`. If zero databases are found, the glob literal is passed to chmod, causing a non-zero exit that triggers `set -e` and aborts the backup. Impact: The backup service will fail on hosts or maintenance windows where no connectable, non-template databases exist, leaving the staging directory uncommitted and potentially triggering alerting. Suggested fix: Enable `shopt -s nullglob` before the chmod commands, or guard the operation with `[ -n "$(ls -A "$staging_dir")" ] || true` to safely handle empty directories. ### Suggestions - **Redundant directory creation in shell script** (modules/services/postgresql-backup.nix:38; high confidence) The script executes `install -d -m 0750 -o root -g users "$backup_root"` while `systemd.tmpfiles.rules` already provisions `${cfg.backupPath}` with identical permissions at boot. Impact: Minor redundancy; the explicit directory creation runs unnecessarily on every backup execution and slightly obscures the separation of concerns between NixOS provisioning and runtime logic. Suggested fix: Remove the `install -d` line from the shell script and rely entirely on `systemd.tmpfiles.rules` for directory provisioning. - **Globals dump includes role passwords with restoration caveats** (modules/services/postgresql-backup.nix:41; medium confidence) `runuser -u postgres -- pg_dumpall --globals-only` dumps `pg_authid` entries containing role passwords. The README notes restoring roles first, then databases. Impact: Restoring these globals into an existing cluster can overwrite current passwords or cause OID mismatches if the cluster was reinitialized. Safe restoration typically requires a fresh cluster or careful role reconciliation. Suggested fix: Explicitly document in the restore procedure that `globals.sql.zst` should only be applied to a freshly initialized cluster, or consider adding `--no-passwords` to the globals dump if password management is handled separately via SOPS or application configuration. ### Tests and validation - No configured deterministic checks. ### Questions - Does the `serviceFailureMonitor` require specific alerting configuration to route failures from `postgresql-backup`? - Should the backup script verify PostgreSQL connectivity or version compatibility before attempting dumps to avoid silent failures during cluster upgrades? ### Review limitations - Review is limited to the provided diff and documentation. Runtime behavior, actual database state, and Backrest repository integration were not tested. - Nix evaluation and `nix flake check` results were not executed; findings assume standard NixOS module resolution and shell execution semantics. ### Diff coverage - `README.md`: **reviewed** — included in a context-limited batch - `hosts/cosmos/default.nix`: **reviewed** — included in a context-limited batch - `modules/services/postgresql-backup.nix`: **reviewed** — included in a context-limited batch <details><summary>Review metadata</summary> - Reviewer: forgejo-ai-review v1 - Model: qwen3.6:35b-a3b-q8_0 - Guidance loaded: .forgejo/ai-review.md, .forgejo/ai-review.yml, AGENTS.md, README.md - Commits considered: 1 - Previous automated review: none found - Findings: 3 </details>
nimmo merged commit 185acda39d into main 2026-07-30 23:20:14 +01:00
Sign in to join this conversation.
No reviewers
No milestone
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
nimmo/nixos-config!124
No description provided.