11 Backup and Restore

TraxrGRC has no backup of its own. The host or platform that runs it owns backups: PostgreSQL's tools for the database, and a file or object store's for the attached files. The Disaster Recovery Guide lists everything to back up, compares the tools for each kind of deployment, and gives full restores for a lost host, the database alone, the files alone and a lost secret, with the steps that must follow any restore. This chapter covers the included scripts, object storage and a restore drill.

11.1 Server backups

deploy/backup.sh backs up one instance. Run it from the installation directory:

cd /srv/grc
./deploy/backup.sh /var/backups/grc 14

The first argument is the target directory (created if missing). The second is how many days to keep (default 14). The script:

  1. Finds the Compose project name the running stack uses (from COMPOSE_PROJECT_NAME, .env or the directory name), so it archives the uploads volume that the stack actually mounts. It stops if it cannot determine the name.
  2. Runs pg_dump inside the db container as the database owner, in PostgreSQL's custom format, to db-<stamp>.dump. The dump is a consistent snapshot; the application can keep running.
  3. Archives the uploads volume, read-only, through a temporary alpine:3.20 container to uploads-<stamp>.tar.gz, leaving out the backups and backup-work folders an earlier version may have left there.
  4. If BACKUP_AGE_RECIPIENTS is set, encrypts each file to *.age and removes the plain-text file.
  5. Deletes backup files in the target directory older than the retention period.

<stamp> is the UTC time, for example 20261016T020000Z. Files are created with mode 600. The script exits with an error and writes nothing further if any step fails, so a failed run is visible in its exit status and log.

The db container must be running, and pg_dump needs no other access. Run the script as an account that can use docker.

11.1.1 Scheduling

Run the backup nightly from cron, for example at 02:00:

0 2 * * * cd /srv/grc && ./deploy/backup.sh /var/backups/grc 14 >> /var/log/grc-backup.log 2>&1

Cron runs with a short PATH. If docker (or age) is not in /usr/bin or /bin on your server, set PATH at the top of the crontab. Check the log after the first scheduled run, and alert on a missing or failed run as you would for any other backup job.

11.1.2 Encrypting backups

To make the off-site copies unreadable without a key that never needs to be on the server, encrypt them with age:

  1. On a machine other than the server, create a key pair:

    age-keygen -o grc-backup-key.txt
    

    The command prints the public key (age1...). Store grc-backup-key.txt, the private key, in your password vault. Without it the backups cannot be restored.

  2. On the server, put the public key in a file, for example /srv/grc/backup-recipient.txt. List that file in .deploy-keep so upgrades leave it alone (see Upgrades).

  3. Install age on the server and run the backup with the recipient file:

    BACKUP_AGE_RECIPIENTS=/srv/grc/backup-recipient.txt ./deploy/backup.sh /var/backups/grc 14
    

The script encrypts as a separate step after each file is complete, so a failure in either step stops the run.

11.1.3 Copying backups off the server

A backup on the same disk as the database does not survive losing that disk or the server. Copy the backup directory to another machine or to object storage after each run, with the tool your organization already uses (rsync, restic, a cloud storage client). Keep the copy's retention at least as long as the local one, and restrict who can read it: an unencrypted dump holds every record, password hashes and the encrypted integration secrets.

11.1.4 What else to keep

A restore needs more than the backup files, chiefly .env, the release and the age private key. The Disaster Recovery Guide has the full list, and what each secret costs you if it is lost.

11.2 Restoring a server backup

deploy/restore.sh restores one backup onto a new, empty deployment. It refuses to run over a database that already has tables.

  1. Prepare the target: unpack the release into a new directory, put the original .env there, and do not start the stack.

  2. Copy the two backup files from the same run (db-<stamp>.dump and uploads-<stamp>.tar.gz, or their .age versions) to the server.

  3. Run the restore from the new installation directory:

    ./deploy/restore.sh /var/backups/grc/db-<stamp>.dump /var/backups/grc/uploads-<stamp>.tar.gz
    

    For encrypted files, pass the .age files and the private key:

    BACKUP_AGE_IDENTITY=/root/grc-backup-key.txt ./deploy/restore.sh \
      /var/backups/grc/db-<stamp>.dump.age /var/backups/grc/uploads-<stamp>.tar.gz.age
    
  4. With web and the worker off, run migrate and restore:finish, then continue with "After any restore" in the Disaster Recovery Guide. It has what to redo before people return:

    docker compose build
    docker compose run --rm migrate
    docker compose run --rm --no-deps worker ./node_modules/.bin/tsx --tsconfig tsconfig.worker.json src/lib/jobs/restore-finish.ts --confirm
    
  5. Verify the result as in steps 6 to 9 of the restore drill below.

