effem-AppView BACKUPS.md
14 kB 304 lines
Markdown
at main

Effem AppView: database backups and restore #

The service keeps everything in one Postgres database: the Postgres service in the Railway project effem-appview, image postgres-ssl:18, with a 5 GB volume mounted at /var/lib/postgresql/data.

Status on 2026-11-09: none of this is set up. Checked that day with the read-only commands in section 2 and railway postgres pitr status: no backup schedule is configured, no backup exists, and point-in-time recovery is off. No dump has been taken off the platform and no restore has been rehearsed. A schedule, a first dump and one drill are tasks to finish before public launch. The database was created on 2026-11-09 and holds almost nothing yet, so the cost of waiting is small today and grows with every user.

A backup counts once it has been restored. Section 4 is the drill.

Run the railway commands from this directory, which is linked to the project.

1. What a backup protects #

Losing the database does not lose what users wrote. Their records live in their own repositories and can be indexed again, account by account. What exists only in this database is the service's own work:

Only here Table
Private reports and their resolutions reports
Account bans account_status
Takedowns the removed flag on comments and recommendations
Labels applied or imported labels
The admin audit log admin_audit
Notifications, read state and preferences notifications, notification_state, notification_prefs
Device tokens device_tokens
The catalog rows that users' records point at episode_catalog, podcast_catalog
Which accounts the service knows every table with a did column

The last two rows are less obvious. A catalog record can be fetched from Podcast Index again, but its CID covers its content, and the records users wrote carry the CID as it was when they wrote them. And a backfill needs the account's DID: with the database gone, so is the list of accounts to backfill.

Everything else can be rebuilt: the indexed records from the repositories, the counters from those, and pi_cache from Podcast Index.

2. Railway volume backups #

Railway can snapshot the volume on a schedule. The facts below are from Railway's documentation (docs.railway.com/volumes/backups and /cli/postgres) as read on 2026-11-09. The commands exist in the installed CLI (5.71.0). The two read-only ones were run against this project; the others have not been run here.

What Railway offers:

  • Backups can be made by hand or on a daily, weekly or monthly schedule, in any combination. A daily backup is kept 6 days, a weekly one 27 days, a monthly one 89 days.
  • They are incremental, and billed like volume storage for the data only they hold.
  • A backup can only be restored into the same project and environment.
  • Wiping the volume deletes all of its backups.
  • A manual backup is limited to 50% of the volume's size.

Railway's documentation names no plan requirement for volume backups. This project is on the Hobby plan and the feature has not been used here, so the first schedule set is also the test of that.

To see what is configured (read-only):

railway postgres pitr schedule list --service Postgres
railway postgres pitr backup list --service Postgres

To turn the schedule on (recommended: daily and weekly), and to take a backup by hand:

railway postgres pitr schedule set --daily --weekly --service Postgres
railway postgres pitr backup create --name before-launch --service Postgres

The same settings are in the dashboard, on the Postgres service under Backups.

Point-in-time recovery is a separate Railway feature, off for this database. When enabled it archives the write-ahead log to a Railway storage bucket and can restore a copy of the database, as a new service, to any moment in roughly the last four weeks. It is billed as bucket storage and network egress. It is the right tool for a bad migration or a mistaken DELETE between two backups. It is not needed before launch; railway postgres pitr enable --service Postgres turns it on.

3. A dump kept off the platform #

Railway's backups live inside Railway and can only be restored into this project. A pg_dump kept somewhere else covers the cases they cannot: the project deleted by mistake, an account problem, or a move to another host.

The database has no public TCP proxy, so it cannot be reached from outside the project directly. There are two ways in.

Through an SSH tunnel (preferred). railway connect opens a tunnel to a private database and, with --tunnel-only, prints the connection details and holds the tunnel open instead of starting a client:

# Terminal 1: leave this running until the dump is done.
railway connect Postgres --tunnel-only --port 15432

Railway documents its SSH access as needing an SSH key registered with your account (railway ssh keys lists them, railway ssh keys add registers one), so expect the tunnel to ask for one. This step has not been run against this database yet.

