# Backing up and restoring

> What to back up on the control plane and on an inference node, how to make a consistent backup, and how to restore the control plane onto the same or a new machine.

## What to back up

The control plane holds the platform's records; an inference node holds little that cannot be made again.

**The control plane.** Where each folder below is, also on a Mac, and who writes it, is in [Files and folders on the machine](/en/platform/docs/v5.10/guides/machine-administration/files-and-folders/index.md). The program in `/opt/onprem-ai/control-plane/` needs no backup: installing the same release puts it back.

| What | Where | Needed for a restore |
|---|---|---|
| The database: accounts, inference nodes, deployments, catalogs, settings, the audit log, the control plane's root certificate | the Docker volume `onprem-ai-control-plane_postgres-data` | yes |
| The secrets | `/var/lib/onprem-ai/control-plane/secrets/` | yes, always together with the database |
| Your settings | `/etc/onprem-ai/control-plane/` (`config.env` and the files it names) | yes |
| This machine's identity | `/var/lib/onprem-ai/control-plane/machine-identity.generated.env` | yes, always |
| Certificate files you provided | `/var/lib/onprem-ai/control-plane/tls/` | when you provided some |
| Your own certificate authorities | `/etc/onprem-ai/trusted-certificate-authorities/` (on a Mac `/Users/Shared/onprem-ai/trusted-certificate-authorities/`) | when it holds files |
| The trust bundle built from them at each start | the Docker volume `onprem-ai-control-plane_trusted-certificate-authorities-bundle` | no; each start builds it again from the folder |
| The object storage: the catalogs' deploy profile files and git repositories, the deployment plans the inference nodes run, the hardware catalog, error report packages | the Docker volume `onprem-ai-control-plane_object-storage-data` | yes, always together with the database: its records point at these files |
| The metrics history | the Docker volume `onprem-ai-control-plane_victoria-metrics-data` | no; without it the charts start empty |

`config.generated.env` and `backend.generated.env` in `/var/lib/onprem-ai/control-plane/`, and `.env` and `compose.override.yaml` in `/opt/onprem-ai/control-plane/`, need no backup: `sudo onprem-ai-ctl apply` writes them again. `machine-identity.generated.env` is the exception: back it up.

The database, the object storage and the secrets belong together, and a backup takes them close together in time: a deployment made between the two copies is recorded in one and missing from the other.

The database and the secrets are bound most tightly. The database keeps the control plane's root certificate with its private key encrypted under `secrets/config_encryption_key`, and the database password is `secrets/postgres_password`. A database backup without the secrets cannot be restored into a working control plane; a backup of the secrets is a backup of keys, so store it as you store passwords.

**An inference node** (on Linux):

| What | Where | Needed |
|---|---|---|
| The configuration, and the machine's own certificate authorities | `/etc/onprem-ai/inference-node/config.env` and `/etc/onprem-ai/trusted-certificate-authorities/` | optional |
| Its identity, certificate and local records | `/var/lib/onprem-ai/inference-node/` | optional |
| Model weights | `/var/lib/onprem-ai/inference-node/cached-file-sets/`, or where you moved them | optional: they download again |

An inference node needs no backup to be restored: what it runs is the control plane's record. A lost inference node is removed under **Servers** and installed again with a new join token; the control plane gives it its models again, and the weights download again. Backing up the weights only saves that download.

## Making a backup

Run these on the control plane's machine as root. The database is dumped while the control plane runs, which is consistent on its own (PostgreSQL's dump is one transaction). The object storage is copied while its one service is stopped for a moment, so its files are consistent too; the control plane keeps serving meanwhile and writes no error report package in that moment.

