Setting up continuous backup and restore for the EDB Postgres service

You can configure EDB Postgres to allow backup and restore with S3 storage.

Creating a backup with S3 storage

You can create a backup of your database instance in S3 storage.

  1. Set the following environment variables:
    • Set the EDB_INSTANCE_NAME environment variable to the name of the database instance for which you want to create a backup.
      export EDB_INSTANCE_NAME=<name>
    • Set the BACKUP_EDB_INSTANCE_NAME environment variable to the name of the backup database instance.
      export BACKUP_EDB_INSTANCE_NAME=<name>
    • Set the EDB_INSTANCE_TYPE environment variable to the EDB Postgres database type. Valid values are Enterprise or Standard.
      export EDB_INSTANCE_TYPE=<EDBInstanceType>
  2. Create a secret with access credentials to S3 storage.
    kubectl create secret generic s3-creds \
      --from-literal=ACCESS_KEY_ID=<access key> \
      --from-literal=ACCESS_SECRET_KEY=<secret key>
    # --from-literal=ACCESS_SESSION_TOKEN=<session token> # if required
  3. Modify and apply your database custom resource YAML file with the following backup configuration under the spec key:
    apiVersion: edb.cpd.ibm.com/v1
    kind: CPDEdbInstance
    metadata:
      name: ${EDB_INSTANCE_NAME}
    spec:
      type: ${EDB_INSTANCE_TYPE}
      imageRepository: "cp.stg.icr.io/cp/cpd"
      # Travis pipeline will insert here
      members: 1
      version: "12.9"
      blockStorageClass: nfs-client
      shutdown: false
      storageSize: 100Gi
      resources:
        requests:
          cpu: 1
          memory: 4Gi
        limits:
          cpu: 1
          memory: 4Gi
      backup:
        barmanObjectStore:
          destinationPath: <destination_path>
          endpointURL: <endpoint_url>
          s3Credentials:
            accessKeyId:
              key: ACCESS_KEY_ID
                name: <S3_secret_name>
                inheritFromIAMRole: false  
              secretAccessKey:
                key: ACCESS_SECRET_KEY
                name: <S3_secret_name>
  4. Run the following command to apply a backup configuration:
    cat <<EOF | oc apply -n ${PROJECT_CPD_INST_OPERANDS} -f -
    apiVersion: postgresql.k8s.enterprisedb.io/v1
    kind: Backup
    metadata:
      name: ${BACKUP_EDB_INSTANCE_NAME}
        labels:
          icpdsupport/ignore-on-nd-backup: 'true'
          velero.io/exclude-from-backup: 'true'
          icpdsupport/addOnId: edb
        spec:
          cluster:
            name: ${EDB_INSTANCE_NAME}-edb-db
    EOF
  5. Check that your S3 storage location contains a backup of your instance. The backup will be located in the destination path with the corresponding base and wals folders.

Restore from a backup

You can restore data from a backup of your database instance that is located in S3 storage.

  1. Ensure that the secret you created when you deployed the backup database instance exists in the namespace. If not, you will need to create the secret again. Ensure that the backup configuration is applied to your project. If not, you will need to re-apply the backup configuration.
  2. To restore your EDB database instance from a backup stored in S3 storage, modify and apply the custom resource configuration as follows:
    apiVersion: edb.cpd.ibm.com/v1
    kind: CPDEdbInstance
    metadata:
      name: ${EDB_INSTANCE_NAME}
    spec:
      type: ${EDB_INSTANCE_TYPE}
      imageRepository: "cp.stg.icr.io/cp/cpd"
      # Travis pipeline will insert here
      members: 1
      version: "12.9"
      blockStorageClass: nfs-client
      shutdown: false
      storageSize: 100Gi
      resources:
        requests:
          cpu: 1
          memory: 4Gi
        limits:
          cpu: 1
          memory: 4Gi
    
      recovery:
        source: ${EDB_INSTANCE_NAME}-edb-db
    
      externalClusters:
        - name: ${EDB_INSTANCE_NAME}-edb-db
          barmanObjectStore:
            destinationPath: <destination_path>
            # Replace with your S3 url
            endpointURL: <endpoint_url>
            s3Credentials:
              accessKeyId:
                key: ACCESS_KEY_ID
                name: <S3_secret_name>  
              secretAccessKey:
                key: ACCESS_SECRET_KEY
                name: <S3_secret_name>
            wal:
              maxParallel: 8