Backup and restore
A backup contains the database (a pg_dump archive) and the uploaded media files, plus a manifest with checksums. Restores always go into a new, empty database.
Backups and restores must connect as a role that bypasses row-level security: the postgres superuser, or a dedicated role with BYPASSRLS and pg_read_all_data. The application role only sees one workspace at a time, so a dump made with it would be empty.
Requirements
- Node.js 22+ and the repository (the scripts are in
scripts/). - PostgreSQL client tools of the same major version as the server (
pg_dump,pg_restore,psql). If they are not onPATH, setPG_BINto their folder.
Create a backup
node scripts/backup.mjs \
--database-url "postgresql://postgres:$POSTGRES_PASSWORD@db-host:5432/byterings" \
--storage /path/to/media \
--out /srv/backupsThis creates /srv/backups/<timestamp>/ with:
database.dump: compressedpg_dumparchivestorage.tar: the media files (local storage driver)manifest.json: date, tool version, sizes and SHA-256 checksums
With STORAGE_DRIVER=s3, protect the bucket with your provider's versioning or replication, and back up the database with the command above (omit --storage).
Recommended policy
| What | How often | Keep |
|---|---|---|
| Database + media | Daily | 14 daily, 8 weekly |
| Off-site copy | Weekly | Encrypted (restic, borg, or object storage with encryption) |
| Restore drill | Quarterly | Write down the result |
Backups contain personal data and password hashes. Encrypt them and restrict access.
Restore
Create a new, empty database owned by the application role (the role must exist):
bashcreatedb -U postgres -O byterings byterings_restoredRestore:
bashnode scripts/restore.mjs \ --from /srv/backups/2026-11-14T03-00-00-000Z \ --database-url "postgresql://postgres:$POSTGRES_PASSWORD@db-host:5432/byterings_restored" \ --storage /path/to/new-media-folder \ --confirm- Point
DATABASE_URLand the media folder or volume at the restored copies and restart the API.
The restore script refuses to run without --confirm, verifies checksums first, refuses targets that already contain tables or files, restores in a single transaction, and keeps the original owners so row-level security keeps working.
Restore drills
The project includes an automated drill (apps/api/test/restore-drill.test.ts). It fills an installation with data, backs it up, restores it into an empty database, signs in, and checks records and media byte for byte. It runs in CI. Run it locally with:
PG_BIN=/usr/lib/postgresql/17/bin pnpm --filter @bytecontent/api test -- restore-drillA backup that has never been restored is only a hope. Restore real production backups into a scratch environment regularly.