Upgrade and backup
Upgrade
Section titled “Upgrade”An upgrade uses the same commands as Deploy: pull a known-good revision,
run just stack prod tag YES <tag>, then just stack prod up YES migrations.
A failed migration does not leave the old API serving. up exits non-zero and names the
migrator; the new API is either not started or running on the unfinished schema. Fix forward or
roll back:
just stack prod rollback YES <tag> # earlier release's images, current schemajust stack prod rollback YES <tag> <revision> # also downgrade the schema to <revision>The schema downgrade runs only when no migration in the range dropped or rewrote data. The
migration history was flattened into a9c2e4f60b18 on 2026-09-08, so older revisions cannot be
targeted. If the migrator stops on an unresolvable revision, the database predates that flatten:
bring it to a9c2e4f60b18 on a release from before the flatten, or restore from backup.
deploy/DEPLOY-PROD.md Part 3 covers recovery in detail. just stack prod down YES stops the
stack.
Before it starts anything, up checks that UID 65532 can write the three mounts the stack
writes to: the user_uploads and restic_cache volumes and the restic bind mount. Reads and
stat still succeed on a wrongly-owned mount, so without this check only uploads and backups
would fail. Docker sets a named volume’s ownership only when it creates the volume. A host whose
volumes were created by a release that ran as a different UID needs a one-time chown.
Take the safety backup before down: a manual backup does not start PostgreSQL. Switch the tag
first, so the backup runs on the new image:
env=prod # or stagingjust stack "$env" tag YES <tag>sudo chown -R 65532:65532 "${BACKUP_HOST_DIR:-./backups}" # a host bind: safe with the stack upjust backup "$env" manual # tagged, so retention cannot expire itjust stack "$env" down YES# Run as uid 0 in a container, so the host needs no knowledge of where Docker keeps volumes.for volume in user_uploads restic_cache; do docker run --rm --user 0 -v "r9lab_${env}_${volume}:/mnt" \ busybox chown -R 65532:65532 /mntdonejust stack "$env" up YES migrationsUpgrading across the R9lab rename
Section titled “Upgrading across the R9lab rename”One release renames the deployment identifiers from relab to r9lab: the Compose project
(r9lab_<env>, so every volume and network name), the database and its roles (r9lab_db,
r9lab_app, r9lab_migrator, r9lab_backup), the images (ghcr.io/cmlplatform/r9lab-*), the
scheduled jobs (r9lab-*@<env>), the RELAB_* variables (R9LAB_*) and the telemetry service
name (r9lab-api). A plain tag and up would start an empty second stack next to the old one,
so this upgrade is a short window with its own steps. Allow about an hour; copying the volumes
takes most of it.
Before the window, pull one of the new images on the host
(docker pull ghcr.io/cmlplatform/r9lab-backend:<tag>) to prove the registry is reachable, and
check there is room for a second copy of the volumes (docker system df -v).
The window, staging first. Run every step from the checkout root. On a host with a dedicated
deploy user, steps 1 and 9 run from an account with sudo, and steps 2 to 7 run as the deploy user
(for example sudo -u <deploy user> just ..., and docker and git the same way), so the checkout
and .env keep their owner. Step 8 runs on the monitoring host. The helper containers in step 4
use the same pinned Alpine image as the backup scripts:
env=prod # or stagingimg="$(sed -n 's/^ALPINE_IMAGE="${ALPINE_IMAGE:-\(.*\)}"$/\1/p' scripts/backup_restic_ops.sh)"-
Stop the scheduled jobs and wait until none is running:
Terminal window sudo systemctl disable --now "relab-backup@$env.timer" "relab-backup-maintenance@$env.timer" \"relab-watchdog@$env.timer" "relab-restore-check@$env.timer"systemctl list-units "relab-*@$env.service" --state=active # wait until this lists nothing -
Still on the old release: close traffic by stopping the tunnel, take a safety backup, record the row counts and the password type of each role, then stop the stack. With the tunnel down nothing writes, so the counts here are the ones step 5 compares against. A manual backup needs PostgreSQL running, so it comes before
down. Never pass-vtodown: the old volumes are the way back.Terminal window docker stop "relab_$env-cloudflared-1"just backup "$env" manualcount_rows() { # container name, database namedocker exec -i "$1" sh -c "psql -U \"\$POSTGRES_USER\" -d $2 -At" <<'SQL'SELECT table_name, (xpath('/row/c/text()', query_to_xml(format('SELECT count(*) AS c FROM public.%I', table_name), false, true, '')))[1]::textFROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE' ORDER BY 1;SQL}count_rows "relab_$env-postgres-1" relab_db >"rows-before-$env.txt"docker exec -i "relab_$env-postgres-1" sh -c 'psql -U "$POSTGRES_USER" -d postgres -At' <<'SQL'SELECT rolname, left(rolpassword, 5) FROM pg_authid WHERE rolname LIKE 'relab\_%';SQLjust stack "$env" down YES -
Keep a copy of the old
.env(cp -p .env .env.pre-r9lab), then check out the new release (git pull --ff-only). In the root.env, setCOMPOSE_PROJECT_NAME=r9lab_<env>and rename anyRELAB_*variable toR9LAB_*, keeping its value. Then pull the new images and write the tag:just stack "$env" tag YES <tag>. Until step 4 has created the new database and uploads volumes and the old stack is down,up,migrate,rollbackand the backup recipes refuse to run, because the data is still in therelab_<env>project. After that they run, so start nothing but PostgreSQL until step 6. -
Copy each old volume into a new one. The script refuses to run while any
relab_<env>container runs, because a volume that is in use does not copy cleanly. It creates each new volume with the labels Compose puts on its own, so Compose adopts it without a warning, and it creates no containers that a laterupwould find stopped. The copy runs as root in a container, so ownership and modes come along;restic_cacheis skipped because it rebuilds itself. The script stops at the first failure, and you can run it again: a second run copies over the first. It prints the file count, byte count and root ownership of each pair and exits non-zero when the two differ:Terminal window env="$env" img="$img" bash -euo pipefail <<'EOF'running="$(docker ps -q --filter "name=^relab_${env}-")"test -z "$running" || { echo "relab_$env containers still run: stop them first" >&2; exit 1; }summary() { # volume namedocker run --rm -v "$1:/v:ro" "$img" sh -c \'echo "$(find /v | wc -l) files, $(find /v -type f -exec stat -c %s {} + | awk "{s+=\$1} END {print s+0}") bytes, root $(stat -c "%u:%g %a" /v), marker $(cat /v/.relab-volume 2>/dev/null)"'}for v in database_data user_uploads cache_data alloy_data clamav_db; dodocker volume inspect "relab_${env}_$v" >/dev/null 2>&1 || continuedocker volume create --label "com.docker.compose.project=r9lab_$env" \--label "com.docker.compose.volume=$v" "r9lab_${env}_$v" >/dev/nulldocker run --rm -v "relab_${env}_$v:/from:ro" -v "r9lab_${env}_$v:/to" "$img" cp -a /from/. /to/old="$(summary "relab_${env}_$v")"new="$(summary "r9lab_${env}_$v")"echo "$v: $new"test "$old" = "$new" || { echo "$v differs from the old volume: $old" >&2; exit 1; }doneEOF -
Start only PostgreSQL and rename the database and roles, before anything else starts:
upcreates any role it does not find, and would create emptyr9lab_*roles next to the old ones. Each rename runs only if the old name still exists, so you can run the block again after a failure. Then compare the row counts with step 2’s; the diff must print nothing.Terminal window bash scripts/deploy_ops.sh stack "$env" compose up -d --wait postgresbash scripts/deploy_ops.sh stack "$env" compose exec -T postgres \sh -c 'psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d postgres' <<'SQL'DO $$DECLARE r text;BEGINIF EXISTS (SELECT 1 FROM pg_database WHERE datname = 'relab_db') THENALTER DATABASE relab_db RENAME TO r9lab_db;END IF;FOREACH r IN ARRAY ARRAY['app', 'migrator', 'backup'] LOOPIF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'relab_' || r) THENEXECUTE format('ALTER ROLE %I RENAME TO %I', 'relab_' || r, 'r9lab_' || r);END IF;END LOOP;END $$;SELECT count(*) AS old_roles_left FROM pg_roles WHERE rolname LIKE 'relab\_%';SQLcount_rows "r9lab_$env-postgres-1" r9lab_db | diff "rows-before-$env.txt" -If the database rename reports other sessions, run it again. If step 2 showed
md5for a role, the rename cleared its password: set it again fromsecrets/<env>/withALTER ROLE ... PASSWORD. Roles showingSCRAMkeep theirs. -
Start the stack:
just stack "$env" up YES migrations. This starts the tunnel too, so traffic reopens here. -
Check right away:
docker compose lslistsr9lab_<env>and norelab_<env>; an account created before the window signs in; an uploaded image loads;just backup "$env" manuallogs that the uploads volume identity check passed; telemetry arrives under the new names. -
On the monitoring host, in its checkout and as the user that owns it, remove
relab/<env>from the# COVERS:line ofconfig/grafana/alerting/projects.yaml, then runHC_JOBS=watchdog ./bootstrap.sh r9lab <env>. Keep thePING_WATCHDOGURL it prints for step 9, and delete the oldrelab-<env>-*checks on healthchecks.io. -
From the sudo account. If the host also moves off the relab paths (
deploy/DEPLOY-PROD.md1.7), do that move first, so the scheduled jobs are installed once, from the moved checkout. Then install them (just timers-install "$env", with the sameR9LAB_UNIT_USERas before if you use one; see Scheduling backups), setPING_WATCHDOGin/etc/r9lab/r9lab.envto the URL from step 8 (the watchdog reports the other jobs, so it is the only one needed), checksystemctl list-timers "r9lab-*@$env.timer", and remove the old unit files:Terminal window sudo rm /etc/systemd/system/relab-{backup,backup-maintenance,watchdog,restore-check}@.{service,timer}sudo systemctl daemon-reload -
Keep the
relab_<env>_*volumes for two weeks, then remove them withdocker volume rm.
Rolling back. Until up in step 6, nothing old was changed: stop the new stack with
just stack "$env" down YES, check out the commit before the rename, restore the old .env
(cp -p .env.pre-r9lab .env), re-enable the four relab-*@<env> timers, and
just stack "$env" up YES starts the old project on its untouched volumes and roles.
After up, traffic is open and the new project takes writes the old volumes never see. The same
steps still work within minutes of up, but lose everything written since. Later than that, fix
forward, or restore the new project from its backup.
First backup
Section titled “First backup”The backup container runs as UID 65532. Create the restic directory before the first backup. The
service refuses to start without it (create_host_path: false), so Docker cannot create an empty
directory that looks like a real mount.
Creating the restic repository is a separate, one-time step: backup runs never create one.
“Backup repository” in deploy/DEPLOY-PROD.md explains why.
mkdir -p "${BACKUP_HOST_DIR:-./backups}/restic"sudo chown -R 65532:65532 "${BACKUP_HOST_DIR:-./backups}"just backup-init prod # creates the repository, marks the uploads volumejust backup prod # takes the first snapshotjust restore-check prod # restores that snapshot: DB into a scratch container, uploads into a scratch dirThe repository is encrypted with secrets/prod/restic_password. Never rotate it: every later
snapshot depends on it.
Before anything destructive, take a tagged backup with just backup prod manual. The manual tag
is exempt from retention. An untagged copy is not safe: as snapshots age, retention keeps only the
newest one per calendar day, so it can expire the same day.
Scheduling backups
Section titled “Scheduling backups”Starting the stack does not schedule backups. A systemd timer on the host runs the one-shot
backup service every hour. A second, daily timer does repository upkeep: retention, the integrity
check, and the offsite copy. Install all four scheduled jobs (backup, backup-maintenance, watchdog,
restore-check) once per environment. The installer renders the committed units with this host’s
checkout path, deploy user, and just location, then enables the timers:
just timers-render # inspect what will be installedjust timers-install staging # renders, installs, enablessystemctl list-timers 'r9lab-*@staging.timer' # confirm NEXT times are scheduledThe installer also seeds /etc/r9lab/r9lab.env with empty PING_* URLs. Fill in PING_WATCHDOG
with a healthchecks.io check URL. The hourly watchdog reports every other
job’s state through it; the other three URLs are optional per-job checks. Until you fill it in, job
failures are invisible outside the host, and just watchdog <env> reports that. So does a host
without the timers.
With a dedicated deploy user, run the installer from an account with sudo:
R9LAB_UNIT_USER=<user> just timers-install <env>.
Optional WebDAV offsite backups
Section titled “Optional WebDAV offsite backups”The offsite copy is a second restic repository, copied from the local one over restic’s rclone backend. Do not mirror the repository directory with rsync or rclone yourself.
-
Write
secrets/<env>/rclone.confwith exactly one WebDAV remote. The remote’s root is the repository: the backup uses it asrclone:<remote>:, with no path.just deploy-secrets-checkwarns while the file still holds the placeholder.The daily maintenance job then copies snapshots offsite. It creates the offsite repository on its first run.
-
To copy snapshots on demand, for example right after a one-off local backup:
Terminal window just backup-offsite-copy staging