---
title: "Deploy runbook"
description: "Staging and production on Dokploy at The Keep, rollback, backups and restore."
url: https://docs-staging-ritorno.findmalek.engineer/docs/deploy-runbook
markdown: https://docs-staging-ritorno.findmalek.engineer/docs/deploy-runbook.md
---

# Deploy runbook

## What runs

Each environment is one Dokploy compose service built from `docker-compose.deploy.yml`: its own `postgres` (own volume), `api`, `worker` (pg-boss), `web`, `dashboard`, `docs` and `backup`. Traefik terminates tls with Let's Encrypt. Dokploy projects: `ritorno-staging`, `ritorno-production` and `ritorno-previews`.

| | staging | production |
| --- | --- | --- |
| trigger | every merge to `main` | published release (tag) |
| hosts | `staging-ritorno`, `app-staging-ritorno`, `api-staging-ritorno`, `docs-staging-ritorno` | `ritorno`, `app-ritorno`, `api-ritorno`, `docs-ritorno` |
| `SEED` | `demo` | `none` |
| `MESSAGING_LIVE` | `false` | `true` (a channel still needs its provider keys) |
| `ALLOW_PREVIEW_ORIGINS` | `true` | `false` |
| `S3_PREFIX` | `staging/` | empty |

All hosts are under `findmalek.engineer`. Health: the api answers `/health` with the commit it was built from (`sha`, the `GIT_SHA` build arg), every image ships a docker `HEALTHCHECK`, and the deploy job resolves the deployed ref to a commit and waits until `/health` reports that `sha`. After 20 minutes it fails and prints the last status and `sha` it saw. The worker starts once the api is healthy, so migrations have run.

## Secrets

App secrets come from Infisical, see [secrets](https://docs-staging-ritorno.findmalek.engineer/docs/secrets.md): the deploy job exports the `staging` or `prod` environment and writes it, with the computed urls and flags, to the Dokploy compose env before deploying. Without the `INFISICAL_*` settings the env already in Dokploy is used.

## Set up from scratch

0. optional: the Infisical settings (secrets `INFISICAL_CLIENT_ID`, `INFISICAL_CLIENT_SECRET`, variables `INFISICAL_URL`, `INFISICAL_PROJECT_ID`), see [secrets](https://docs-staging-ritorno.findmalek.engineer/docs/secrets.md)
1. an api key for a Dokploy user (Dokploy UI, profile, api keys) as repo secret `DOKPLOY_TOKEN`, plus repo variables `DOKPLOY_URL`, `PREVIEW_DOMAIN`, `DOKPLOY_ENVIRONMENT_ID` (previews environment), `STAGING_API_URL`, `PRODUCTION_API_URL`
2. `DOKPLOY_URL=... DOKPLOY_TOKEN=... TARGET=staging ENVIRONMENT_ID=<env id> node .github/scripts/dokploy-bootstrap.mjs`, then again with `TARGET=production`. It creates the compose service, a random env (secrets, admin password) and the domains, and prints the compose id. Set it as variable `DOKPLOY_STAGING_COMPOSE_ID` or `DOKPLOY_PRODUCTION_COMPOSE_ID`. Re-running keeps the env of an existing service
3. add the provider keys (Resend, Kapso, S3, GlitchTip, OpenObserve) in the compose Environment tab, the keys are listed in `docker-compose.deploy.yml`
4. DNS: a CNAME per host to the server, plus `*.preview-ritorno`

## Database names after the rename

New stacks use the role and database `ritorno` (`POSTGRES_USER` and `POSTGRES_DB` in `docker-compose.deploy.yml`, default `ritorno`). The staging and production volumes were created before the rename and still hold `le_hoetel`, so both keep `POSTGRES_USER=le_hoetel` and `POSTGRES_DB=le_hoetel` in their env (Infisical or the Dokploy Environment tab). The deploy job writes those two values itself when the env sets neither, so a deploy never points the api at a database that does not exist.

To move an environment to `ritorno`: dump the database with `pg_dump --no-owner`, set both keys to `ritorno` and `docker volume rm` the old volume (or point the service at a new one), deploy so postgres starts empty, restore the dump, then check `/health`.

## Hosts after the rename

The Ritorno hosts are added next to the old ones, nothing is removed. Once DNS for the new hosts points to the server, run the `dokploy-domains` workflow (or `TARGET=staging node .github/scripts/dokploy-domains.mjs` with the Dokploy env) for staging and production. Remove the old domains in Dokploy only after the new ones answer.

## Rollback

Run the `release` workflow by hand with an older tag. It points the production compose service at that tag and rebuilds it. Migrations only go forward: if a migration must be undone, restore the database from a backup first.

## Backups

Each environment runs a `backup` service next to its database (`docker/backup`, same postgres major as the `postgres` service, so `pg_dump` always matches the server). Every day at 03:00 UTC it runs `pg_dump -Fc` and uploads `backups/<DEPLOY_ENV>/<db>-<timestamp>.dump` to `S3_BUCKET`, with the same `S3_*` keys as the api. The dump must be non-empty and readable by `pg_restore --list` before it is uploaded, and the uploaded size must match. Only after that it deletes dumps older than 14 days, always keeping the newest 3. A failed step logs `backup: failed: <step>`, uploads and deletes nothing, and the next run is the next day. The logs show object names and sizes only. Without `S3_BUCKET` and the S3 keys it logs that backups are off and idles.

A backup on demand, for example before a risky migration:

```sh
docker exec <backup container> db-backup now
```

When you change the postgres image, change `docker/backup/Dockerfile` in the same PR.

## Restore a backup

`db-restore` (`scripts/db-restore.sh`, shipped in the backup image) downloads a dump and runs `pg_restore --clean --if-exists --no-owner` into the database you name, on the environment's postgres. It takes a database name, not a url: the connection and password come from the container's `PG*` env, so nothing secret shows in `ps`. With a terminal (`docker exec -it`) it asks you to type the database name. In production it refuses unless you pass `--prod`. Always restore into a scratch database first and check it.

1. On the server, find the backup container of the environment: `docker ps --filter name=backup`.
2. List the dumps with their size and date: `docker exec <backup container> db-restore list`.
3. Create a scratch database: `docker exec <postgres container> psql -U <POSTGRES_USER> -c 'create database restore_check'`.
4. Restore into it: `docker exec -it <backup container> db-restore latest restore_check` (production: `db-restore --prod latest restore_check`). Pass a dump name such as `ritorno-2026-11-26T030000Z.dump` instead of `latest` for an older day.
5. Check it: `docker exec <postgres container> psql -U <POSTGRES_USER> -d restore_check -c 'select count(*) from guest'` and compare with what you expect.
6. To replace the live database: stop the `api` and `worker` services in Dokploy, run step 4 with the live database name instead of `restore_check`, start them again and check `/health`. Then drop the scratch database.

Tested locally with docker compose: postgres 17, the backup image and `rclone serve s3` as the bucket. A dump was uploaded and old planted dumps were pruned down to the newest 3. A failing `pg_dump` (wrong password) uploaded and pruned nothing. `db-restore latest` brought back every row into a fresh database and again over a changed one, and refused with `DEPLOY_ENV=production` until `--prod` was passed.
