Skip to content

backup(schedule): automatic backups, retention, and a proven restore path - #9

Merged
TusanHomichi merged 1 commit into
mainfrom
claude/repo-setup-complete-y2qmmk
Aug 28, 2026
Merged

TusanHomichi merged 1 commit into
mainfrom
claude/repo-setup-complete-y2qmmk

Conversation

@TusanHomichi

@TusanHomichi TusanHomichi commented Aug 28, 2026 •

Copy link
Copy Markdown
Member

Primary Issue

Closes #8

Problem And Outcome

Backups only happened when an operator remembered, nothing pruned old snapshots, and restore was undocumented file surgery. After merge, a running installation converges to a recent validated snapshot automatically, retention is bounded, and recovery from a destroyed database is one command whose mistake path is itself recoverable.

Changes

  • serve runs a background scheduler keeping the newest snapshot no older than the configured interval (default 24h, --backup-interval-hours, min 1); the due-decision is a pure tested function over snapshot mtime; failures log and retry; deliberately no disable flag (PRINCIPLES.md 8)
  • Retention prunes to the newest N (default 14, --backup-keep, min 1) only after a new snapshot validates; never deletes the last remaining snapshot
  • serve holds an exclusive OS file lock (instance/serve.lock): two servers can't share a data directory, and restore refuses while a server runs — a lock check, not a guess
  • consolebook restore <snapshot>: validate → lock → set current database aside (validated pre-restore snapshot when healthy; raw bytes marked .damaged when too corrupt to snapshot, so corruption can't block its own repair) → remove stale WAL sidecars → copy + fsync → prove the result exactly as startup would → audit
  • backup_completed / restore_completed append-only audit events, per the domain model's audit vocabulary
  • doctor warns when the newest snapshot exceeds the default interval and reuses the backup module's listing
  • ADR 0006 records the scheduling, retention, lock, and restore decisions

Scope

Verification

  • Listed the exact verification commands run below
  • Added or updated tests when behavior changed — 7 new integration tests (retention edges incl. keep=0, scheduling decisions incl. clock skew, destroyed-database round trip with the .damaged set-aside asserted, invalid-snapshot refusal, lock refusal, lock exclusivity)
  • Added or updated an ADR when a durable decision changed — ADR 0006
  • All fixtures and examples are invented; no real agency data
- cargo fmt --check
- cargo clippy --workspace --all-targets -- -D warnings
- cargo test --workspace   (35 passed across 7 targets)
- live-binary proof: automatic first snapshot within seconds of serve
  start; second server refused by the lock; restore refused while the
  server ran; then database overwritten with random bytes → doctor FAIL →
  restore → doctor fully green with the original installation identity,
  corrupt bytes preserved as .damaged

Review And Merge Notes

  • Review focus: the damaged set-aside fallback in restore.rs (found during testing: the pre-restore VACUUM INTO cannot snapshot a corrupt database, and the fix must not weaken the healthy-path guarantees), and ADR 0006's costs section
  • User or operator impact: backups now happen by default; restore is a supported command; serve refuses to double-start on one data directory

…path

Complete the Milestone 1 backup scope (ADR 0003 follow-through):

- serve runs a background scheduler that keeps the newest snapshot no
  older than the configured interval (default 24h, minimum 1h); the
  due-decision is a pure tested function over snapshot mtime, failures
  log and retry, and there is deliberately no way to disable it
- retention prunes to the newest N snapshots (default 14, minimum 1)
  only after a new snapshot validates, and never deletes the last one
- serve holds an exclusive OS file lock on instance/serve.lock: two
  servers cannot share a data directory, and restore refuses while a
  server runs — a lock check, not a guess
- consolebook restore <snapshot>: validates the snapshot, sets the
  current database aside (validated pre-restore snapshot when healthy,
  raw bytes marked .damaged when too corrupt to snapshot — corruption
  must not block its own repair), removes stale WAL sidecars, copies
  into place, fsyncs, and proves the result exactly as startup would
- backup and restore record append-only audit events, as the domain
  model requires
- doctor warns when the newest snapshot is older than the default
  interval and reuses the backup module's listing
- 7 new integration tests: retention edges, scheduling decisions,
  destroyed-database round trip (damaged set-aside asserted), invalid
  snapshot refusal, lock refusal, lock exclusivity
- ADR 0006 records scheduling, retention, lock, and restore decisions

Closes #8
@TusanHomichi
TusanHomichi merged commit 5dab269 into main Aug 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Milestone 1: automatic scheduled backups, retention, and a tested restore path

2 participants