pg_dump must be at least as new as the server, which is Postgres 18. It is not installed on the Mac this was written on, so the commands below run it from the postgres:18-alpine image, which reaches the tunnel on the Mac through host.docker.internal. This part was run on 2026-11-09 against a local Postgres 18.

# Terminal 2. Paste the URL the tunnel printed, with its host changed to
# host.docker.internal, then press Return. Nothing is echoed.
read -rs DUMP_URL && export DUMP_URL

DUMP_FILE="effem-$(date -u +%Y%m%dT%H%M%SZ).dump"
docker run --rm -e DUMP_URL postgres:18-alpine \
  sh -c 'pg_dump --format=custom --no-owner --no-privileges "$DUMP_URL"' \
  > "$DUMP_FILE"

# A complete archive has one TABLE DATA entry per table: 33 as of migration 0013.
docker run --rm -i postgres:18-alpine pg_restore --list < "$DUMP_FILE" | grep -c 'TABLE DATA'

unset DUMP_URL

Then stop the tunnel with Ctrl+C.

Through a temporary TCP proxy. If the tunnel cannot be used, the database can be exposed for the length of the dump. This puts it on the public internet behind its password while the proxy exists, and the traffic is billed as egress, so remove the proxy as soon as the dump is done:

railway tcp-proxy create --port 5432 --service Postgres
railway tcp-proxy list --service Postgres          # the proxy's domain and port
# ... run the same docker pg_dump against that domain and port ...
railway tcp-proxy delete <proxy id> --yes
railway tcp-proxy list --service Postgres          # must say none are left

The user, password and database name are the ones in the service's own DATABASE_URL; only the host and port change to the proxy's. Railway's documentation describes the same thing from the dashboard: adding Public Access under the service's Settings, Networking creates the proxy and sets a DATABASE_PUBLIC_URL variable. This path has not been run here either.

Keeping the file. The dump holds everything in the database, including the free text of reports and the device tokens. Keep it encrypted and somewhere that is not Railway. Take one before launch, one before any deploy that ships a migration, and then on a schedule you will actually keep; weekly is a reasonable start. Nothing automates this today.

Backups also hold what was later deleted. An account purged from the service is still in every backup and dump taken before the purge, until the backup expires or the dump is deleted. Do not keep dumps longer than you need them.

4. The restore drill #

Do it once before launch and again whenever the way backups are taken changes. It needs Docker and this repository. The steps were run on 2026-11-09 with a dump of a local database; the checks are what to expect.

  1. Start an empty Postgres 18:

    docker run --rm -d --name effem-restore-drill \
      -e POSTGRES_USER=effem -e POSTGRES_PASSWORD=restore -e POSTGRES_DB=effem \
      -p 127.0.0.1:54329:5432 postgres:18-alpine
    
  2. Restore the newest dump into it:

    docker run --rm -i postgres:18-alpine pg_restore --no-owner --no-privileges --exit-on-error \
      --dbname 'postgres://effem:restore@host.docker.internal:54329/effem' < effem-<stamp>.dump
    
  3. Start the server against the copy, from this directory. cron mode with no CRON_SECRET means nothing reads Jetstream and nothing is backfilled, so the copy stays exactly as restored. If this directory has a .env file, its other settings still apply; move it aside if it sets CRON_SECRET.

    EFFEM_DATABASE_URL='postgres://effem:restore@localhost:54329/effem?sslmode=disable' \
    EFFEM_INGEST_MODE=cron EFFEM_BIND=127.0.0.1:8099 \
    EFFEM_AUTH_READ_TOKENS='drill-read=*' EFFEM_AUTH_ADMIN_TOKENS='drill-admin=*' \
    swift run effem-appview
    
  4. Check, in this order:

    Check Proves
    pg_restore exited with status 0 and printed no error. The archive is complete and readable.
    The server's log says database schema is current and has no applying migration line. The migration ledger came back with the data. If migrations are applied, the dump predates the code; that is fine, but note it.
    curl -s localhost:8099/_live answers with "status":"ok" and "db":true. The server can use the copy.
    curl -s -H 'Authorization: Bearer drill-admin' 'localhost:8099/xrpc/xyz.effem.admin.listReports?limit=5' returns the newest reports you expect. Data that exists only in the database came back.
    curl -s -H 'Authorization: Bearer drill-read' --get --data-urlencode 'subject=<an episode AT-URI with comments>' localhost:8099/xrpc/xyz.effem.feed.getComments returns those comments. The read path works on the copy.
    Row counts match the source at the time of the dump, for at least comments, subscriptions, reports and schema_migrations (13 rows). Nothing was dropped on the way.

    /_health on the copy answers degraded with "firehose_up":false once the dump is more than 300 seconds old. That is expected: nothing is reading Jetstream.

    To count rows on either side, use psql from the same image, with the URL in DUMP_URL as in section 3:

    docker run --rm -e DUMP_URL postgres:18-alpine sh -c 'psql "$DUMP_URL" -Atc \
      "SELECT (SELECT COUNT(*) FROM comments), (SELECT COUNT(*) FROM subscriptions), (SELECT COUNT(*) FROM reports), (SELECT COUNT(*) FROM schema_migrations)"'
    
  5. Stop the server with Ctrl+C, remove the copy with docker stop effem-restore-drill, and add a row to the log in section 7.

