> ## 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.

# Ongoing maintenance

> Day-2 care for a self-hosted Patch server: backups, upgrades, health checks, and disk growth.

Self-hosting means you own the box. Patch automates as much of install, backup, upgrade, and restore as it can; this page is the remaining ops rhythm you should expect. Exact commands live in [Operations](/docs/reference/operations).

## Backups

Run backups on a schedule you trust, and keep each one off the host. On-box copies help with a bad upgrade; they do not help if the droplet or disk is gone.

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

Every backup copies the settings, deployment-mode manifest, and version
metadata. When either data component is bundled, it pauses publishing briefly
while it dumps Postgres and/or mirrors the Silo bucket. With both components
external, it creates a configuration-only backup without stopping the server.
An external database or S3/GCS bucket is **not** included: the command names the
omitted components before continuing, and you must back them up with your
provider's snapshot, PITR, or object-storage tooling. Use `created_at` in the
backup's `versions.txt` as the point-in-time anchor for those provider restores.
`--download` copies only the components that the server-side backup contains to
your own machine.

Restore with `cmpatch selfhost restore`, which lists what is on the server and
takes a safety backup of the current data first. That safety backup has the same
mode-specific exclusions, and restore does not recover external provider data.

Both are thin wrappers over `scripts/selfhost/backup.sh` and `scripts/selfhost/restore.sh`, which you can still run on the host directly.

Before you treat the stack as production, take one backup and confirm you can find it somewhere other than the server.

### On Fly {#fly-backups}

