For AI agents: this documentation is indexed for you in llms.txt, and all of it in /llms.txt. Every page is also available as markdown: append .md to its address.

Back up and restore

This guide describes how to use the PostgreSQLBackup Custom Resource to:

Perform a backup of a database

This section describes how you can prepare backups of your database deployed using the Cennso PostgreSQL Instance.

Important

The backup job runs against a read replica instead of the primary, so the target PostgreSQLInstance must run at least one read replica, with spec.instances[].replicas set to 2 or more in at least one instance set. 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 ready read replica found. Backup can be performed only on a read replica.. The job also refuses a replica that does not stream WAL from the primary or lags behind it by more than spec.maxReplicationLagBytes. For more information, see the Configuration restrictions and Backup source sections.

To deploy a Cennso PostgreSQL Backup, execute the following steps:

  1. Prepare the Kubernetes secret in the Kubernetes namespace where your PostgreSQL cluster is up and running. This secret should contain the list of environment variables with the data needed for the authorisation at a ResticOpens in a new tab backend. To prepare a secret, create a file, for example secret.data, with the list of environment variables which you need to authorise at a restic backend. For example:

    bash

    Note

    To find information about all supported environment variables, see the official documentationOpens in a new tab.

    The backup job loads this file in a shell before it runs Restic, so each line must start with export. The job reads the file from the secret key of the Kubernetes secret, so the key must have exactly this name.

    At the next step, encode the contents of the file to base64:

    bash

    Create a Kubernetes secret file (for example secret.yaml) and put the output of the previous command to the data.secret field. For example:

    yaml

    Apply this secret to the target cluster where you want to perform backups using the following command:

    bash
  2. Prepare the postgresql-backup-cr.yaml file with the PostgreSQLBackup Custom Resource (CR) to deploy the Cennso PostgreSQL Backup in your Management Cluster. For example:

    yaml

    The metadata.name must match the metadata.name of the PostgreSQLInstance you want to back up, and spec.namespace must match the namespace where this instance runs. Omit spec.connectionSecretRef to run the backup on the Management Cluster.

    Important

    The PostgreSQLBackup resource has restrictions on some configurable values. For more information, see the Configuration restrictions section.

  3. Apply the prepared CR to your Management Cluster:

    bash
  4. Check your target cluster for the created Kubernetes CronJob to check if backups are configured to run at the specified time:

    bash

The example above shows the Custom Resource that creates a Kubernetes CronJob. This CronJob performs a backup of the PostgreSQL cluster named {POSTGRESQL-INSTANCE-NAME}, deployed in the Core Cluster specified by the {CORE-CLUSTER-NAME} and located in the namespace specified by the {NAMESPACE-WITH-POSTGRESQL-CLUSTER}. It performs a backup each day at 00:00 UTC from one of the read replicas of the cluster using pg_basebackup and uploads it to the Restic repository. The {SECRET-NAME} must specify the name of the Kubernetes secret which you created in the previous step.

For more information about available configuration options, see the Cennso PostgreSQL Backup document.

Restore a database from a backup

If your database or the data was corrupted in a way that you can not continue to run PostgreSQL instances on existing data files, you have to restore the database from a backup.

The steps below restore the backup to the data volumes of an existing PostgreSQL cluster. If the PostgreSQL cluster no longer exists, deploy a new one first using the instructions given in the Deploy Cennso PostgreSQL document. The new cluster must have the same name and the same number of instances as the one you backed up.

Before restoring the database from a backup, make sure you know the names of the PostgreSQL instances and their roles. To get the PostgreSQL Pod names and their roles, use the following command:

bash

The output should look like this:

text

