# Restore the cluster from a previously saved backup You can restore from a backup as follows: * On the same cluster where you made a backup * On [a new cluster deployed in a different Kubernetes-based environment](backups-restore-to-new-cluster.md). This document focuses on the restore to the same cluster. ## Restore scenarios This document covers the following restore scenarios: * [Restore from a full backup](#restore-from-a-full-backup) - the restore from a backup without point-in-time * [Point-in-time recovery](#restore-with-point-in-time-recovery) - restore to a specific time, a specific or latest transaction or skip a specific transaction during a restore. This ability requires that you [configure storing binlogs for point-in-time recovery](backups-pitr.md) * [Restore when a backup has different passwords](#restore-the-cluster-when-backup-has-different-passwords) To restore from a backup, you create a special Restore object using a special restore configuration file. The example of such file is [deploy/backup/restore.yaml :octicons-link-external-16:](https://github.com/percona/percona-xtradb-cluster-operator/blob/main/deploy/backup/restore.yaml). You can check available options in the [restore options reference](restore-cr.md). Note that you **cannot restore** to [emptyDir and hostPath volumes](storage.md), but you can make a backup from such storage (i. e., from emptyDir/hostPath to S3), and later restore it to a [Persistent Volume :octicons-link-external-16:](https://kubernetes.io/docs/concepts/storage/persistent-volumes/). --8<-- [start:backup-prepare] ## Before you start 1. Make sure that the cluster is running. 2. List the cluster to find the correct cluster name. Replace the `` with your value: ``` {.bash data-prompt="$" } $ kubectl get pxc -n ``` 3. List backups to retrieve the desired backup name. Replace the `` with your value: ``` {.bash data-prompt="$" } $ kubectl get pxc-backup -n ``` 4. For point-in-time recovery, disable storing binlogs point-in-time functionality on the existing cluster. You must do it regardless of whether you made the backup with point-in-time recovery or without it. Use the following command and replace the cluster name and the `` with your values: ```{.bash data-prompt="$" } $ kubectl patch pxc cluster1 \ -n \ --type merge \ -p '{"spec":{"backup":{"pitr":{"enabled":false}}}}' ``` --8<-- [end:backup-prepare] ## Restore from a full backup To restore your Percona XtraDB cluster from a backup, define a `PerconaXtraDBClusterRestore` custom resource. Set the following keys: * `spec.pxcCluster`: the name of the target cluster * `spec.backupName`: the name of your backup, Pass this configuration to the Operator: === "via the YAML manifest" 1. Edit the [deploy/backup/restore.yaml :octicons-link-external-16:](https://github.com/percona/percona-xtradb-cluster-operator/blob/main/deploy/backup/restore.yaml) file and specify the following keys: ```yaml apiVersion: pxc.percona.com/v1 kind: PerconaXtraDBClusterRestore metadata: name: restore1 spec: pxcCluster: cluster1 backupName: backup1 ``` 2. Start the restore with this command: ``` {.bash data-prompt="$" } $ kubectl apply -f deploy/backup/restore.yaml -n ``` === "via the command line" You can skip creating a separate file by passing YAML content directly: ``` {.bash data-prompt="$" } $ cat < -o jsonpath='{.status.latestRestorableTime}' ``` 2. Set the following keys for the `PerconaXtraDBClusterRestore` custom resource: * `spec.pxcCluster`: the name of the target cluster * `spec.backupName`: the name of your backup * for the `pitr` section: * `type`: one of the following values: * `date` - roll back to specific date, * `transaction` - roll back to a specific transaction (available since Operator 1.8.0), * `latest` - recover most recent transaction, * `skip` - skip a specific transaction (available since Operator 1.7.0). * `date`: is used with `type=date` option and contains the value in the datetime format, * `gtid`: is used with `type=transaction` option and contains exact GTID of a transaction **which follows** the last transaction included into the recovery (available since the Operator 1.8.0) * (optional) `storageName`: the exact name of the storage. Note that you must have [already defined the storage](backups-storage.md) in the `backup.storages` subsection of the `deploy/cr.yaml` file. 3. Pass this configuration to the Operator: === "via the YAML manifest" 1. Edit the [deploy/backup/restore.yaml :octicons-link-external-16:](https://github.com/percona/percona-xtradb-cluster-operator/blob/main/deploy/backup/restore.yaml) file. The sample configuration may look as follows: ```yaml apiVersion: pxc.percona.com/v1 kind: PerconaXtraDBClusterRestore metadata: name: restore1 spec: pxcCluster: cluster1 backupName: backup1 pitr: type: date date: "2020-12-31 09:37:13" backupSource: storageName: s3-us-west ``` 2. Start the restore: ``` {.bash data-prompt="$" } $ kubectl apply -f deploy/backup/restore.yaml ``` === "via the command line" You can skip editing the YAML file and pass its contents to the Operator via the command line. For example: ``` {.bash data-prompt="$" } $ cat <-mysql-init` and puts (or appends) the required `ALTER USER` statement into it. Then MySQL Pods are mounting this init Secret if exist and running corresponding statements on startup. When a new backup is created and successfully finished, the Operator deletes the init Secret. In the above example passwords are changed after backup2 was finished, and then three new backups were created, so the init Secret does not exist. If you want to restore to backup2, you need to create the init secret by your own with the latest passwords as follows. 1. Make a base64-encoded string with needed SQL statements (substitute each `` with the password of the appropriate user): === "in Linux" ``` {.bash data-prompt="$" } $ cat <'; ALTER USER 'root'@'localhost' IDENTIFIED BY ''; ALTER USER 'operator'@'%' IDENTIFIED BY ''; ALTER USER 'monitor'@'%' IDENTIFIED BY ''; ALTER USER 'clustercheck'@'localhost' IDENTIFIED BY ''; ALTER USER 'xtrabackup'@'%' IDENTIFIED BY ''; ALTER USER 'xtrabackup'@'localhost' IDENTIFIED BY ''; ALTER USER 'replication'@'%' IDENTIFIED BY ''; EOF ``` === "in macOS" ``` {.bash data-prompt="$" } $ cat <'; ALTER USER 'root'@'localhost' IDENTIFIED BY ''; ALTER USER 'operator'@'%' IDENTIFIED BY ''; ALTER USER 'monitor'@'%' IDENTIFIED BY ''; ALTER USER 'clustercheck'@'localhost' IDENTIFIED BY ''; ALTER USER 'xtrabackup'@'%' IDENTIFIED BY ''; ALTER USER 'xtrabackup'@'localhost' IDENTIFIED BY ''; ALTER USER 'replication'@'%' IDENTIFIED BY ''; EOF ``` 2. After you obtained the needed base64-encoded string, create the appropriate Secret: ``` {.bash data-prompt="$" } $ kubectl apply -f - < EOF ``` 3. Now you can restore the needed backup as usual.