5. Restoring production #

Neither path has been exercised on Railway yet. Rehearse the first one on a quiet day before it is needed.

From a Railway backup. This is the fast path when the project itself is intact.

  • In the dashboard: the Postgres service, Backups, pick the backup by its date and choose Restore. Railway stages the change: a new volume with the backup's data is mounted in place of the current one, which stays in the project, unmounted. Review the staged change and deploy it.
  • From the CLI: railway postgres pitr backup restore <backup id> --service Postgres. Railway documents this as a restore in place that overwrites the database's data, so take a manual backup first.

From a dump. This is the path when the Railway backups are gone or the service is moving.

  1. Create an empty database service: railway add --database postgres, then railway service list for its name.
  2. Open a tunnel to the new service as in section 3 and restore into it with the pg_restore command from the drill.
  3. Point the AppView at it: set EFFEM_DATABASE_URL on zonal-charisma to ${{<new service name>.DATABASE_URL}}?sslmode=require, in single quotes so the shell leaves it alone. The change redeploys the service.
  4. Run the checks from "Checks after a deploy" in README.md.

After either restore, the service picks up by itself, and a few things are worth knowing:

  • The Jetstream cursor is as old as the backup. The reader resumes from it and replays what happened since, which brings back everything written after the backup.
  • If the cursor is older than Jetstream keeps, the reader either logs jetstream info with OutdatedCursor and carries on from the oldest event Jetstream still has, or logs jetstream rejected the saved cursor and starts from the present. In both cases it queues a backfill of every account the restored database knows. An account that wrote its first Effem record after the backup is not among them. Its earlier records stay missing until xyz.effem.admin.backfillUser is run for it.
  • A backfill adds and updates records. It does not remove rows for records that were deleted in the gap.
  • Whatever was created after the backup and lives only in the database is gone: reports, bans, takedowns, labels and audit rows. Check the report channel for notices newer than the backup and redo what they led to.
  • A purge done after the backup is undone: the account's rows are back. Run xyz.effem.admin.purgeAccount again for every deletion request handled since the backup. The audit rows for those purges are gone with the rest, so the requests themselves are the record of what to redo.

6. Before a deploy that ships a migration #

Migrations run by themselves when the new deployment starts, and there are no down migrations. Going back means restoring the database and deploying the previous code. So before railway up with a new file in Resources/Migrations:

railway postgres pitr backup create --name pre-0014 --service Postgres

Keep it until the deploy has been running for a day. An applied migration file must never be edited afterwards, not even a comment: the migrator compares checksums and refuses to start on a mismatch.

7. Drill log #

Date Dump taken Dump size Restore time Result and notes