> For the complete documentation index, see [llms.txt](https://docs.cennso.com/cennso/cennso-db/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-backup.md).

# Cennso Database Backup

A Cennso Database Backup is represented by the `DBBackup` Custom Resource (CR) and defines a job to back up MySQL instances managed by the [Cennso Database Operator](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-db-operator.md).

> [!IMPORTANT]
> To make a backup of your databases, you must first set the **spec.backup.enabled** parameter of the `DBInstance` Custom Resource to `true`. For more information, see the [Deploy Cennso Database](https://docs.cennso.com/cennso/cennso-db/latest/deployment-guides/cennso-database.md) document.

The sections below provide information about configuration parameters that you can specify in the `DBBackup` CR. For more information on how to make a backup and restore from it, see the [Back up and restore](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/backup.md) document.

## Configuration restrictions

The values configured in the `DBBackup` Custom Resource have the following restrictions:

- The `DBBackup` claim name must match the `DBInstance` claim name for the database instance you want to back up.
- **spec.namespace** must match the Kubernetes namespace where the database instance is deployed.
- The MySQL cluster must run at least one read replica. The backup job runs against a replica instead of the primary, so the target `DBInstance` must have **spec.mysql.size** set to `2` or more. If the cluster runs only a single instance, the backup job cannot find a pod to back up and fails with the error: `ERROR: No read replica found. Backup can be performed only on a read replica.`.
- The following parameters must not contain single quote (`'`) characters:
  - **spec.resticRepository**
  - **spec.additionalResticOpts.cacheDir**
  - **spec.additionalResticOpts.initExtraArgs**
  - **spec.additionalResticOpts.pruneExtraArgs**
  - **spec.additionalResticOpts.backupExtraArgs**
  - **spec.xtradbExtraArgs**
- The backup directory specified by **spec.backupDirectory** must be a subdirectory under `/backup` or `/var/lib/mysql/backup`. Empty path segments, `.` path segments, and `..` path segments are not allowed.

## Kubernetes cluster secret reference

To deploy the Cennso Database Backup to a Core Cluster, you must specify a reference to the [Kubernetes secret](https://kubernetes.io/docs/concepts/configuration/secret/) so the cluster is accessible. You can do this using the following configuration parameters:

```yaml
spec:
  connectionSecretRef:
    name: {CORE-CLUSTER-NAME}
    namespace: {CORE-CLUSTER-SECRET-NAMESPACE}
    key: kubeconfig
```

Set **spec.connectionSecretRef.name** to the name of the Core Cluster where the Cennso Database Backup should be installed. The **spec.connectionSecretRef.namespace** configuration parameter defines the Kubernetes Namespace on the Management Cluster where the Kubernetes secret to access this Core Cluster is stored. The **spec.connectionSecretRef.key** is an optional configuration parameter that defines the key in the Kubernetes secret which contains the kubeconfig for the Core Cluster. Its value defaults to `kubeconfig`.

## Image configuration

You can use the following configuration parameters to specify the container image for the backup jobs, along with the secret to pull it:

```yaml
spec:
  image:
    registry: {BACKUP-JOB-IMAGE-REGISTRY}
    tag: {BACKUP-JOB-IMAGE-TAG}
  imagePullSecretRef:
    name: {SECRET-NAME}
```

The backup job container runs `kubectl` to start the backup inside the XtraBackup sidecar of a MySQL instance. The **spec.image.registry** configuration parameter is the full repository of the image, and **spec.image.tag** is its tag. The image must provide `kubectl` compatible with the Core Cluster.

## Backup jobs placement

The backup jobs specified by the Cennso Database Backup CR are deployed in the Kubernetes namespace specified by the following configuration option:

```yaml
spec:
  namespace: {BACKUP-JOB-NAMESPACE}
```

## Backup jobs scheduling and execution

To configure when backup jobs should be performed, you can use the following configuration option:

```yaml
spec:
  schedule: {BACKUP-SCHEDULE}
```

The `{BACKUP-SCHEDULE}` must be a cron expression which specifies how often backup jobs are performed. For example:

- `spec.schedule: "0 6 * * 1"` - run a backup job weekly on Monday at 6 AM
- `spec.schedule: "@daily"` - run a backup job once a day at midnight
- `spec.schedule: "0 */3 * * *"` - run a backup job every three hours

For more information, see the [Writing a CronJob spec](https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/#writing-a-cronjob-spec) documentation.

You can also control how long a backup run may take, how many times failures are retried, and how many completed jobs are kept. These options are optional and have the following default values:

```yaml
spec:
  activeDeadlineSeconds: 7200
  backoffLimit: 2
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 3
```

- The **spec.activeDeadlineSeconds** parameter sets the maximum time a single backup run may take before Kubernetes terminates it and marks the run as failed. Because only one backup run is allowed at a time, this prevents a hung run from blocking all following scheduled backups. It is highly recommended to increase this value for large databases whose backups can take longer.
- The **spec.backoffLimit** parameter sets how many times a failed backup run is retried before it is marked as failed.
- The **spec.successfulJobsHistoryLimit** and **spec.failedJobsHistoryLimit** parameters set how many completed successful and failed backup jobs Kubernetes retains. Failed jobs are kept so that their logs can be inspected when a backup fails.

## Backup directory

To specify the temporary directory where XtraBackup writes the local backup before it is uploaded to the Restic repository, use **spec.backupDirectory**:

```yaml
spec:
  backupDirectory: "/var/lib/mysql/backup/db-backup"
```

> [!IMPORTANT]
> The value must be a subdirectory under `/backup` or `/var/lib/mysql/backup`.

## Restic settings

The backup container uses [Restic](https://restic.net/) to upload `XtraBackup` output to a repository. To configure a [Restic repository](https://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html) you can use the following configuration option:

```yaml
spec:
  resticRepository: s3:http://minio:9000/mysql-backup
```

The value of this parameter must be a valid URI supported by the `restic init` command.

In addition, you can pass extra configuration options for the Restic repository initialization and the backup procedure using the following configuration options:

```yaml
spec:
  additionalResticOpts:
    initExtraArgs: "--insecure-tls"
    backupExtraArgs: "--insecure-tls"
```

The list of supported options you can find in the official documentation for the [init](https://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html) and [backup](https://restic.readthedocs.io/en/latest/040_backup.html) commands.

To save disk space and apply a retention policy, you can remove old backups using the following configuration options:

```yaml
spec:
  additionalResticOpts:
    pruneRepository: true
    pruneExtraArgs: "--prune --keep-last 7"
```

If you set the **spec.additionalResticOpts.pruneRepository** to `true` and do not specify `--prune` in **spec.additionalResticOpts.pruneExtraArgs**, only metadata of the affected snapshots is removed. Include `--prune` in **spec.additionalResticOpts.pruneExtraArgs** if you also want Restic to remove unreferenced data.

If you set **spec.additionalResticOpts.pruneRepository** to `true`, you must also specify a retention policy in **spec.additionalResticOpts.pruneExtraArgs** (for example, `--keep-last 7`). The backup job runs the `restic forget` command with these arguments, and the command cannot run without a policy. If you leave **spec.additionalResticOpts.pruneExtraArgs** empty, the backup job fails with the following error:

```
Fatal: no policy was specified, no snapshots will be removed
command terminated with exit code 1
```

For more information on pruning restic repositories, see the [official documentation](https://restic.readthedocs.io/en/stable/060_forget.html).

To specify the directory where Restic stores its local cache during backup operations, use **spec.additionalResticOpts.cacheDir**:

```yaml
spec:
  additionalResticOpts:
    cacheDir: "/tmp/restic-cache"
```

## Backend storage credentials

Using the Restic utility, the backups are stored in one of the supported backends. These backends often need authorisation and authentication. You can use the following configuration options to refer to a Kubernetes Secret that stores settings for authorisation and authentication:

```yaml
spec:
  credentialsSecretRef:
    name: "{SECRET-NAME}"
```

For more information, see the [Back up and restore](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/backup.md) document.

## Additional options for XtraBackup

You can use **spec.xtradbExtraArgs** to specify additional configuration options of the [XtraBackup](https://docs.percona.com/percona-xtrabackup/8.4/), which are passed to this utility when performing a backup:

```yaml
spec:
  xtradbExtraArgs: "--compress --tables-exclude=\"example.cdr\""
```

You can find the list of available configuration options in the [official documentation](https://docs.percona.com/percona-xtrabackup/8.0/index.html).