```sh
cd /var/lib/onprem-ai/control-plane
C="docker compose --project-name onprem-ai-control-plane --project-directory /var/lib/onprem-ai/control-plane --file /opt/onprem-ai/control-plane/compose.yaml --env-file /var/lib/onprem-ai/control-plane/config.generated.env"
mkdir -p /root/onprem-ai-backup
$C exec -T postgres pg_dump -U onprem_ai -d onprem_ai -Fc > /root/onprem-ai-backup/onprem_ai.dump
tar -czf /root/onprem-ai-backup/data-folder.tar.gz secrets tls machine-identity.generated.env
tar -czf /root/onprem-ai-backup/etc-onprem-ai.tar.gz -C /etc onprem-ai
$C stop object-storage
docker run --rm -v onprem-ai-control-plane_object-storage-data:/v:ro -v /root/onprem-ai-backup:/b alpine tar -C /v -czf /b/object-storage.tar.gz .
$C start object-storage
```

The first `tar` copies the secrets, the certificate files and the machine's identity; the second copies `/etc/onprem-ai/`: your settings and your own certificate authorities. Both archives hold keys, so store them as you store passwords.

Copy the backup off the machine.

A snapshot of the whole machine or of its disks works as well: PostgreSQL recovers from a crash-consistent snapshot as it does after a power cut. A snapshot of the database's volume alone, taken while it runs, is crash-consistent; the dump above is the application-consistent one.

## Restoring the control plane

Onto the same machine or a new one, with the same release as the backup (the release is in `state/installed-release/release.json`, and on the Updates page). A newer release afterwards is an update, one step at a time, as the Updates page offers it.

1. Install the control plane of that release:

   ```sh
   curl -fsSL https://get.onprem.ai/control-plane | sh -s -- --release <release>
   ```

2. Stop it and put back the secrets, the settings and the machine's identity:

   ```sh
   cd /var/lib/onprem-ai/control-plane
   C="docker compose --project-name onprem-ai-control-plane --project-directory /var/lib/onprem-ai/control-plane --file /opt/onprem-ai/control-plane/compose.yaml --env-file /var/lib/onprem-ai/control-plane/config.generated.env"
   $C down
   tar -xzf /root/onprem-ai-backup/data-folder.tar.gz
   tar -xzf /root/onprem-ai-backup/etc-onprem-ai.tar.gz -C /etc
   ```

3. Restore the database into a fresh one, and give its owner the password from the restored secrets (the fresh install made its own):

   ```sh
   $C up -d --wait postgres
   $C exec -T postgres dropdb -U onprem_ai onprem_ai
   $C exec -T postgres createdb -U onprem_ai onprem_ai
   $C exec -T postgres pg_restore -U onprem_ai -d onprem_ai --no-owner < /root/onprem-ai-backup/onprem_ai.dump
   $C exec -T postgres psql -U onprem_ai -d postgres -c "ALTER ROLE onprem_ai PASSWORD '$(cat secrets/postgres_password)'"
   ```

4. Restore the object storage:

   ```sh
   docker run --rm -v onprem-ai-control-plane_object-storage-data:/v -v /root/onprem-ai-backup:/b:ro alpine sh -c "rm -rf /v/* && tar -C /v -xzf /b/object-storage.tar.gz"
   ```

5. Start the control plane and check it. The install gives the restored files their owners and modes back:

   ```sh
   sudo onprem-ai-ctl install
   sudo onprem-ai-ctl doctor
   ```

The control plane comes back with its accounts, keys, inference nodes, deployments, catalogs, settings, audit log and error reports with their packages. It keeps its root certificate, so every inference node trusts it as before and reconnects on its own, without a new join token.

### On a machine with a new address

The inference nodes connect to the address they were installed with and to the addresses the control plane announced to them since. When the restored control plane has another address, give the new machine the old address if you can. Otherwise move each inference node to the new address: make a join token under **Servers**, **Add Inference Node**, and run `sudo onprem-ai-node connect --join-token <token> --control-plane-root-certificate-sha256 <fingerprint> --control-plane-gateway-address <new address>` on it; it comes back as the same inference node, with its history. Certificates the control plane makes name the new machine's addresses from its first start; certificates you provide (`tls/`) must name them.

## Restoring an inference node

Install it again with the command from **Servers**, **Add Inference Node**, after removing the lost one there. The control plane gives it its models again, and their weights download again.

GPU drivers and the NVIDIA Container Toolkit belong to the machine and are installed as for any new inference node; the GPU assignments, deploy profiles and settings come from the control plane.
