CrunchyData / postgres-operator

Production PostgreSQL for Kubernetes, from high availability Postgres clusters to full-scale database-as-a-service.
https://access.crunchydata.com/documentation/postgres-operator/v5/
Apache License 2.0
3.95k stars 593 forks source link

Stanza cannot be set for backups in k8s #3220

Open cdaller opened 2 years ago

cdaller commented 2 years ago

Overview

I am trying to backup/restore to an S3 bucket using the non-default stanza (which is "db"), but I do not manage to set a stanza on the backup.

I am following the guide from https://www.crunchydata.com/blog/cloud-data-sources-for-the-postgres-operator-for-kubernetes

Environment

Steps to Reproduce

apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
  name: hippo-source
spec:
  image: registry.developers.crunchydata.com/crunchydata/crunchy-postgres:centos8-14.0-0
  postgresVersion: 14
  instances:
    - name: instance1
      dataVolumeClaimSpec:
        accessModes:
        - "ReadWriteOnce"
        resources:
          requests:
            storage: 1Gi
  backups:
    pgbackrest:
      image: registry.developers.crunchydata.com/crunchydata/crunchy-pgbackrest:centos8-2.36-0
      manual:
        repoName: repo1
      configuration:
      - secret:
          name: s3-key
      global:
        repo1-path: /rc-k8s
      repos:
      - name: repo1
        s3:
          bucket: wp-backup-db-2
          endpoint: s3.eu-central-1.amazonaws.com
          region: eu-central-1

To restore the data I want to use the following postgresCluster (taken from tutorial). Here I can set a stanza name, but I cannot do so in the backup!

apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
  name: hippo
spec:
  dataSource:
    pgbackrest:
      stanza: rc-k8s
      configuration:
      - secret:
          name: s3-key
      global:
        repo1-path: /rc-k8s
      repo:
        name: repo1
        s3:
          bucket: wp-backup-db-2
          endpoint: s3.eu-central-1.amazonaws.com
          region: eu-central-1
  image: registry.developers.crunchydata.com/crunchydata/crunchy-postgres:centos8-14.0-0
  postgresVersion: 14
  instances:
    - name: instance1
      dataVolumeClaimSpec:
        accessModes:
        - "ReadWriteOnce"
        resources:
          requests:
            storage: 1Gi
  backups:
    pgbackrest:
      image: registry.developers.crunchydata.com/crunchydata/crunchy-pgbackrest:centos8-2.36-0
      repos:
      - name: repo1
        volume:
          volumeClaimSpec:
            accessModes:
            - "ReadWriteOnce"
            resources:
              requests:
                storage: 1Gi

I tried to set the stanza in the backup of hippo-source postgresCluster like:

  backups:
    pgbackrest:
      image: registry.developers.crunchydata.com/crunchydata/crunchy-pgbackrest:centos8-2.36-0
      manual:
        repoName: repo1
        options:
         - --type=full
         - --stanza=rctest

But then the error message states

│ hippo-source-backup-zsk7--1-ws4kq time="2022-05-20T10:54:21Z" level=info msg="command to execute is [pgbackrest backup --stanza=db --repo=1 --type=full --stanza=rctest]"
│ hippo-source-backup-zsk7--1-xkmsd time="2022-05-20T10:54:20Z" level=info msg="stderr=[ERROR: [031]: option 'stanza' cannot be set multiple times\n]"  

I had a look into the source code and a quick search found this piece of code in bgbackrest.go:

// generateBackupJobSpecIntent generates a JobSpec for a pgBackRest backup job
func generateBackupJobSpecIntent(postgresCluster *v1beta1.PostgresCluster,
    repo v1beta1.PGBackRestRepo, serviceAccountName string,
    labels, annotations map[string]string, opts ...string) (*batchv1.JobSpec, error) {

    selector, containerName, err := getPGBackRestExecSelector(postgresCluster, repo)
    if err != nil {
        return nil, errors.WithStack(err)
    }

    repoIndex := regexRepoIndex.FindString(repo.Name)
    cmdOpts := []string{
        "--stanza=" + pgbackrest.DefaultStanzaName,
        "--repo=" + repoIndex,
    }
    cmdOpts = append(cmdOpts, opts...)

There it seems that for backups always the default stanza name is used and it is not possible to use a custom stanza.

Apart from that the tutorial worked out fine. So my S3 credentials are correct and the backup can be created (though with stanza "db" and restored).

EXPECTED

I expect that there is a configuration option to set the stanza for backups.

ACTUAL

The default stanza name is always used for backups. For restores I can define a stanza name.

andrewlecuyer commented 2 years ago

@cdaller thank you for the detailed issue submission.

As you are seeing, PGO is currently opinionated about the stanza for any repos it manages, and will therefore always set the stanza to db .

However, we have added an enhancement to our backlog to enable support for custom stanzas in a future PGO release.

Thanks again!