# backups A zds host keeps two kinds of state: the SQLite database (accounts, repos, events, OAuth sessions, everything but media) and the blobstore (uploaded media, one file per blob). The container image can stream the database to an S3-compatible bucket with [Litestream](https://litestream.io), and zds can keep a copy of every blob in a bucket with its [blob mirror](#blob-mirror). Both can share one bucket. ## database replication The image's command, [`tools/container-start.sh`](https://tangled.org/zat.dev/zds/blob/main/tools/container-start.sh), runs zds directly unless `ZDS_LITESTREAM_BUCKET` is set. With it set, Litestream starts zds as its child process, ships each change in the database's WAL to the bucket, and syncs once more after zds exits. zds itself does not change: it does not know Litestream is there, and Litestream reads the database through SQLite's own locks. The settings are in [`config/litestream.yml`](https://tangled.org/zat.dev/zds/blob/main/config/litestream.yml). | setting | meaning | default | | --- | --- | --- | | `ZDS_LITESTREAM_BUCKET` | bucket to replicate to; turns replication on | unset (off) | | `ZDS_LITESTREAM_PATH` | path of the replica inside the bucket | `zds.sqlite3` | | `ZDS_LITESTREAM_SYNC_INTERVAL` | how often new WAL pages are uploaded | `10s` | | `ZDS_LITESTREAM_RETENTION` | how far back a point-in-time restore can go | `168h` | | `ZDS_LITESTREAM_RESTORE_IF_MISSING` | restore the replica when the database file is missing | `false` | | `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` | bucket credentials | | | `AWS_ENDPOINT_URL_S3`, `AWS_REGION` | S3 endpoint and region; empty endpoint means AWS | `auto` region | The sync interval bounds what a lost volume costs: writes from the last interval are gone. It also sets the request bill, since a busy host uploads once per interval. At 10 seconds that is at most about 260,000 uploads a month, plus a daily snapshot and Litestream's compactions. Retention keeps a week of daily snapshots plus the changes between them. Any S3-compatible store works. Cloudflare R2 is the example here: it charges nothing for egress, so a restore costs only its requests. Create a bucket and an R2 API token with Object Read & Write on that bucket, then give the host the token's access key and secret along with the bucket's endpoint: ```sh ZDS_LITESTREAM_BUCKET=zds-backups AWS_ENDPOINT_URL_S3=https://.r2.cloudflarestorage.com AWS_REGION=auto AWS_ACCESS_KEY_ID= AWS_SECRET_ACCESS_KEY= ``` On Fly these are app secrets (`fly secrets set`). For AWS S3, leave `AWS_ENDPOINT_URL_S3` empty and set the bucket's region; for another store (MinIO, Backblaze B2, Tigris, Wasabi), use its endpoint and region. Changing a Fly secret restarts the machine. The log then shows Litestream's `replicating to` line, followed by `snapshot complete` once the first full copy is in the bucket. ### when the database file is missing A machine that starts with no database file is either a new install or a host that lost its volume. Litestream would replicate a fresh, empty database over the backup, so the start script checks the bucket first: - replica empty: a new install. zds starts and the first snapshot follows. - replica has a backup, `ZDS_LITESTREAM_RESTORE_IF_MISSING=true`: the backup is restored and zds starts on it. - replica has a backup, restore not asked for: the start script refuses to start and logs `refusing to replicate an empty database over it`. - the bucket cannot be read: the start script refuses to start and logs the error. A machine with its database file never checks the bucket, so a storage outage does not stop a normal boot. Leave restore-if-missing off except during a recovery, because a second machine with its own empty volume (for example after `fly scale count 2`) would otherwise start serving and replicating a second copy of the host. ## restore drill [`tools/restore-drill.sh`](https://tangled.org/zat.dev/zds/blob/main/tools/restore-drill.sh) restores the replica into a scratch directory and checks it. It only reads the bucket. Run it with the same bucket settings the host uses (and `litestream`, `sqlite3`, `curl` and `jq` on `PATH`): ```sh ZDS_LITESTREAM_BUCKET=zds-backups \ ZDS_DRILL_LIVE_URL=https://pds.zat.dev \ ZDS_DRILL_BIN=zig-out/bin/zds \ tools/restore-drill.sh ``` It reports how long the restore took and how big the copy is, runs SQLite's full integrity check and `foreign_key_check`, and counts accounts, records, commits, blocks, events and blobs. With `ZDS_DRILL_LIVE_URL` it compares each repo's newest commit with the live host's `listRepos`; a handful of repos behind by a sync interval is expected. With `ZDS_DRILL_BIN` it boots that zds against the copy on a local port, with crawlers off, mail kept in the outbox, permissioned-data workers and the blob mirror off and an empty scratch blobstore, and lists its repos. Set `ZDS_DRILL_TIMESTAMP` (RFC 3339) to restore an earlier point instead of the latest, and `ZDS_DRILL_KEEP=1` to keep the scratch directory. Run the drill after turning replication on, after a release with a schema migration, and on a schedule; a backup that has never been restored is a guess. `just smoke-backups` runs the whole loop locally against an S3 stand-in: a new install, a stop, replication while running, the refusal on an empty volume, restore-if-missing, and the drill's boot. ## recovering a lost volume 1. Stop sending traffic to the broken machine, or let it stay down. 2. Run the restore drill against the bucket to confirm the backup is sound and see how far it reaches. 3. Give the app a new, empty volume at `/data` and set `ZDS_LITESTREAM_RESTORE_IF_MISSING=true`. The machine restores the database on boot and starts zds on it. 4. With the blob mirror on, nothing more: blobs come back from the bucket as they are read. Without it, restore the blobstore from a volume snapshot (see [blobs and restores](#blobs-and-restores)). 5. Unset `ZDS_LITESTREAM_RESTORE_IF_MISSING`. Going back to an earlier point is a manual operation with no script yet: restore that point with the drill (`ZDS_DRILL_TIMESTAMP` and `ZDS_DRILL_KEEP=1`), check the copy, and put it in place of `/data/zds.sqlite3` (removing its `-wal` and `-shm` files) while zds is not running. Point `ZDS_LITESTREAM_PATH` at a new replica path before zds starts on it, so the old history stays readable and the new one starts clean. ## blob mirror With `ZDS_BLOB_MIRROR_BUCKET` set, zds copies every blob to the bucket. The files under `ZDS_BLOBSTORE_PATH` stay the primary copy and serve every read; the bucket is what a lost or restored volume gets its blobs back from. | setting | meaning | default | | --- | --- | --- | | `ZDS_BLOB_MIRROR_BUCKET` | bucket for blob copies; turns the mirror on | unset (off) | | `ZDS_BLOB_MIRROR_PREFIX` | key prefix; a blob is stored at `/` | `blobs/` | | `ZDS_BLOB_MIRROR_PATH_STYLE` | `endpoint/bucket/key` URLs instead of `bucket.endpoint/key` | `false` | | `AWS_ENDPOINT_URL_S3` | S3 endpoint, scheme and host only; required | | | `AWS_REGION` | region the requests are signed for | `auto` | | `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN` | credentials | | The endpoint, region and credentials are the same `AWS_*` settings Litestream reads, so one bucket and one R2 API token can hold both, each under its own prefix: ```sh ZDS_LITESTREAM_BUCKET=zds-backups ZDS_LITESTREAM_PATH=pds.example.com/db ZDS_BLOB_MIRROR_BUCKET=zds-backups ZDS_BLOB_MIRROR_PREFIX=pds.example.com/blobs/ ``` R2 takes both URL styles; virtual-hosted (`bucket..r2.cloudflarestorage.com`) is the default here. How it behaves: - One background thread does every bucket write, signed with SigV4. Uploads are acknowledged once the blob is on disk, as before; the thread copies new blobs within about 30 seconds. It reads one blob into memory at a time, up to `ZDS_BLOB_UPLOAD_LIMIT`. - Each blob's row in the `blobs` table records whether the bucket has it (`storage` is `disk` until it does, then `s3`). Every blob that existed before the mirror was turned on starts as `disk`, so turning it on backfills them in upload order. The startup log says how many are mirrored and how many are pending, and each round logs what it uploaded. - Before uploading, the thread checks that the file hashes to the blob's CID, so a damaged file on disk is logged and skipped instead of copied. The upload is signed with that hash too, which S3 checks against the body it receives. - When a blob's file is missing on disk, a read fetches it from the bucket, checks that it hashes to the CID, and keeps it on disk. Bytes that do not match are refused and logged as `digest_mismatch`. - Deleting a blob (blob collection or account deletion) leaves a note under `/.mirror-deletes`, and the thread deletes the bucket's copy on its next round. The notes survive a restart. - Upload failures are logged with the S3 status; after three in a row the thread waits a minute before trying again. A blob that keeps failing is retried on every pass through the table. Limits: - The volume still holds every blob; nothing is evicted to the bucket yet. - Blobs deleted while the mirror was off keep their bucket copies. - Pointing the mirror at a new bucket or prefix does not re-upload what the old one has. Reset the rows so the thread copies everything again: `UPDATE blobs SET storage = 'disk'`. ## blobs and restores Restore the database and the blobs from about the same time. A database ahead of its blobs lists media that `getBlob` cannot find, and one behind them leaves files that blob collection never reclaims, since it only deletes blobs the database knows about. With the blob mirror on, a lost volume needs only the database: restore it with Litestream, start zds on an empty blobstore, and each blob is fetched from the bucket the first time it is read. Blobs uploaded after the mirror's last round before the loss are gone. Without the mirror, a Fly volume snapshot is the only copy of the blobs.