Get started

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.

Use a privileged database role

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 on PATH, set PG_BIN to their folder.

Create a backup

bash
node scripts/backup.mjs \
  --database-url "postgresql://postgres:$POSTGRES_PASSWORD@db-host:5432/byterings" \
  --storage /path/to/media \
  --out /srv/backups

This creates /srv/backups/<timestamp>/ with:

  • database.dump: compressed pg_dump archive
  • storage.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).

WhatHow oftenKeep
Database + mediaDaily14 daily, 8 weekly
Off-site copyWeeklyEncrypted (restic, borg, or object storage with encryption)
Restore drillQuarterlyWrite down the result

Backups contain personal data and password hashes. Encrypt them and restrict access.

Restore

  1. Create a new, empty database owned by the application role (the role must exist):

    bash
    createdb -U postgres -O byterings byterings_restored
  2. Restore:

    bash
    node 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
  3. Point DATABASE_URL and 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:

bash
PG_BIN=/usr/lib/postgresql/17/bin pnpm --filter @bytecontent/api test -- restore-drill

A backup that has never been restored is only a hope. Restore real production backups into a scratch environment regularly.

ByteContent by ByteRings is open source under the MIT License. Edit on GitHub