# Backing up databases

How Serverless SQL and Managed Databases are backed up, from the provider's own export or a direct dump, as plain SQL, and what hourly backups cost.


Every database Brewless manages for an environment is in its backup: a Serverless SQL Database, and a database in a Managed Database for PostgreSQL or MySQL. The runner stores each one as **plain SQL**, the format any `psql` or `mariadb` reads, and restic compresses it and keeps only the parts that changed since the last backup.

## Two ways to take a database

| Way | When | What it does |
| --- | --- | --- |
| **The provider's export** | A daily backup of a Serverless SQL Database, and every backup of a Managed Database | The runner asks Scaleway for an export of the newest backup Scaleway made itself, streams the download, turns it into plain SQL and stores it. The database is not touched: a Serverless SQL Database is not woken |
| **A direct dump** | An hourly or custom schedule on a Serverless SQL Database | The runner signs in and runs `pg_dump`. It sees the database as it is at that moment, but it wakes a Serverless SQL Database for every backup |

When the newest backup Scaleway made is older than 26 hours, the runner dumps the database directly instead and says so in the run's report.

The runner signs in to a Serverless SQL Database with its own IAM application, which may only read. For a Managed Database, Brewless makes a read-only user `brewless_backup` with a random password and puts it in your Secret Manager, from where Scaleway mounts it into the backup job.

> [!WARNING]
> A schedule more often than daily wakes a Serverless SQL Database for every backup, and you pay Scaleway for the time it is awake. `brewless plan` and the Backups tab show what it adds a month before you switch. That figure is an estimate: it assumes each dump keeps the database awake for about two minutes at its minimum size.

## What is stored

For every database, a backup holds two things:

- `db/<name>.sql`: the database as plain SQL (for MySQL and MariaDB a direct dump includes routines, triggers and events);
- `db/<name>.meta.json`: the server version, the extensions and their versions, the encoding, the schemas, the roles and their grants, and an estimate of the rows per table.

No password is stored. A [restore](/running/restoring-backups) creates the extensions first and signs in as your application's own user, so the tables it makes belong to your application.

## A Managed Database you made yourself

A Managed Database instance that Brewless did not make is not a resource of the environment, so its backups do not reach it. Take it into the environment with:

```bash
brewless db:adopt production <instance id> --database=shop --all-databases --exclude=scratch
```

The API behind it is `POST /api/environments/{environment}/databases/adopt`, with `instance`, `database`, `all_databases` and `exclude_databases`.

This needs the right to manage databases. Brewless reads the instance, and it must be in the environment's Scaleway project and region. Brewless then knows it as a resource of the environment. Nothing is made in the instance: no login, no database, and no change to its settings. The audit trail has `resource.adopted`.

With `all_databases`, the backup takes **every** database on the instance, each as an export of its own: for example the central database and one database per organisation of an application on stancl/tenancy. Each database keeps its own history and is restored on its own, by its name. Left out are `rdb`, `_rdb`, `postgres`, `template0` and `template1`, and the names in `exclude_databases`.

Setting the backups up gives the read-only user `brewless_backup` read rights on each database. A database added later, such as a new organisation's, gets them at the next hourly check (`backup.database_access_granted` in the audit trail). Until then the backup reports that database as not readable, and backs up the others.

Without `all_databases`, only `database` is backed up.

## A database that several environments share

A database that more than one environment uses is backed up once, by the first production environment that uses it, or else by the first. The other environments list it as **Backed up with another environment that shares it**.

## Leaving a database out

Add it to `exclude` in `brewless.yml`:

```yaml
environments:
  production:
    backup:
      exclude:
        - database:shop-scratch
```

## What is not backed up

- A **cache** (Managed Database for Redis) is not backed up: it holds nothing that cannot be built again. The Backups tab lists it as unsupported, so the environment is shown as partly protected.
- The database and the buckets of one backup are each taken at their own moment, not at one point in time. A file your application wrote a minute after the database was taken is in the bucket backup, but its row is not in the database backup. See [known limitations](/backups/troubleshooting#known-limitations).
