# Full key rotation

Copy every backup of an environment into new repositories with a new master key, for when a key may have been seen by someone else.


## When you need it

Every repository of an environment's backups is encrypted with a master key. The runner key and your recovery key are passwords that unlock that master key. Replacing a password keeps the master key: whoever copied the master key out of a repository, for example from a leaked runner key, can keep reading it.

A full rotation makes, at every destination, a new repository with a new master key and copies every backup of the environment into it. Afterwards the backups go into the new repositories, and a new runner key opens them.

> [!NOTE]
> Full key rotation is being rolled out. Until Brewless has switched it on, the button is not there and `POST /api/environments/{environment}/backups/rotation` answers `404`.

## Starting one

On the **Backups** tab of an environment, owners and admins click **Rotate all keys**. A dialog says what will happen; after **Rotate all keys** there, Brewless asks you to [confirm that it is you](/backups/getting-started#confirming-that-it-is-you). While the rotation runs, the tab says since when and for how many destinations, and the button waits until it reports.

The same through the API:

| Endpoint | What it does |
| --- | --- |
| `POST /api/environments/{environment}/backups/rotation` | Start a full rotation; the answer is the request's id |
| `GET /api/environments/{environment}/backups/rotation` | The rotation under way, or `null` |

One rotation runs at a time per environment. The copy runs in your own account, as the environment's backup job, and can take as long as a backup of everything; it reads the old repositories and writes the new ones at the same destination, under a new prefix such as `repo-r202612031000`.

## What happens

1. Brewless makes room for the new key in the runner key's secret, which Scaleway limits to 20 versions, by removing the oldest versions. Each removal is in the audit trail (`secrets.version_removed`).
2. At every destination the job makes the new repository, copies every backup of the environment into it, pinned ones included, and checks it. The old repository gets the new password as an extra key, so a backup that still writes there keeps working.
3. Only when every destination succeeded, the job stores the new runner key in your Secret Manager. Brewless then points the environment's backups at the new repositories.
4. Brewless asks you for a new [recovery key](/backups/getting-started#the-recovery-key): your old recovery key opens only the old repositories.

If a destination fails, nothing changes: the runner key and the prefixes stay as they were, and the extra key the old repositories got is removed again.

## The old repositories

Brewless never deletes them, and under Object Lock nobody can before the lock ends. They stay readable with the old runner key, the old recovery key and the new runner key. When you no longer need them, remove them yourself at Scaleway or at the provider of the destination, under the old prefix. A backup that ran between the copy and Brewless hearing that it finished is only in the old repository.

The audit trail records `backup.rotation_started`, `backup.repository_rotated` and `backup.rotation_failed`.
