Backup and restore#
Take one backup that brings back the whole gateway: its people, keys, settings, spend and the provider credentials it holds.
Use the local-storage commands for the Docker image quickstart or compose.yaml. Use the Postgres and Redis commands for compose.self-host.yaml. Block new client traffic, wait for requests to finish, and keep the backup on private, encrypted storage.
Back up local storage#
umask 077
container=tokedex-gateway
volume=$(docker inspect --format '{{range .Mounts}}{{if and (eq .Type "volume") (eq .Destination "/home/node/.tokedex")}}{{.Name}}{{end}}{{end}}' "$container")
test -n "$volume" &&
mkdir tokedex-backup &&
docker inspect --format '{{.Image}}' "$container" > tokedex-backup/image-id &&
docker stop "$container" &&
docker run --rm --network none -v "$volume:/source:ro" alpine:3.20 \
tar -czf - -C /source . > tokedex-backup/state.tgz &&
docker start "$container"Use this for a gateway with local storage started by the Docker image quickstart or Docker Compose quickstart. With Compose, run docker compose ps --all -q gateway from the project directory and use that container ID for container. The command discovers the actual volume name, including any Compose project prefix.
The gateway restarts when the archive succeeds. If the backup fails after the container stops, run docker start "$container" to bring it back, then retry with a fresh backup directory.
Back up Postgres and Redis#
Run from the Compose project directory with your usual environment. These commands stop every gateway replica, dump Postgres, and stop Redis before archiving its append-only files and the gateway volumes.
(
set -eu
umask 077
shared() { docker compose -f compose.self-host.yaml "$@"; }
gateway=$(shared ps -q gateway | head -n 1)
test -n "$gateway"
project=$(docker inspect -f '{{index .Config.Labels "com.docker.compose.project"}}' "$gateway")
mkdir tokedex-shared-backup
docker inspect -f '{{.Image}}' "$gateway" > tokedex-shared-backup/image-id
shared stop gateway
shared exec -T postgres sh -ec 'pg_dump -Fc -U "$POSTGRES_USER" -d "$POSTGRES_DB"' \
> tokedex-shared-backup/postgres.dump
shared exec -T redis redis-cli SAVE
shared stop redis
for name in state-data kek-data auth-data spend-unwritten redis-data; do
volume="${project}_${name}"
docker volume inspect "$volume" > /dev/null
docker run --rm --network none -v "$volume:/source:ro" alpine:3.20 \
tar -czf - -C /source . > "tokedex-shared-backup/$name.tgz"
done
shared start --wait redis gateway
)Use the same Compose project options you normally pass to docker compose. If a step fails while the services are stopped, restart them with docker compose -f compose.self-host.yaml start --wait redis gateway, then retry with a fresh backup directory.
umask 077
helm upgrade tokedex ./helm/tokedex -n tokedex -f release-values.yaml \
--set replicaCount=0 --set autoscaling.enabled=false --wait --timeout 10m
kubectl -n tokedex wait --for=delete pod --timeout=10m \
-l app.kubernetes.io/instance=tokedex,app.kubernetes.io/component=gateway
# Take an on-demand snapshot of your managed Postgres now.
# Take a Redis snapshot now if you need stored conversations to survive.
kubectl -n tokedex apply -f volume-pod.yaml
kubectl -n tokedex wait --for=condition=Ready pod/tokedex-volume --timeout=5m
kubectl -n tokedex exec tokedex-volume -- tar -czf - -C /state . > tokedex-data.tgz
kubectl -n tokedex delete pod tokedex-volume
helm upgrade tokedex ./helm/tokedex -n tokedex -f release-values.yaml --wait --timeout 10mvolume-pod.yaml mounts the gateway's volume claim in a Pod that runs as root, so the archive keeps each file's owner and mode; a namespace that enforces the restricted Pod Security Standard refuses it. Set image to the image your release runs:
apiVersion: v1
kind: Pod
metadata:
name: tokedex-volume
spec:
restartPolicy: Never
containers:
- name: shell
image: tokedex/gateway:0.1.1
command: ["sleep", "3600"]
securityContext:
runAsUser: 0
volumeMounts:
- name: state
mountPath: /state
volumes:
- name: state
persistentVolumeClaim:
claimName: tokedex-dataYou need: kubectl and helm access to the release, here tokedex in the namespace tokedex with the values it runs in release-values.yaml, a maintenance window, and private, encrypted storage: the archive holds the key that decrypts stored provider credentials. Block client traffic first.
Backup contents#
With local storage, the volume holds accounts, API keys, gateway settings, the master key, stored provider credentials and their encryption key, spend, and traces. Conversations stored in local memory end when the gateway stops.
With compose.self-host.yaml, take the Postgres dump and all five volume archives together.
| State | Where it lives |
|---|---|
| Accounts, API keys, gateway settings, encrypted provider credentials, spend, traces and audit log | Postgres |
| Master key and other gateway state files | state-data |
| Key that decrypts stored provider credentials | kek-data |
| Password pepper when supplied by a file | auth-data |
| Spend waiting to be written to the ledger | spend-unwritten |
| Stored OpenAI Responses conversations and provider key cooldowns | redis-data |
For either backup, keep the original launch settings, including the Compose .env and any deployment overrides. Keep the original values of externally supplied credentials and keys in your secret store.
Back up the database, volume claim and Secrets together. Include a Redis snapshot from the same maintenance window if you need to keep stored conversations. Turn on your managed Postgres's automated backups and point-in-time recovery.
| State | Where it lives |
|---|---|
| People, workspaces, API keys, settings, budgets, spend, traces and the audit log | Postgres; on the claim for a release without Postgres |
| Master key | TOKEDEX_MASTER_KEY in the gateway Secret, with a copy at master-key on the claim |
| Verifier root and fingerprint key | The gateway Secret; on the claim for a release without Postgres |
| Password pepper | TOKEDEX_PASSWORD_PEPPER in the gateway Secret; on the claim if the Secret has no such key |
| Key that encrypts stored provider credentials | kek/credential-kek.json on the claim, or your KMS key with custody.mode: kms |
| Spend not yet written to the database | state/spend-unwritten on the claim |
| Stored OpenAI Responses conversations and provider key cooldowns | Redis |
The claim is tokedex-data unless you set persistence.existingClaim. With every backup, record the chart revision, your values file, the image digest and every Secret the release names.
Restore into a clean place#
Restore local storage#
docker volume create tokedex-data-restore &&
docker run --rm -i --network none -v tokedex-data-restore:/state alpine:3.20 \
sh -c 'test -z "$(ls -A /state)" && tar -xzpf - -C /state' \
< tokedex-backup/state.tgz &&
docker run -d --name tokedex-gateway-restore \
-p 127.0.0.1:4001:4000 \
-v tokedex-data-restore:/home/node/.tokedex \
"$(cat tokedex-backup/image-id)"Use a new volume so the restore can run beside the original gateway. If the recorded image is absent from this host, use the same image version from your original launch settings. Reapply externally supplied gateway settings and credentials from the same backup when you start it.
Restore Postgres and Redis#
Use the same gateway image and Compose file that took the backup. Load your original Compose settings and credentials from the backup and your secret store. Use a new project name and a free loopback port so you can rehearse beside the original gateway.
(
set -eu
project=tokedex-restore
backup=tokedex-shared-backup
restore() { docker compose -p "$project" -f compose.self-host.yaml "$@"; }
test -z "$(restore ps --all -q)"
test -z "$(docker volume ls -q --filter "label=com.docker.compose.project=$project")"
docker image tag "$(cat "$backup/image-id")" "$project-gateway"
for name in state-data kek-data auth-data spend-unwritten redis-data; do
volume="${project}_${name}"
docker volume create --label "com.docker.compose.project=$project" \
--label "com.docker.compose.volume=$name" "$volume" > /dev/null
docker run --rm -i --network none -v "$volume:/state" alpine:3.20 \
sh -ec 'test -z "$(ls -A /state)" && tar -xzpf - -C /state' < "$backup/$name.tgz"
done
restore up --wait postgres redis
restore exec -T postgres sh -ec '
pg_restore --single-transaction --no-owner --no-acl -U "$POSTGRES_USER" -d "$POSTGRES_DB"
' < "$backup/postgres.dump"
TOKEDEX_GATEWAY_PORTS=4001 TOKEDEX_PUBLIC_URL=http://127.0.0.1:4001 \
TOKEDEX_DATABASE_MIGRATE=check TOKEDEX_SPEND_ALERTS_DELIVERY_INTERVAL_MS=0 \
restore up --no-build --wait gateway
)The new gateway listens on port 4001, checks the restored schema, and keeps alert delivery off during the rehearsal. Use a fresh project if you need to start the restore again.
Restore every part from the same backup, with the gateway version that took it, and keep clients away until the checks below pass. Restore beside the running release, never over it, so it stays your way back if the restore fails. Upgrade afterwards: an earlier version cannot always read state a later one wrote.
-
Restore Postgres into a new instance. For stored conversations, restore Redis into a new instance from its matching snapshot. Start with empty Redis if you can discard those conversations and provider key cooldowns.
-
Create the namespace
tokedex-restoreand its Secrets from your secret manager. Keep the original gateway Secret values, includingTOKEDEX_MASTER_KEY,TOKEDEX_FINGERPRINT_KEY,TOKEDEX_VERIFIER_ROOTandTOKEDEX_PASSWORD_PEPPERif set. Point the Postgres and Redis Secrets at the new instances. -
In a copy of
release-values.yamlnamedrestore-values.yaml, setpublicUrlandingress.hostto a restore address such asgateway-restore.example.com, andingress.tlsSecretNameto a TLS Secret for that hostname intokedex-restore, as HTTPS and reverse proxies shows. Install with no replicas:helm upgrade --install tokedex ./helm/tokedex -n tokedex-restore -f restore-values.yaml \ --set replicaCount=0 --set autoscaling.enabled=false --set postgresql.migrationMode=check -
Copy the archive into the new, empty claim:
kubectl -n tokedex-restore apply -f volume-pod.yaml kubectl -n tokedex-restore wait --for=condition=Ready pod/tokedex-volume --timeout=5m kubectl -n tokedex-restore exec -i tokedex-volume -- tar -xzpf - -C /state < tokedex-data.tgz kubectl -n tokedex-restore delete pod tokedex-volume -
Start the gateway:
helm upgrade tokedex ./helm/tokedex -n tokedex-restore -f restore-values.yaml \ --set postgresql.migrationMode=check --wait --timeout 10m
With migrationMode: check, a gateway whose schema does not match its image refuses to start and names the pending migrations. A release without Postgres skips the database restore and both migrationMode settings. To serve clients, set the production address back and run helm upgrade again.
With KMS custody, the restored release needs access to the original KMS key.
Check the restore#
export TOKEDEX_URL='http://127.0.0.1:4001'
curl -fsS "$TOKEDEX_URL/readyz"export TOKEDEX_URL='https://gateway-restore.example.com'
curl -fsS "$TOKEDEX_URL/readyz"{"ok":true,"release":{"environment":"local"},"service":"tokedex"}Check the State backends line from kubectl -n tokedex-restore logs deployment/tokedex to confirm which stores the gateway uses. Startup fails if it cannot decrypt a stored provider credential.
List models with an API key that existed at the backup. 200 shows the restored key verifies. If you get 401, check that the key was active at the time of the backup and that you restored the matching verifier root.
curl -sS -o /dev/null -w '%{http_code}\n' "$TOKEDEX_URL/v1/models" \
-H "Authorization: Bearer $TOKEDEX_API_KEY"200Sign in as an Owner and review Workspaces, Members, API keys, Providers and Audit log. Repeat every revocation and administrative change made since the backup before you let clients back in.
Important: An old backup brings back sessions, API keys and permissions revoked since it was taken, and rewinds spend and budget counters.
Recommendations#
- Back up before every upgrade. Restore the archive into a new volume if you need to go back to the earlier gateway version.
- Keep the archive in private, encrypted storage. It holds the keys that sign in as the owner and decrypt stored provider credentials.
- Rehearse a restore before an upgrade. Check that the restored gateway accepts an original API key and has the state you expect.
- Back up before every upgrade and after each change the database does not hold. That is when you first store a provider credential and whenever you change the gateway's Secrets. A restore is the only way back from an upgrade, and those two changes are not in a database backup.
- Rehearse a restore on a schedule and before each upgrade. You find a missing part, such as a Secret, before you need it.
- Keep the archive apart from the database snapshots. It holds the key that decrypts the stored provider credentials, so a leaked database snapshot alone cannot open them.
Next: Upgrades and migrations, Recover dashboard access or Troubleshooting.