The script:

  1. Checks that both files exist and finds the project's uploads volume the same way backup.sh does.
  2. For .age files, requires BACKUP_AGE_IDENTITY, decrypts into a temporary directory and removes it when done.
  3. Starts only the db service and waits for it to be healthy.
  4. Counts the tables in the database and stops with The database already has <n> tables. Restore only onto a new, empty deployment. if there are any.
  5. Restores the dump as the owner with pg_restore --no-owner --no-privileges --exit-on-error --single-transaction. A failure here leaves the database empty, so it can run again. Grants are left out on purpose: the application role does not exist yet, and migrate creates it and reapplies its grants on the first start.
  6. Unpacks the uploads archive into the project's uploads volume. The database has committed by now, so if this or the next step fails, the same command refuses to run again. The script says so: fix the cause, run --files-only with the archive, then run migrate and restore:finish.
  7. Removes rows an earlier version's in-app backups left, where their files are not on the restored volume, and reports how many. With STORAGE_BACKUPS set it leaves them, as it cannot see that store.

--db-only <dump> and --files-only <archive> restore one part: see the Disaster Recovery Guide.

The audit log, its hash chain and the prune anchors are restored exactly as they were, so the restored trail verifies as the original did. Users keep their passwords. Sessions and tokens revoked since the backup work again until you run restore:finish.

Keep AUTH_SECRET (and SETTINGS_ENCRYPTION_KEY, if set) the same as on the original instance. With a different key, everyone has to sign in again and the SMTP password or client secret, LDAP bind password and SAML private key read as empty until an administrator enters them again under Settings → Integrations.

11.2.1 Restoring in place on the same server

restore.sh never overwrites data, so to restore over a damaged instance on the same server you first remove its volumes. This destroys the current data: take a backup of what is there first, even if it is damaged. The first block is one command, so a failed backup stops it before anything is removed. PROJECT is this directory's own project, so it cannot name another instance on the host. If the database is too damaged to dump, take a raw copy of its volume instead, as the Disaster Recovery Guide shows.

cd /srv/grc
PROJECT="$(docker compose config | sed -n 's/^name: *//p' | head -n 1)" &&
  ./deploy/backup.sh /var/backups/grc-before-restore 3650 &&
  docker compose --profile tls down &&
  docker volume rm "${PROJECT}_pgdata18" "${PROJECT}_uploads"

Check that both volumes are gone, then restore:

./deploy/restore.sh /var/backups/grc/db-<stamp>.dump /var/backups/grc/uploads-<stamp>.tar.gz &&
  docker compose build &&
  docker compose run --rm migrate &&
  docker compose run --rm --no-deps worker ./node_modules/.bin/tsx --tsconfig tsconfig.worker.json src/lib/jobs/restore-finish.ts --confirm

Then continue with "After any restore" in the Disaster Recovery Guide, which starts the rest. The uploads volume must be removed too: the archive is unpacked over whatever the volume holds.

11.3 Files with no record

Restoring the database from an older backup leaves files added since then with no record naming them, and a failed upload can leave one too. The worker's daily comparison counts them, once they are a day old, and tells administrators when that count changes. It removes nothing, and those files keep their contents, so if the restore was meant to take back personal data or anything else sensitive, remove them yourself:

docker compose run --rm --no-deps worker ./node_modules/.bin/tsx --tsconfig tsconfig.worker.json src/lib/jobs/storage-reconcile.ts
docker compose run --rm --no-deps worker ./node_modules/.bin/tsx --tsconfig tsconfig.worker.json src/lib/jobs/storage-reconcile.ts --remove-orphans --older-than-days 90

