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, or anywhere else Litestream can write, with Litestream. zds can keep a copy of every blob in an S3-compatible bucket with its blob mirror. Both can share one bucket.
database replication #
The image's command, tools/container-start.sh,
runs zds directly unless ZDS_LITESTREAM_BUCKET, ZDS_LITESTREAM_URL or
ZDS_LITESTREAM_CONFIG is set. With one of them set, Litestream starts zds as
its child process, ships each change in the database's WAL to the replica, 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. A bucket uses config/litestream.yml;
a URL uses the same snapshot, retention and sync settings.
| 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_URL |
replica URL, instead of a bucket and path; turns replication on | unset |
ZDS_LITESTREAM_CONFIG |
your own Litestream config, used instead of either; turns replication on | unset |
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:
ZDS_LITESTREAM_BUCKET=zds-backups
AWS_ENDPOINT_URL_S3=https://<account_id>.r2.cloudflarestorage.com
AWS_REGION=auto
AWS_ACCESS_KEY_ID=<R2 token access key id>
AWS_SECRET_ACCESS_KEY=<R2 token 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.
replica URLs #
ZDS_LITESTREAM_URL names the replica, path included, for Azure, Google Cloud
Storage, SFTP or any S3 store. ZDS_LITESTREAM_PATH is not used with it, and
setting it together with ZDS_LITESTREAM_BUCKET is refused. These forms are
the ones the image's Litestream (0.5.18) reads:
| store | ZDS_LITESTREAM_URL |
credentials |
|---|---|---|
| Azure Blob Storage | abs://<account>@<container>/zds |
LITESTREAM_AZURE_ACCOUNT_KEY or LITESTREAM_AZURE_SAS_TOKEN; with neither, Azure's default chain, which includes managed identity |
| Google Cloud Storage | gs://<bucket>/zds |
Google's default credentials; for a service account key, mount it and set GOOGLE_APPLICATION_CREDENTIALS to its path |
| AWS S3 | s3://<bucket>/zds |
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY |
| Cloudflare R2 | s3://<bucket>/zds?endpoint=https://<account_id>.r2.cloudflarestorage.com®ion=auto |
the same two, from an R2 token |
| SFTP | sftp://<user>:<password>@<host>:22/backups/zds |
in the URL, percent-encoded |
Quote the URL in a shell, since & ends a command. An SFTP URL holds its
password, so keep it in a secret. It also leaves the server's host key
unchecked: to check it, or to sign in with a key file, mount a config with
Litestream's host-key and key-path and point ZDS_LITESTREAM_CONFIG at it.
The config's database path must be the same as ZDS_DB.
The restore drill reads only the bucket settings. For a URL replica, restore a copy with the same credentials and check it:
litestream restore -integrity-check full -o copy.sqlite3 "$ZDS_LITESTREAM_URL"
startup log #
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 replica 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 replica cannot be read: the start script refuses to start and logs the error.
A machine with its database file never makes this check, so a storage outage does
not stop a normal boot. On a host with a volume, 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.
running on an ephemeral disk #
Where the platform has no disk SQLite can use safely (network file shares such as SMB or NFS do not count), the database can live on the container's own disk and be restored on every boot:
- Put
ZDS_DBon the local disk, configure a replica, and setZDS_LITESTREAM_RESTORE_IF_MISSING=truefor good. Each new container restores the database before zds starts. If the replica is empty, zds starts as a new install, so a wrong URL or path looks like one: check forrestore completedin the log. - A clean stop syncs once more before it exits, if the platform allows it the
time. A crash or a hard kill loses the writes since the last sync, normally
up to one
ZDS_LITESTREAM_SYNC_INTERVAL(longer if uploads were failing). - Put
ZDS_BLOBSTORE_PATHon a persistent mount. Litestream does not copy blobs, and the blob mirror writes only to S3-compatible stores. Even with the mirror on, a blob it has not copied yet is lost with the disk.
Run one container at a time, deploys included. Litestream does not stop two
containers writing to the same replica. Two that overlap each accept writes the
other never sees and can damage the backup. Rolling deploys that start the new
container before stopping the old one cause this even at one replica: Azure
Container Apps in single-revision mode, Kubernetes RollingUpdate, and Fly
bluegreen. Use a deploy that stops the old container, and waits for it to
exit, before starting the new one (Kubernetes: strategy: Recreate). Where the
platform has no such option, stop the app before each deploy.
restore drill #
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):
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. It repeats the refusal and the restore
with file:// and s3:// replica URLs and with a custom config.
recovering a lost volume #
- Stop sending traffic to the broken machine, or let it stay down.
- Run the restore drill against the bucket to confirm the backup is sound and see how far it reaches.
- Give the app a new, empty volume at
/dataand setZDS_LITESTREAM_RESTORE_IF_MISSING=true. The machine restores the database on boot and starts zds on it. - 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).
- 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 <prefix><did>/<cid> |
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:
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.<account_id>.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
blobstable records whether the bucket has it (storageisdiskuntil it does, thens3). Every blob that existed before the mirror was turned on starts asdisk, 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
<ZDS_BLOBSTORE_PATH>/.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.