# Back up and restore

Source: https://dropshelf.ai/docs/self-host/operate/backup

Back up the two halves of a dropshelf instance, the Postgres database and the bucket, and restore them together.

This page is for operators who need to recover a dropshelf instance after data loss, or before an
upgrade. A dropshelf instance keeps its state in two places, and a backup is a copy of both, taken
database first.

## What to back up [#what-to-back-up]

The instance's state is in these places:

- **The Postgres database** holds every record: members, drops, versions, tokens, comments and
  settings.
- **The object store** holds the files: every version of every drop, uploaded assets and profile
  pictures. This is the bucket, the Azure Blob container, or the `storage.fs.root` directory on a
  single host.
- **Your values file and the Secret** hold the configuration. Keep them with the backup, because
  the keys in `connectors.secrets` seal source credentials stored in the database.

The spool and cache directories hold nothing to back up. The server empties both at every startup,
and the cache refills from the bucket.

The database backup alone is half of the state. A database restored beside a bucket that has moved
on gives you drops whose files are gone, and files that no drop points at.

## Prerequisites [#prerequisites]

- `pg_dump` and `pg_restore` from a machine that can reach the database, at the same Postgres major
  version as the server or newer.
- The database URL, which the chart reads from `DROPSHELF_DATABASE_URL` in the Secret.
- Permission to change the bucket's settings at your cloud provider.

## Turn on bucket versioning [#turn-on-bucket-versioning]

dropshelf deletes a drop's files from the bucket when the drop is purged, and keeps nothing after
that. To make deleted or expired drops recoverable, turn on versioning or replication for the
bucket at your provider. Versioning also lets you restore the bucket to the moment of a database
backup.

## Back up the database [#back-up-the-database]

Back up the database first. Every file goes into the bucket under a new name and is never changed,
so a bucket copy taken after the database backup holds every file that backup points at.

To back up with `pg_dump`, run:

```bash
pg_dump --format=custom --file=dropshelf-$(date +%F).dump "$DROPSHELF_DATABASE_URL"
```

A managed database's snapshots or point-in-time recovery are usually the better answer, and need no
command.

## Back up the bucket [#back-up-the-bucket]

With versioning on, there is nothing to copy. Note the time of the database backup: that is the
point to restore the bucket to.

Without versioning, copy the bucket after the database backup finishes. On S3, for example:

```bash
aws s3 sync s3://<bucket> s3://<backup-bucket>
```

On a single host with the `fs` driver, copy the `storage.fs.root` directory instead:

```bash
rsync -a <fs-root>/ <backup-directory>/
```

A drop purged between the database backup and the bucket copy loses its files from the copy. Keep
the gap short, or use versioning.

## Restore [#restore]

<Callout type="warn">
A restore replaces the instance's data with the backup. Every change made after the backup was
taken is lost.
</Callout>

To restore an instance on Kubernetes:

1. Stop the servers, so nothing writes while you restore.

   ```bash
   kubectl -n dropshelf scale deployment/dropshelf --replicas=0
   ```

2. Restore the bucket to the time of the database backup, if it lost files. With versioning, use
   your provider's tools to restore the objects as they were then. Without it, copy your bucket
   backup back.

3. Create an empty database and restore the backup into it.

   ```bash
   pg_restore --dbname=<empty-database-url> dropshelf-<date>.dump
   ```

4. If the restored database has a new address, set `DROPSHELF_DATABASE_URL` in the Secret to it.

5. Start the servers again, on the release that took the backup or a newer one.

   ```bash
   kubectl -n dropshelf scale deployment/dropshelf --replicas=<replicas>
   ```

   `<replicas>` is your `replicaCount`, 2 by default. A newer release migrates the restored
   database at startup. With `migrations.mode: job`, run `helm upgrade` with your values file
   before you scale up, so the migration Job runs first.

## Confirm it worked [#confirm-it-worked]

1. Check that every pod is ready.

   ```bash
   kubectl -n dropshelf rollout status deployment/dropshelf
   ```

2. List the drops the restored database holds.

   ```bash
   kubectl exec -n dropshelf deploy/dropshelf -- /dropshelf-server list
   ```

3. Open a few drops, among them one published shortly before the backup. A drop that opens proves
   its files are in the bucket, which readiness does not.

To trust a backup, restore it now and then into a separate database and namespace, and run the same
checks there.

## Next steps [#next-steps]

- [Upgrade dropshelf](/docs/self-host/operate/upgrade)
- [Choose object storage](/docs/self-host/configure/storage)
- [Troubleshoot an instance](/docs/self-host/operate/troubleshooting)
- [Configuration reference](/docs/reference/configuration#database)
