# Backup and restore

Back up the PostgreSQL database and the blob storage of a self-hosted Cohorly, restore them, and test the restore on a scratch copy without touching the running instance.

## What to back up

| What               | Where it lives                                                                             | Why                                                                     |
| ------------------ | ------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------- |
| The database       | The `selfhost-pgdata` volume, database `cohorly`                                           | Events, profiles, accounts, reports, settings: everything except blobs. |
| Blobs              | The `selfhost-blobs` volume with the `disk` driver, or your bucket with `s3`               | Source map and native symbol uploads, session replay chunks.            |
| Your configuration | `.env`, `docker-compose.override.yml` if you have one, the license key file if you use one | The secrets in `.env` cannot be recovered from the database.            |

Keep the copies on another machine or in another storage account. A backup that
lives only on the host dies with the host.

Caddy's certificates (the `selfhost-caddy-data` volume) do not need a backup:
Caddy gets a new certificate on the first start, within Let's Encrypt's rate
limits.

## Back up the database

Run this in the directory with `docker-compose.yml`. It takes a consistent
database snapshot while the instance keeps running (see
[Back up the blobs](#back-up-the-blobs) for a backup that must also be
consistent with the blobs):

```bash
docker compose exec -T postgres pg_dump -U cohorly cohorly \
  | gzip > cohorly-$(date +%F).sql.gz
```

The result is a plain SQL dump, compressed. Its size is dominated by the
events table (see [Sizing](/self-hosting/requirements#sizing) for the disk each
million events takes).

## Back up the blobs

With the `s3` driver, the blobs are in your bucket: protect it the way you
protect your other buckets (versioning, replication, a lifecycle policy).

With the default `disk` driver, archive the volume right after the dump. This
runs the server image with `tar` as its command, so it does not start the API
and does not check the license:

```bash
docker compose run --rm -T --no-deps --entrypoint tar server \
  czf - -C /app/data/blobs . > cohorly-blobs-$(date +%F).tar.gz
```

An empty archive is normal if you have uploaded no symbols and use no session
replay.

Take the blobs after the database. A blob written between the two is then in
the archive without a row pointing to it, which is harmless. A blob that is
deleted between the two (by retention, a cleanup or a privacy deletion) leaves a
row in the dump with no file in the archive. For a backup where that cannot
happen, stop the API and the worker while you take both, and start them again
after:

```bash
docker compose stop server error-worker
# run the pg_dump and the blob archive commands above
docker compose start server error-worker
```

Events that devices send while the API is stopped are retried by the SDKs.

## Restore

A restore replaces the database. It needs the stack's PostgreSQL running and
the API, worker, dashboard and proxy stopped:

```bash
docker compose up -d --wait postgres
docker compose stop caddy dashboard error-worker server
```

Drop and recreate the database, then load the dump:

```bash
docker compose exec -T postgres psql -U cohorly -d postgres \
  -c 'DROP DATABASE cohorly WITH (FORCE)' \
  -c 'CREATE DATABASE cohorly'

gunzip -c cohorly-2026-01-15.sql.gz \
  | docker compose exec -T postgres psql -U cohorly -d cohorly -v ON_ERROR_STOP=1
```

Restore the blobs, if you back them up:

```bash
docker compose run --rm -T --no-deps --entrypoint tar server \
  xzf - -C /app/data/blobs < cohorly-blobs-2026-01-15.tar.gz
```

Start everything and check it:

```bash
docker compose up -d --no-build
curl https://cohorly.example.com/health
```

Run the image version that was running when the backup was taken. A dump
carries the schema of that version, and the server migrates forward only (see
[Going back](/self-hosting/upgrade#going-back)).

## Test a restore

A backup you have not restored is a hope. Test one on a scratch copy that does
not touch the running instance. A second Compose project has its own
containers and volumes, and the `postgres` service publishes no port, so it
cannot clash with the first:

```bash
docker compose -p cohorly-restore-test up -d --wait postgres

gunzip -c cohorly-2026-01-15.sql.gz \
  | docker compose -p cohorly-restore-test exec -T postgres \
      psql -U cohorly -d cohorly -v ON_ERROR_STOP=1

docker compose -p cohorly-restore-test exec -T postgres psql -U cohorly -d cohorly \
  -c 'SELECT count(*) AS events, to_timestamp(max(time) / 1000) AS latest FROM events'
```

Compare the count and the latest event time with what the running instance
shows for the moment of the backup. Then remove the scratch copy and its
volumes:

```bash
docker compose -p cohorly-restore-test down -v
```

The `-p cohorly-restore-test` flag matters: `down -v` without it would delete
the volumes of your real instance. Do the test on a schedule, not only once.
