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:
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:bashNote
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 thesecretkey of the Kubernetes secret, so the key must have exactly this name.At the next step, encode the contents of the file to base64:
bashCreate a Kubernetes secret file (for example
secret.yaml) and put the output of the previous command to the data.secret field. For example:yamlApply this secret to the target cluster where you want to perform backups using the following command:
bashPrepare the
postgresql-backup-cr.yamlfile with thePostgreSQLBackupCustom Resource (CR) to deploy the Cennso PostgreSQL Backup in your Management Cluster. For example:yamlThe metadata.name must match the metadata.name of the
PostgreSQLInstanceyou 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
PostgreSQLBackupresource has restrictions on some configurable values. For more information, see the Configuration restrictions section.Apply the prepared CR to your Management Cluster:
bashCheck 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:
The output should look like this:
Now you can proceed to restore your database from the backup:
Pause the
PostgreSQLInstancerelated 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 thePostgreSQLInstance:bashThe output should look like this:
textThen pause the
PostgreSQLInstancewith the following command:bashPause the Crossplane
Objectthat manages thePerconaPGClusterof your PostgreSQL cluster. ThePostgreSQLInstancecomposes severalObjectresources, and the one with thePerconaPGClusterkind keeps thePerconaPGClusteron the target cluster in sync with the composition. While it is paused, the provider does not revert the changes you make to thePerconaPGCluster. To find its name, list theObjectresources composed by yourPostgreSQLInstance:bashThe output should look like this. Take the name of the
Objectwith thePerconaPGClusterkind:textThen pause this
Objectwith the following command:bashStop the PostgreSQL cluster on the target cluster. The
PerconaPGClusterhas the same name as yourPostgreSQLInstanceand runs in the namespace of the PostgreSQL cluster. Set spec.pause totrue, 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:bashWait until the operator reports the cluster as
pausedand no PostgreSQL Pods are left:bashThe output should look like this:
textThe PersistentVolumeClaims of the instances must still be present. Note their names, as you need them for the next step:
bashThe output should look like this:
textDeploy 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:yamlReplace
{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 examplepostgresql-instance-instance1-2bl2-pgdata. Use the image of thePostgreSQLBackupif you changed spec.image there.Create a second file, for example
restore-replica.yaml, with the same content, but set metadata.name topg-restore-replicaand 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:
bashThe output should look like this:
textRestore the backup to the data volume of the primary. Open a shell in the restore Pod of the primary:
bashRun 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:bashList 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
PostgreSQLInstanceas the Restic host name. If spec.additionalResticOpts.backupExtraArgs of yourPostgreSQLBackupcontains--insecure-tls, add it to everyresticcommand in this step as well:bashMove the current data directory and WAL directory of the instance aside. The directory names contain the PostgreSQL major version, for example
pg18andpg18_walfor PostgreSQL18:bashRestore the chosen snapshot to a temporary directory on the data volume. The snapshot contains the
pg_basebackupoutput under the/backup/pgdatapath:bashMove 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_walsubdirectory. You do not need to create the WAL directory yourself, as the instance movespg_walto its WAL directory on startup and replaces it with a symbolic link:bashCheck 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:
bashThe output should look like this:
textLeave the shell with
exit.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 bypg_controldatamust match the one of the primary.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
Endpointsresources in the namespace of the PostgreSQL cluster:{POSTGRESQL-INSTANCE}-ha-configholds theinitializeannotation 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}-haholds theoptimeannotation 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
initializeannotation to the system identifier you noted in step 5 and remove theoptimeannotation:bashCheck that the
initializeannotation now holds the new system identifier:bashThe output should look like this:
textStart the PostgreSQL cluster again. Set spec.pause of the
PerconaPGClusterback tofalse. 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:bashWait until the operator reports the cluster as
readyand all PostgreSQL Pods are running, with one of them as the primary and the others as replicas:bashThe output should look like this:
textFinally, 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/pausedannotation from theObjectwith thePerconaPGClusterkind and from thePostgreSQLInstance:bashCheck that the
PostgreSQLInstanceis synced and ready again:bashThe output should look like this:
textVerify that the restored data is in place before you continue with the next step.
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.oldandpg18_wal.old, together with the emptyrestoredirectory 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:bashThen 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.