Now you can proceed to restore your database from the backup:

  1. Pause the PostgreSQLInstance related to your PostgreSQL cluster on the Management Cluster. While it is paused, Crossplane does not reconcile the resource, so the changes you make to the cluster in the next steps are not reverted. First, find the name of the PostgreSQLInstance:

    bash

    The output should look like this:

    text

    Then pause the PostgreSQLInstance with the following command:

    bash
  2. Pause the Crossplane Object that manages the PerconaPGCluster of your PostgreSQL cluster. The PostgreSQLInstance composes several Object resources, and the one with the PerconaPGCluster kind keeps the PerconaPGCluster on the target cluster in sync with the composition. While it is paused, the provider does not revert the changes you make to the PerconaPGCluster. To find its name, list the Object resources composed by your PostgreSQLInstance:

    bash

    The output should look like this. Take the name of the Object with the PerconaPGCluster kind:

    text

    Then pause this Object with the following command:

    bash
  3. Stop the PostgreSQL cluster on the target cluster. The PerconaPGCluster has the same name as your PostgreSQLInstance and runs in the namespace of the PostgreSQL cluster. Set spec.pause to true, so the Cennso PostgreSQL Operator shuts down the PostgreSQL instances and PgBouncerOpens in a new tab and removes their Pods. The persistent volumes with the data files are kept, so you can overwrite the data files in the next steps:

    bash

    Wait until the operator reports the cluster as paused and no PostgreSQL Pods are left:

    bash

    The output should look like this:

    text

    The PersistentVolumeClaims of the instances must still be present. Note their names, as you need them for the next step:

    bash

    The output should look like this:

    text
  4. Deploy a restore Pod for every PostgreSQL instance of the cluster, one for the primary and one for each read replica. Each Pod mounts the PersistentVolumeClaim of one instance, so you can write the restored data files to it. You must restore the same backup to all instances. If you restore only the primary, the read replicas keep data files from a newer timeline and cannot follow the restored primary.

    Create a file, for example restore-primary.yaml, with the following content:

    yaml

    Replace {PRIMARY-PVC-NAME} with the PersistentVolumeClaim of the primary instance from the previous step. The claim name is the Pod name of the instance followed by -pgdata, for example postgresql-instance-instance1-2bl2-pgdata. Use the image of the PostgreSQLBackup if you changed spec.image there.

    Create a second file, for example restore-replica.yaml, with the same content, but set metadata.name to pg-restore-replica and spec.volumes[].persistentVolumeClaim.claimName to the PersistentVolumeClaim of the read replica. If the cluster runs more than one read replica, create one file per replica.

    Apply the files to the target cluster and wait until the Pods are running:

    bash

    The output should look like this:

    text
  5. Restore the backup to the data volume of the primary. Open a shell in the restore Pod of the primary:

    bash

    Run all commands of this step inside this shell. First, load the Restic credentials from the mounted secret and set the repository. Use the same value as spec.resticRepository of your PostgreSQLBackup:

    bash

    List the snapshots of your PostgreSQL cluster and choose the one you want to restore. The backup job stores the snapshots with the name of the PostgreSQLInstance as the Restic host name. If spec.additionalResticOpts.backupExtraArgs of your PostgreSQLBackup contains --insecure-tls, add it to every restic command in this step as well:

    bash

    Move the current data directory and WAL directory of the instance aside. The directory names contain the PostgreSQL major version, for example pg18 and pg18_wal for PostgreSQL 18:

    bash

    Restore the chosen snapshot to a temporary directory on the data volume. The snapshot contains the pg_basebackup output under the /backup/pgdata path:

    bash

    Move the restored data directory to the place of the data directory and restrict its permissions, as PostgreSQL refuses to start with a data directory that other users can access. The restored data directory contains the WAL streamed during the backup in its pg_wal subdirectory. You do not need to create the WAL directory yourself, as the instance moves pg_wal to its WAL directory on startup and replaces it with a symbolic link:

    bash

    Check that the restored data directory is a valid PostgreSQL cluster and note its system identifier. It must be the same on all instances after the restore:

    bash

    The output should look like this:

    text

    Leave the shell with exit.

  6. Repeat the previous step in the restore Pod of every read replica, for example in pg-restore-replica. Restore the same snapshot there. After the restore, the system identifier reported by pg_controldata must match the one of the primary.

  7. Update the cluster state of Patroni, the high availability manager that runs inside every PostgreSQL instance. Patroni stores this state in annotations of two Kubernetes Endpoints resources in the namespace of the PostgreSQL cluster:

    • {POSTGRESQL-INSTANCE}-ha-config holds the initialize annotation with the system identifier of the cluster. If it differs from the system identifier of the restored data directory, Patroni refuses to start the instances, as they appear to belong to a different cluster.
    • {POSTGRESQL-INSTANCE}-ha holds the optime annotation with the last WAL position of the former primary. The restored data is behind this position, so Patroni treats all instances as lagging and does not select a new primary.

    Set the initialize annotation to the system identifier you noted in step 5 and remove the optime annotation:

    bash

    Check that the initialize annotation now holds the new system identifier:

    bash

    The output should look like this:

    text
  8. Start the PostgreSQL cluster again. Set spec.pause of the PerconaPGCluster back to false. The Cennso PostgreSQL Operator starts the PostgreSQL instances and PgBouncer again. Each instance finds the restored data directory on its volume and Patroni elects one of the instances as the new primary:

    bash

    Wait until the operator reports the cluster as ready and all PostgreSQL Pods are running, with one of them as the primary and the others as replicas:

    bash

    The output should look like this:

    text

    Finally, resume the Crossplane resources you paused in steps 1 and 2 on the Management Cluster, so that Crossplane manages the PostgreSQL cluster again. Remove the crossplane.io/paused annotation from the Object with the PerconaPGCluster kind and from the PostgreSQLInstance:

    bash

    Check that the PostgreSQLInstance is synced and ready again:

    bash

    The output should look like this:

    text

    Verify that the restored data is in place before you continue with the next step.

  9. Remove the old data from the data volumes. The data directory and WAL directory you moved aside in step 5 are still on the volume of every instance as pg18.old and pg18_wal.old, together with the empty restore directory left by Restic. They take up as much disk space as the restored data, so remove them after you verify the restored data. Run the following commands in the restore Pod for the primary and in the restore Pod for each replica:

    bash

    Then delete the restore Pods, as they are no longer needed:

    bash

From now on, your PostgreSQL cluster runs with the data restored from the backup. For more information on how to validate your PostgreSQL cluster, see the Validate the PostgreSQL cluster document.