On a [Fly install](/docs/install#fly), run the backup from the machine that recorded the install, in the directory where you want the backups to live:

```bash
cmpatch selfhost backup
```

It dumps the database inside its Postgres machine while Patch keeps running, and saves it with the app's Fly configuration under `codemagic-patch-backups/<app>.fly.dev/` in the current directory. It does not include:

- The release files in the Tigris buckets; protect them with Tigris's own tooling
- The app's secrets
- A database you run yourself; the backup then holds only the configuration, and the database needs your provider's backups

Copy the backup folder somewhere other than this machine. If this machine's default server is another install, set `CODEMAGIC_PATCH_SERVER_URL=https://<app>.fly.dev` for the backup, restore, and upgrade commands, or make the Fly install the default with `cmpatch config set server-url`.

```bash
cmpatch selfhost restore
```

Restore lists the Fly backups in that folder and replaces the database of the same Fly app. The server version stays as it is, so a backup taken on a newer server is refused: upgrade to at least that version first. While it runs, the dashboard, the API, and publishing are down; your apps keep downloading updates from storage. Storage is not restored: release files uploaded after the backup stay in the buckets for you to reconcile. Restore snapshots the database volume first, keeps the Postgres machine from auto-stopping while it works, and writes each step to a restore record (`<app>-restore-<time>.md`) next to the backups. If it stops after taking Patch down, finish or undo it with the steps it printed; the record has them too, with your ids. Afterwards, sign in again with `cmpatch login`.

## Upgrades

When a newer server release is out:

```bash
cmpatch selfhost upgrade
# or pin a registry image:
cmpatch selfhost upgrade --image registry.example.com/codemagic-patch-server:tag
```

It shows the plan and asks before it changes anything; `--yes` skips the question, and a scripted run needs it. The command then moves the server's copy of the source to the newest server release (it refuses a checkout with local edits or commits of its own, one newer than that release, or a database that already has migrations the release does not know, before asking; for that last check it pulls the release's server image and runs the image's own schema check), then runs that release's `scripts/selfhost/upgrade.sh` over SSH. By default that script:

1. Pulls the server image published with the release (it includes the dashboard)
2. Creates a pre-upgrade backup (under `~/codemagic-patch-backups/pre-upgrade/` when run through the CLI, so `cmpatch selfhost restore` can find it) and pulls the stock Caddy image
3. Recreates the Compose stack and waits for health
4. Runs `scripts/selfhost/smoke.sh` (full publish smoke needs `CODEMAGIC_PATCH_TOKEN`)

`--build-from-source` builds the server image on the server instead of pulling it; that needs at least 2 GB of memory.

Pass `--i-have-a-backup` only if you already took an external backup for this upgrade. If the upgrade fails before the stack is recreated, the source and image settings are put back and nothing else changed. If it fails after, the new server may already have migrated the database, so re-pinning the old image is not a safe rollback: follow the steps the command prints, in order — stop the new server, check the previous release out again, roll back an external database with your provider's tooling, then restore the pre-upgrade backup with its settings. Keep the previous server image on the server until you have confirmed the upgrade; the rollback runs it. A `--build-from-source` upgrade keeps a host-built image as `codemagic-patch-server:pre-upgrade-<time>` for that.

An install made before published server images runs the default branch, usually with a server image built on the server. `cmpatch selfhost upgrade` moves any install still on the default branch onto the newest release and its published image. If the server's copy is ahead of the newest release, it refuses, because that would run an older server on a newer database: wait for the next release, or pass `--build-from-source`. If the server ran a locally built image, do not prune the old images until the upgrade is confirmed.

### On Fly {#fly-upgrades}

On a Fly install, `cmpatch selfhost upgrade` deploys the newest server release's published image with the app's current Fly configuration (`--image` deploys an image you name; `--build-from-source` builds the release on Fly's remote builder instead). Before the deploy it:

1. Snapshots the database volume, when cmpatch created the database
2. Writes a baseline folder with a `ROLLBACK.md` under `codemagic-patch-backups/<app>.fly.dev/` in the current directory

It shows the plan and asks before the snapshot, as on a server; `--yes` skips the question. With a database you run yourself there is no snapshot: take your own backup, or note a point-in-time recovery point, before you upgrade.

The CLI does not roll back by itself. To go back, follow that `ROLLBACK.md`; its commands already carry your snapshot, image, and tag.

## Health and monitoring

Open **Status** in the [dashboard](/docs/using-patch/dashboard) sidebar to confirm this install is fully running and up to date. It shows database, storage, and disk health, and whether a newer Patch server release is available.
From a terminal, `cmpatch server status` prints the same checks; add `--format json` to script them.

Set up a free uptime check against the ready endpoint (it checks the server process and its Postgres connection; storage is not covered). Without one, you may only notice downtime when OTA or the dashboard fails:

```bash
curl -fsS https://updates.example.com/health/ready
```

Also alert on host disk filling up. Monitor storage health separately (`https://storage-updates.example.com/minio/health/ready`). The ready endpoint does not detect storage failures.

Use Compose status and logs when something is already wrong, not as the primary check:

A hand-run `docker compose` command needs the Compose files that match your install. The commands below are for the default install (bundled database and storage); for other setups, see the file list in [Status and logs](/docs/reference/operations#status-and-logs).

```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
```

If you use [Cloudflare or CloudFront](/docs/reference/cdn) and clients report stale updates, look in server logs for `delivery cache purge completed with failures`. On Cloudflare, also check that the old two-hour Edge TTL Cache Rule from the previous setup guide has been deleted. It overrides the origin `s-maxage` and stretches the stale window. See [CDN settings](/docs/reference/cdn#add-cache-rules).

## Disk size {#disk-growth}

The default single-host stack keeps release artifacts in object storage and device metrics in Postgres. The server keeps raw metric events for 8 days, then folds them into small daily totals that are kept indefinitely; the window is fixed and needs no cron job. Disk usage for Postgres should be around 2 GB per 100,000 daily active users. See [Machine size](/docs/install#machine-size) for starting sizes. Plan more disk before the volume fills.

Postgres keeps the space the fold frees inside the table files instead of returning it to the operating system. New rows reuse it, so the volume stops growing. To shrink the volume after the first fold on a large history, run `VACUUM FULL metric_event` in a quiet window: it locks the table until it finishes, and metric ingest waits meanwhile. [pg_repack](https://github.com/reorg/pg_repack) does the same with only brief locks, if its extension is installed on the database.

## When the default stack is not enough

Most installs stay on one Docker host. For production, put [Cloudflare or CloudFront](/docs/reference/cdn) in front of storage; it is strongly recommended so devices download from the edge rather than the single host, and it gives you automatic manifest purge. When to move Postgres or release files off that host, see [Infrastructure](/docs/setup/infrastructure).

## Related

- [Operations](/docs/reference/operations): backup, restore, upgrade, and smoke commands
- [Install](/docs/install): first install and verify
- [Security](/docs/using-patch/security): tokens, access, and hardening
- [Preparing for production](/docs/using-patch/preparing-for-production): release-side checklist before real users
