# Backup and restore

In an installation on your own server, everything that matters is in the
**database**: settings, instructions, knowledge sources, messages, drafts and
the encrypted secrets. So you back up the database — and `MASTER_KEY`.

## What to back up

1. **The database** — with a regular dump.
2. **`MASTER_KEY`** from the `.env` file — **once, off the server**. Without it
   you cannot read the API keys, mailbox access or sign-in clients from the
   database; the backup would be useless.
3. Optionally the whole `.env` (database passwords and other secrets) — in an
   equally safe place.

A database dump contains the customers' mail. Protect it like the mailbox
itself.

## Dumping the database

From the installation's directory:

```
mkdir -p data/backups
docker compose exec -T db mariadb-dump -udraften -p"$DB_PASSWORD" --single-transaction draften | gzip > data/backups/draften-$(date +%F).sql.gz
```

Run it regularly (daily from cron, say) and copy the dumps off the server.

## Restoring

Into an empty database:

```
gunzip -c dump.sql.gz | docker compose exec -T db mariadb -udraften -p"$DB_PASSWORD" draften
```

Then start the installation with the same `MASTER_KEY` that applied at the
time of the backup.

## Watch out

- The `db-data` volume is the live database. **Never** remove the installation
  with `docker compose down -v` unless you have a fresh dump.
- The `data/` directory no longer holds the truth — only text logs, the report
  and dumps are there.
- You can always check in the interface: the Installation page shows whether
  `MASTER_KEY` is set.