The first lists them. The second removes those older than the age you give, at least 30 days, by each file's last-modified date, so a very recent file has to wait. That date says when the file was uploaded, not when a record last named it, so no age guarantees that no kept backup names it: an old file deleted last week is named by last week's backup. Choose an age past your oldest kept backup to narrow that, and keep the store's soft delete or versioning for the rest. A person runs this; it never runs on a schedule. It refuses when the database names no files, which --this-database-is-meant-to-be-empty overrides for the rare database that truly holds none. It refuses for 90 days after restore:finish, whatever age you give, and at any time when the store shows the database went back in time and restore:finish hasn't run, because a file uploaded since that backup has no record and may be the only copy. It also refuses when more than half the store has no record, which suggests the wrong database, unless you add --accept-large-removal. Keep the store to this instance alone: another system's files with similar generated names can't be told from these. It acts only on the store this instance writes to, never on one whose address is merely in .env. On the volume it touches only generated file names directly in the uploads folder with no record naming them, so it leaves the backups and backup-work folders and files staged by an open contract bulk load alone. Each batch is named in the audit log as storage.orphans_removed before it goes, and a local disk has no soft delete to bring one back.

11.4 Object storage

By default attached files live in the uploads volume, and this section does not apply. An instance can keep them in Azure Blob Storage, Amazon S3 or another S3-compatible store, set by the STORAGE_ settings in the Configuration Reference.

Not yet validated. Object storage, and the commands below, have been tested only against local emulators (Azurite and MinIO). They have not run on real Azure or AWS, and no real managed identity or task role has signed in. Try them on a copy first.

11.4.1 Backing up on object storage

deploy/backup.sh still dumps the database, and its archive holds only what is left on the volume. Protect the files with soft delete on Azure, or versioning with a lifecycle rule on S3, for at least as long as you keep database backups. A file the store no longer holds cannot be recovered. See the Disaster Recovery Guide. Deleted files stay in soft delete or old versions for as long as that window lasts, and your privacy documentation should say so.

11.4.2 Moving existing files

storage:migrate copies attachments and staged contract files from the uploads volume to the configured store. It leaves the volume's copies in place until you remove them yourself. Under Compose, run it as:

docker compose run --rm --no-deps worker ./node_modules/.bin/tsx --tsconfig tsconfig.worker.json src/lib/jobs/storage-migrate.ts --to s3 --dry-run

Use --to azure-blob for Azure. In order:

  1. Take a server backup, and create the container or bucket.
  2. Add the STORAGE_ settings and run docker compose up -d. New files now go to the store, and existing attachments keep opening from the volume. Keep the volume mounted.
  3. Run the command with --dry-run. It reads every file and reports what it would move and any problem, and changes nothing. If you skipped the restart in step 2, the command refuses and names what is still on the old settings (below).
  4. Run it without --dry-run.
  5. Run storage:reconcile (below), the same way, and open some files in the application.
  6. When you are satisfied, remove the volume's copies (below).

The target must be the store the instance is set to use, since a record moved anywhere else could not be opened. To move between Azure and S3, set the store you are leaving's address too (STORAGE_S3_REGION or STORAGE_AZURE_ACCOUNT_URL), set STORAGE_READ_FROM to it so its files keep opening during the move, and add --from s3 or --from azure-blob. The two stores then use the same container or bucket name. Take STORAGE_READ_FROM out when the move is done.

The application must be on the same settings. Web and worker report their STORAGE_ settings to the database, and the command compares them with its own, so this works wherever the database does. If they differ, because .env was edited and the application not restarted, the command refuses, as the application could not read what it moves. A process that has just stopped is forgotten within two minutes. --ignore-running-app goes ahead anyway; use it only when you know the process it names is gone.

While it runs, people can keep working. New uploads go straight to the store, and a file deleted during the move is removed from the store as well, including when the run dies with that file in flight: the run writes each name down before uploading, and the worker, or the next run, removes an object no record names. Orphan removal and restore:finish wait until the command finishes, using the same lock, and each says what holds it. A large instance may take some time, as files move one at a time.

A record names the store only after its file has been copied and read back with the same SHA-256. Until then it keeps opening from the volume, so the command can stop at any point. Run it again to carry on; a second run writes nothing it has already confirmed.

To stop it, press Ctrl-C once: it finishes the file in hand and reports. A store that has gone silent counts as failed after two minutes with no progress (--idle-timeout-seconds <n>, at least 5, changes that for a slow link), and five store failures in a row end the run. To stop at once, press Ctrl-C again: it exits with code 130, with no report. Without a terminal, find the container with docker ps and signal it, as ending the docker compose run client does not reach it: docker kill --signal TERM <container> does what Ctrl-C does, twice at least two seconds apart, and a plain docker kill <container> ends it outright. After any of these, or a crash, the lock frees itself within two minutes. Run the command again to carry on.

