Backups & Migrations
Gatewyse ships two operational tools for protecting and evolving your data: an S3-backed backup and restore system, and a reversible schema migration runner.
Backups & Restore
Backups run as BullMQ jobs on the log-backup queue: the worker runs mongodump, uploads the archive to S3, and writes a sha256 checksum sidecar alongside it. Backups are limited to an allow-listed set of collections — audit_logs, request_logs, usage_records, documents, document_chunks — to prevent exfiltration of sensitive collections such as users or API keys.
Triggering a backup
POST /api/admin/backupsContent-Type: application/json
{ "collections": ["audit_logs", "request_logs"] }- Super-admin only. Backups can contain cross-tenant data, so the route rejects anyone without the
super-adminrole (in addition to thebackups:createpermission). - Requested collections are filtered against the allow-list; omitting
collectionsbacks up the full allowed set. - A user-supplied
s3Bucketis ignored for security — the configured default destination is always used. - Returns
202 Acceptedwith ajobId. PollGET /api/admin/backups/:jobIdfor status, orGET /api/admin/backupsto list recent jobs.
Restoring a backup
POST /api/admin/backups/:jobId/restoreContent-Type: application/json
{ "drop": false }- Super-admin only (same guard as backup).
- The source backup must be in the
completedstate and have an uploaded S3 archive; otherwise the request is rejected. - The worker downloads the archive from S3 and verifies its sha256 checksum sidecar before replaying via
mongorestore, aborting on any mismatch — a corrupted or tampered archive is never restored. "drop": truepasses--droptomongorestore, replacing the target collections (destructive). Leave itfalseto merge. Test destructive restores into a scratch database first.- Returns
202 Acceptedwith a restorejobId; pollGET /api/admin/backups/:jobIdfor progress.
Recovery objectives
RTO and RPO are deployment-specific:
- RPO (data-loss window) is the age of the most recent successful backup. Backups here are on-demand plus a scheduled
log-backupcron — set the cron cadence to your tolerable RPO (e.g. hourly ⇒ ≤ 1h RPO). - RTO (time-to-restore) is archive download plus
mongorestoretime, roughly linear in dataset size.
Schema Migrations
Schema migrations are applied with the migration runner. Each applied migration is recorded in a schema_migrations collection, so the runner knows what has already run and migrations are never applied twice.
Run it via the server package script:
pnpm --filter @ai-gateway/server migrate <command>which invokes tsx src/scripts/migrate.ts. The available subcommands are:
| Command | Effect |
|---|---|
up | Apply all pending migrations. Reports how many ran (or “Already up to date”). |
down | Revert the single most recently applied migration. |
status | Show every migration with its id, name, and whether it has been applied (and when). This is the default when no subcommand is given. |
# Show applied / pending migrationspnpm --filter @ai-gateway/server migrate status
# Apply all pending migrationspnpm --filter @ai-gateway/server migrate up
# Revert the most recent migrationpnpm --filter @ai-gateway/server migrate downAn unknown subcommand exits non-zero with a usage message (Use up | down | status.). Run status before and after up/down to confirm the applied set.