> ## Documentation Index
> Fetch the complete documentation index at: https://patch.codemagic.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Operations

> Back up, restore, upgrade, and smoke-test a self-hosted Patch installation.

Command reference for a self-hosted install. For cadence and what to watch day to day, see [Ongoing maintenance](/docs/setup/ongoing-maintenance).

The backup, restore, and upgrade sections have `cmpatch selfhost` equivalents
that run the scripts over SSH from your own machine — connecting, selecting,
and confirming for you. They are wrappers, never reimplementations. Status,
logs, and smoke checks are direct host commands. The scripts remain the
reference, and still work when run on the host. On a [Fly install](/docs/install#fly)
there is no host to connect to: backup, restore and upgrade run through flyctl
from the machine that recorded the install, without an address. If this
machine's default server is another install, set
`CODEMAGIC_PATCH_SERVER_URL=https://<app>.fly.dev` for the run, or run
`cmpatch config set server-url <that URL>` once.

On an SSH install, all maintenance commands run against the `codemagic-patch-selfhost` Compose project.

## Status and logs

The self-host stack is split across Compose files. `docker-compose.selfhost.yml` defines only Caddy and the server; the database and storage each come from an overlay file in `deploy/selfhost/`, chosen at install time by settings in `.env.selfhost`. The `cmpatch selfhost` commands add the right overlays for you. When you run `docker compose` yourself, add the same files with `-f` after the base file, in this order:

| Setting in `.env.selfhost` | File to add |
| --- | --- |
| `SELFHOST_DATABASE_MODE=bundled` (or unset) | `deploy/selfhost/compose.bundled-db.yml` |
| `SELFHOST_DATABASE_MODE=external` | `deploy/selfhost/compose.external-db.yml` |
| `SELFHOST_STORAGE_MODE=bundled` (or unset) | `deploy/selfhost/compose.bundled-storage.yml` |
| `SELFHOST_STORAGE_ORIGIN_MODE=cdn-origin`, with bundled storage | `deploy/selfhost/compose.bundled-storage-cdn-origin.yml` |
| `SELFHOST_STORAGE_MODE=s3` | `deploy/selfhost/compose.external-storage-s3.yml` |
| `SELFHOST_STORAGE_MODE=gcs` | `deploy/selfhost/compose.external-storage-gcs.yml` |
| `SELFHOST_SCHEME=http` | `deploy/selfhost/compose.http.yml` |
| `docker-compose.selfhost.override.yml` exists | that file, last |

Without the overlays, Compose sees no database or storage: `ps` lists only Caddy and the server, and `up -d` starts a server that crash-loops without a database. The commands below are for the default install (bundled database and storage).

```bash
docker compose --project-name codemagic-patch-selfhost --env-file .env.selfhost \
  -f docker-compose.selfhost.yml -f deploy/selfhost/compose.bundled-db.yml \
  -f deploy/selfhost/compose.bundled-storage.yml ps

docker compose --project-name codemagic-patch-selfhost --env-file .env.selfhost \
  -f docker-compose.selfhost.yml -f deploy/selfhost/compose.bundled-db.yml \
  -f deploy/selfhost/compose.bundled-storage.yml logs -f server
```

## Backup

Copies the settings, deployment-mode manifest, and version metadata. When
either data component is bundled it also quiesces the server while dumping
Postgres and/or mirroring the Silo bucket. With both components external it
leaves the server running and creates a configuration-only backup:

```bash
cmpatch selfhost backup --download
```

```bash
# on the host:
scripts/selfhost/backup.sh
# → backups/codemagic-patch-selfhost-<timestamp>/ : env.selfhost,
#   backup-manifest, versions.txt, plus postgres.dump and/or
#   minio-codemagic-patch.tar.gz for each bundled component
```

Run through the CLI, backups land under `~/codemagic-patch-backups/` on the
server so `cmpatch selfhost restore` can list them, and `--download` copies one
to this machine as well.

On a server installed before the CLI's `selfhost` commands existed, the
checkout's `backup.sh` predates the `--directory` option the CLI uses, and
`cmpatch selfhost backup` refuses rather than guess. Run
`cmpatch selfhost upgrade` once first (it takes its own pre-upgrade backup),
then back up through the CLI.

:::warning External data needs a separate backup
An external PostgreSQL database or S3/GCS bucket is not copied. The command
names each omitted component before it continues; protect those components with
your provider's snapshot, PITR, or object-storage backup tooling. A downloaded
backup and the automatic pre-restore safety backup have the same exclusions.
Use `created_at` in `versions.txt` as the point-in-time anchor when restoring
provider-managed components.
:::

### Fly backup

```bash
cmpatch selfhost backup
# → codemagic-patch-backups/<app>.fly.dev/<app>-<timestamp>/ in the current
#   directory: fly-backup-manifest, fly.toml, postgres.dump
```

The dump is a `pg_dump -Fc` archive, taken inside the Postgres machine while
Patch keeps running. With a database cmpatch did not create there is no
`postgres.dump`: the backup holds the configuration only. Tigris objects and
secrets are never included.

## Restore

```bash
cmpatch selfhost restore
# lists the backups on the server (labelling the automatic safety ones),
# or takes a folder from this machine and uploads it
```

```bash
# on the host:
scripts/selfhost/restore.sh backups/codemagic-patch-selfhost-<timestamp>
# also replace .env.selfhost from the backup:
scripts/selfhost/restore.sh --restore-env backups/codemagic-patch-selfhost-<timestamp>
```

Restore takes a pre-restore safety backup first unless the current bundled data
volumes do not exist (or the direct script is passed `--skip-safety-backup`). It
restores only the components present in the selected backup; recover any
external database or object storage separately with the provider's tooling.

Restore runs the server image `.env.selfhost` pins, not the checkout's: without
`--restore-env` a backup taken on an older release comes back under the newer
server, which migrates it again. Use `--restore-env` to return to the backup's
server, or upgrade to move forward: the next upgrade checks the database's
applied migrations against the release and refuses one the release does not
know.

### Fly restore

```bash
cmpatch selfhost restore
# lists the Fly backups under codemagic-patch-backups/<app>.fly.dev/
cmpatch selfhost restore <app>-<timestamp> --yes
# non-interactive: name the backup (or its path) and confirm
```

It checks the backup, turns the Postgres machine's auto-stop off so Fly cannot
suspend it mid-restore, uploads the dump while Patch still runs, then
takes Patch down (no machines), snapshots the database volume, replaces the
database, checks it, deploys the same server image again, and turns auto-stop
back on. It refuses a backup of
another app, a configuration-only backup, and a backup taken on a newer server
(upgrade with `--tag` first). Each step, and the commands to finish or undo a
restore that stopped, go to `<app>-restore-<timestamp>.md` next to the backups.
`--restore-env`, `--smoke`, `--skip-smoke` and `--ssh-key` do not apply on Fly.

## Upgrade

To see if a new server version is available, check the repo or the Status page in the dashboard.

Moves to the newest server release, backs up, pulls that release's published
server image and the stock Caddy image, then smoke-tests:

```bash
cmpatch selfhost upgrade
# pin a specific server image:
cmpatch selfhost upgrade --image registry.example.com/codemagic-patch-server:tag
# build the server image on the server instead of pulling it:
cmpatch selfhost upgrade --build-from-source
```

The CLI moves the server's checkout to the release tag first, and refuses a
tree with local edits or commits of its own, or one newer than that release.
Before anything changes it also pulls the release's server image and runs that
image's own schema check, which reads the database with the server's own
connection settings: a database that has already applied a migration the
release does not know is refused, since that would run an older server on a
newer schema. With `--build-from-source` the check runs after the build,
before the restart. A stopped stack cannot be checked; start it and retry.
`--skip-schema-check` skips the check, unsafely.

```bash
# on the host: move to the release, then run its script
git fetch --tags && git checkout codemagic-patch-server-v<x.y.z>
scripts/selfhost/upgrade.sh
scripts/selfhost/upgrade.sh --image registry.example.com/codemagic-patch-server:tag
```

A failure after the stack is recreated is rolled back in this order. The
script prints each step with this host's compose files, ref, and absolute
paths.

1. `docker compose … stop server`, so the new server cannot migrate the
   database again. It stays stopped until the restore.
2. After a `--build-from-source` upgrade from a host-built image:
   `docker tag codemagic-patch-server:pre-upgrade-<time> codemagic-patch-server:selfhost`.
3. `git -C <checkout> checkout <previous ref>`.
4. With an external database: roll it back with your provider's tooling.
5. `<checkout>/scripts/selfhost/restore.sh --restore-env <pre-upgrade backup>`,
   which starts the stack.

### Fly upgrade

```bash
cmpatch selfhost upgrade
# a specific release instead of the newest:
cmpatch selfhost upgrade --tag codemagic-patch-server-v<x.y.z>
```

Before the deploy it snapshots the database volume when cmpatch created the
database, then writes
`codemagic-patch-backups/<app>.fly.dev/<app>-upgrade-<timestamp>/` with
`ROLLBACK.md`, the steps to go back. With a database you run yourself, take
your own backup or note a point-in-time recovery point first. When the running
release's tag is recorded, it refuses an older release. It does not roll back
by itself.