The command prints counts for each kind of file and every problem by name, exits 0 only when everything is accounted for, and writes storage.migration_started and storage.migrated to the audit log (module settings, entity fileStore).

ProblemMeaningWhat to do
corruptThe file does not match the SHA-256 on its record. It was not moved.Recover the file from a server backup, put it back, and run the command again.
missingThe source holds no such file. The record was left as it is.The same; or look into why, since it did not open before the move either.
conflictThe store holds other bytes under this name, and something relies on them. Nothing was overwritten.Compare the two copies.
failedThe store refused, went silent, or returned other bytes when read back, in which case that copy was removed. The record was left as it is.Run the command again once the store is healthy. Five store failures in a row end the run.
refusedThe record names something other than a generated file name.A restored or hand-edited row: look at it.

A staged contract file moves like any other, so a bulk load in progress can stay open.

Old in-app backups on the volume stay where they are, and the command does not move them. See "In-app backups from before this version" in the Disaster Recovery Guide.

11.4.3 Removing the volume's copies

Removal is a separate step, run by a person, and never scheduled:

docker compose run --rm --no-deps worker ./node_modules/.bin/tsx --tsconfig tsconfig.worker.json src/lib/jobs/storage-migrate.ts --to s3 --cleanup --dry-run
docker compose run --rm --no-deps worker ./node_modules/.bin/tsx --tsconfig tsconfig.worker.json src/lib/jobs/storage-migrate.ts --to s3 --cleanup

Immediately before removing each file, it reads the copy in the store back and compares SHA-256. It keeps any file a record still reads from the volume, any file the store could not confirm, and any file no record names, and exits non-zero if the store could not confirm one. It prints each name it removes. A file no record names may be one an older database backup needs, or the only copy of something uploaded after a restored backup. To remove them, add --remove-no-row --older-than-days <n>, at least 30, with the same refusals and limits as --remove-orphans, and the run names any that the target doesn't hold either. That is the only route for them once the instance writes elsewhere, as --remove-orphans acts only on the store it writes to. In the audit log, storage.source_removal_started opens the run, storage.source_removed names each batch of up to 100 files before it goes, and storage.source_removal_finished closes it with the counts and anything named that did not go. If an audit entry cannot be written, the removal stops. After this, the only way back from a lost store is your storage protection and your backups. The backups and backup-work folders are left alone.

11.4.4 Moving back to the volume

To leave object storage, run the same command the other way. In order:

  1. Take a server backup, and check the volume has room for every attached file.

  2. In .env, set STORAGE_DRIVER=local, keep the store's other settings, and add STORAGE_READ_FROM=s3 (or azure-blob), so rows not yet moved can still be read. Run docker compose up -d. New files now go to the volume.

  3. Move them, then compare and check as above:

    docker compose run --rm --no-deps worker ./node_modules/.bin/tsx --tsconfig tsconfig.worker.json src/lib/jobs/storage-migrate.ts --from s3 --to local --dry-run
    docker compose run --rm --no-deps worker ./node_modules/.bin/tsx --tsconfig tsconfig.worker.json src/lib/jobs/storage-migrate.ts --from s3 --to local
    
  4. Remove the store's copies when you are satisfied, with the same two commands and --cleanup. Files no record names stay, and it counts them. To review them, run it with --dry-run --remove-no-row --older-than-days <n>, at least 30, which also names any the target doesn't hold. Remove only what you have checked, without --dry-run. Decide before the next step, as --remove-orphans acts only on the store this instance writes to, and after that step nothing looks at this store again.

  5. Remove STORAGE_READ_FROM and the store's settings from .env, and run docker compose up -d.

Use --from azure-blob for Azure. Every check above applies in this direction too: a record names the volume only after its file has read back with the same SHA-256, and the command can stop at any point. On Azure or AWS the application has no volume to come back to, so this needs a deployment with one.

11.4.5 Comparing records with the store

The worker compares records with the stores daily, on the volume as on object storage, reading only. It writes storage.drift to the audit log when they differ, and alerts administrators about records whose file the store does not hold, and when the count of files with no record changes. An outage produces no answer, never a list of missing files. To run it by hand:

docker compose run --rm --no-deps worker ./node_modules/.bin/tsx --tsconfig tsconfig.worker.json src/lib/jobs/storage-reconcile.ts

