Application Consistent PostgreSQL Backup

If it hasn't been done already, the bitnami Helm repository needs to be added to your local configuration:

# Add bitnami helm repo
$ helm repo add bitnami https://charts.bitnami.com/bitnami

Install the PostgreSQL chart from the bitnami Helm repository:

$ kubectl create namespace postgresql
$ helm install --namespace postgresql postgres bitnami/postgresql --version=11.9.13

Next create a file postgresql-hooks.yaml with the following contents

apiVersion: cr.kanister.io/v1alpha1
kind: Blueprint
metadata:
  name: postgresql-hooks
actions:
  backupPrehook:
    phases:
    - func: KubeExec
      name: makePGCheckPoint
      args:
        namespace: "{{ .StatefulSet.Namespace }}"
        pod: "{{ index .StatefulSet.Pods 0 }}"
        container: postgres-postgresql
        command:
        - bash
        - -o
        - errexit
        - -o
        - pipefail
        - -c
        - |
          PGPASSWORD=${POSTGRES_PASSWORD} psql -U $POSTGRES_USER -c "select pg_start_backup('app_cons');"
  backupPosthook:
    phases:
    - func: KubeExec
      name: afterPGBackup
      args:
        namespace: "{{ .StatefulSet.Namespace }}"
        pod: "{{ index .StatefulSet.Pods 0 }}"
        container: postgres-postgresql
        command:
        - bash
        - -o
        - errexit
        - -o
        - pipefail
        - -c
        - |
          PGPASSWORD=${POSTGRES_PASSWORD} psql -U $POSTGRES_USER -c "select pg_stop_backup();"

And then apply the file using:

$ kubectl --namespace=kasten-io create -f postgresql-hooks.yaml

Finally add the following annotation to the PostgreSQL StatefulSets to instruct K10 to use the above hooks when performing operations on this PostgreSQL instance.

$ kubectl annotate statefulset postgres-postgresql kanister.kasten.io/blueprint='postgresql-hooks' \
     --namespace=postgresql

Finally, use K10 to backup and restore the application.