feat(backend): add automated database backup, restore, and rotation (#201)#237
Merged
Fatimasanusi merged 2 commits intoJun 30, 2026
Conversation
…rust-Analysis#201) Add a verifiable, rotated backup system for all persistent data sources (JSON data files + SQLite/PostgreSQL database): - backup.js: create gzipped + SHA-256-manifested snapshots, verify, rotate by age/count, optional S3 offsite upload, and an opt-in in-process scheduler - restore.js: restore from local or S3 with pre-restore safety copies - GitHub Actions workflow for daily scheduled backups to S3 - docs/backups.md with schedule, S3, retention, and restore runbook - tests covering create/verify/rotate/restore (11 new, 155 total passing) Core uses only Node built-ins; AWS SDK, pg_dump, and better-sqlite3 are loaded lazily so no new runtime dependency is required. Closes Trust-Analysis#201
|
@DaddyMord Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Verification Commands
Overview
This PR adds automated database backup, restore, and rotation to
backendso the platform's persistent data can be recovered after a disaster. It backs up every data source the backend owns — the JSON data files (data.json,webhooks.json) and the SQL database (SQLite in dev/test, PostgreSQL in prod) — into verifiable, gzipped snapshots with a SHA-256 manifest, copies them offsite to S3, and enforces a retention policy. A documented restore path covers both local and offsite copies.The core (create / verify / rotate / schedule) uses only Node built-ins. The AWS SDK,
pg_dump, andbetter-sqlite3are loaded lazily and only when configured, so no new runtime dependency is added (lockfile unchanged).Related Issue
Closes #201
Changes
⚙️ Backup System
backend/backup.jsbackups/backup-<timestamp>/containing one gzipped member per source plus amanifest.jsonof SHA-256 checksums.better-sqlite3online-backup API and PostgreSQL viapg_dump; both are optional and lazily loaded.verifyBackup) — every member must gunzip cleanly and match its manifest checksum.selectExpired/rotateBackups) by age (BACKUP_RETENTION_DAYS) and count (BACKUP_RETENTION_MAX), applied locally and in S3.BACKUP_S3_ENDPOINT).startBackupScheduler, gated byBACKUP_ENABLED).--list,--verify-latest, and a fullcreate → verify → upload → rotaterun.♻️ Restore
backend/restore.js--from-s3), verifying it first (overridable with--force).<file>.pre-restore, so every restore is reversible.psqlrestore command rather than overwriting a live DB automatically.🌐 Scheduling (cron job)
.github/workflows/backup.ymlworkflow_dispatch) that creates a verified backup, uploads it to S3, and attaches it to the run as an additional copy.🔧 Configuration & Wiring
backend/index.js— start the optional backup scheduler on boot (no-op unlessBACKUP_ENABLED=true).backend/package.json— addedbackup,backup:list,backup:verify, andrestorescripts.backend/.env.example— documented all backup/retention/S3 environment variables..gitignore— ignore*.db,backups/,restore-database.sql,*.pre-restore.📚 Documentation
docs/backups.md— backup contents, scheduling options (CI / cron / in-process), S3 setup, retention, and the full restore + disaster-recovery runbook.README.md— linked the new backup guide from the Documentation section.🧪 Tests
backend/__tests__/backup.test.js.pre-restorepreservation) and refusal of corrupt backups.Verification Results
uploadBackupToS3(+ S3-compatible endpoints)docs/backups.mdrestore runbook