It exits 0 when every record's file was found, and 1 when a record has no file, a store could not be read, or a record names a store this instance does not read. A store that cannot be read costs the other stores' answers nothing. A file with no record does not change that. Records left naming the wrong store by a restore across a storage move are repaired with --repair-rows: see the Disaster Recovery Guide.

A file with no record is an orphan, left by an upload that failed after its file landed, a delete that could not reach the store, or a restore of an older backup. Orphans are only reported, as a newer database backup may still name them. A person removes them:

docker compose run --rm --no-deps worker ./node_modules/.bin/tsx --tsconfig tsconfig.worker.json src/lib/jobs/storage-reconcile.ts --remove-orphans --older-than-days 90

The age must be at least 30 days; choose a number past your oldest kept backup, and read the limits of that age and the refusals above. A file any record names is never removed, and only the store this instance writes to is touched. Each batch is named in the audit log as storage.orphans_removed before it goes, and if that entry cannot be written, the run stops.

11.5 Restore drill

Test the server backup regularly (at least quarterly, and after any change to the backup job) by restoring it onto a throwaway instance on the same server. The drill below leaves production untouched and, as long as nobody clicks Send test email or a webhook's Test in it, sends no email and posts no webhook.

  1. On production, record the row counts and take a backup straight after:

    cd /srv/grc
    docker compose exec -T db sh -c 'psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -At -F " " -f -' \
      < deploy/counts.sql | sort > /tmp/counts-prod.txt
    ./deploy/backup.sh /var/backups/grc-drill 1
    
  2. Create the drill directory from the same release and a copy of .env:

    mkdir /srv/grc-drill
    tar -xzf grc-release-<version>.tar.gz -C /srv/grc-drill
    cp /srv/grc/.env /srv/grc-drill/.env
    
  3. Edit /srv/grc-drill/.env:

    COMPOSE_PROJECT_NAME=grc-drill
    APP_IMAGE=grc-platform:drill
    APP_URL=http://localhost:3100
    WEB_PORT=3100
    WORKER_WATCH=off
    

    WORKER_WATCH=off stops web emailing the restored administrators about the worker, which the drill doesn't run. Keep AUTH_SECRET and the other keys as they are, so the restored integration secrets stay readable.

  4. Restore into the drill project:

    cd /srv/grc-drill
    ./deploy/restore.sh /var/backups/grc-drill/db-<stamp>.dump /var/backups/grc-drill/uploads-<stamp>.tar.gz
    
  5. Start only db, migrate and web. Leaving out worker and caddy means notifications, rule emails and webhooks queue in the drill's own database and never go out, as only the worker sends them. It also does not compete for ports 80 and 443. In web, only Send test email and a webhook's Test reach out, and the restored settings point them at production's mail server and webhook addresses, so leave them alone:

    docker compose up -d --build web
    
  6. Check that migrate found nothing to apply and skipped the initial administrator:

    docker compose logs migrate | grep -E 'No pending migrations|users exist'
    
  7. Compare the row counts with production. Only tables whose counts differ are printed; expect none, or only small differences in tables production wrote to between step 1 and the backup:

    docker compose exec -T db sh -c 'psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -At -F " " -f -' \
      < deploy/counts.sql | sort > /tmp/counts-drill.txt
    join -a 1 -a 2 -e MISSING -o 0,1.2,2.2 /tmp/counts-prod.txt /tmp/counts-drill.txt \
      | awk '$1 ~ /^public\./ && $2 != $3'
    
  8. Verify the audit chain:

    docker compose exec web ./node_modules/.bin/tsx prisma/audit-verify.ts
    

    Expect {"ok":true,...}, with rows equal to the drill's public.audit_log count from step 7.

  9. Sign in through an SSH tunnel (ssh -L 3100:127.0.0.1:3100 <server>, then open http://localhost:3100), open a record with an attachment and download it.

  10. Remove the drill instance, its volumes and its image:

    docker compose down -v
    docker image rm grc-platform:drill
    cd / && rm -rf /srv/grc-drill /var/backups/grc-drill /tmp/counts-prod.txt /tmp/counts-drill.txt
    

    down -v deletes the drill's volumes. Run it only in /srv/grc-drill, never in the production directory.

Record the date, the backup used, and the results of steps 6 to 9 as evidence of a tested restore.