# Back up and restore

> What to back up from a Kubernetes installation of Corfedo, what happens when the database is older than the bucket, and how to restore the installation.

Source: https://docs.corfedo.ai/operate/kubernetes/backup-and-recovery/

Corfedo keeps its state in your PostgreSQL database and its indexes in the bucket. The volumes of the search nodes and indexers hold caches and need no backup.

## What to back up

| Back up | Why |
| --- | --- |
| The database, with your PostgreSQL service's backups, such as snapshots or point-in-time recovery | It holds everything except the indexes, such as workspaces, members, conversations and the credentials saved in **Settings** |
| `corfedo-keys` | The credentials saved in **Settings** cannot be read without it |
| `corfedo-oauth` | It signs the tokens of the CLI, coding agents and SDKs. With a new key set, every client signs in again. |
| `corfedo-database` | The passwords of the six logins, which must match the restored server |
| `values.yaml` and the chart version | They restore the same configuration |
| The `.corfedo-root-id` object under `storage.s3.prefix`, or the whole bucket | See [The bucket](#the-bucket) |

With Secrets of your own, back up those. You can create the others again as [Install](https://docs.corfedo.ai/operate/kubernetes/#create-the-secrets) describes.

The backups hold the installation's keys. Store them as you store secrets.

The bundled PostgreSQL and Keycloak have no backup.

## The bucket

Restore the database and the bucket as a pair: the same bucket and prefix, at the same S3 endpoint (an `AWS_ENDPOINT_URL_S3` override counts), with the `.corfedo-root-id` object under the prefix. Without a copy of the indexes, every indexed repository shows **Unavailable** after a restore until you select **Reindex now** on its page, see [Repositories](https://docs.corfedo.ai/workspace/sources/repositories/).

To reindex less, copy the bucket's objects right after each database backup, for example with `rclone copy`, and keep one copy for each database backup you keep. A repository whose index changed between the backup and the copy still shows **Unavailable** until you reindex it.

## When the database is older than the bucket

When you restore the database to an earlier point than the bucket's contents:

- Indexes built after that point are deleted from the bucket about an hour after the restore.
- A repository that got a new index after that point can show **Unavailable**, because its earlier index was deleted. **Reindex now** indexes it again.

## Restore an installation

Restore into a namespace without the release. To restore over an existing release, run `helm uninstall` first. It keeps the Secrets, the database and the bucket.

1. On a new cluster, provide what [Prepare the cluster](https://docs.corfedo.ai/operate/kubernetes/prepare-the-cluster/) lists: cert-manager, the issuer for workload certificates, the storage class your values name and the public entry.
2. Restore the database and its six logins to your PostgreSQL server, with the passwords in your copy of `corfedo-database`. If the server's address changed, set `database.host` in the values file.
3. In an empty namespace, create the Secrets from your copies under the same names, and the others as on [Install](https://docs.corfedo.ai/operate/kubernetes/#create-the-secrets). After `helm uninstall` the Secrets still exist: replace the contents of any that differ from your copies.
4. Keep `storage.s3` as before. If the bucket lost its objects, copy back your copy, or at least `<prefix>/.corfedo-root-id`.
5. Install the chart version you kept with your values file:

   ```sh
   helm install corfedo oci://ghcr.io/corfedo/charts/corfedo --version CHART_VERSION \
     --namespace corfedo --values values.yaml
   ```

6. On a new cluster, point the host name's DNS at its public entry.

**Check:** as in [Check the result](https://docs.corfedo.ai/operate/kubernetes/#check-the-result), the setup Job completes and every pod is ready. Members sign in as before.

| What you see in the setup pod's logs | Why | What to do |
| --- | --- | --- |
| `The storage marker of this installation (…) is missing` | The bucket or prefix lost its `.corfedo-root-id` object | Copy it back from your backup, or start anew with a new database and an empty bucket |
| `The installation's storage identity … belongs to S3 bucket …` | The bucket, prefix or endpoint differs from before | Use the storage the message names |

## Related

- [Upgrade and operate](https://docs.corfedo.ai/operate/kubernetes/upgrade-and-operate/#uninstall) lists what `helm uninstall` keeps.
- [Data handling](https://docs.corfedo.ai/operate/security/data-handling/) lists what is stored where.
