From 8488c2e2e67e30fe76df3a8d4df4b629daabe37b Mon Sep 17 00:00:00 2001 From: Gerrit Date: Wed, 23 Aug 2023 13:44:09 +0200 Subject: [PATCH] Also copy postgres binary in backup step. (#53) --- cmd/internal/database/postgres/postgres.go | 9 +++++++++ cmd/internal/database/postgres/upgrade.go | 14 +++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cmd/internal/database/postgres/postgres.go b/cmd/internal/database/postgres/postgres.go index 7f94889..059546b 100644 --- a/cmd/internal/database/postgres/postgres.go +++ b/cmd/internal/database/postgres/postgres.go @@ -61,6 +61,15 @@ func (db *Postgres) Check() (bool, error) { // Backup takes a backup of the database func (db *Postgres) Backup() error { + // for new databases the postgres binaries required for Upgrade() cannot be copied before the database is running + // therefore this happens in the backup task where the database is already available + // + // implication: one backup has to be taken before an upgrade can be made + err := db.copyPostgresBinaries(false) + if err != nil { + return err + } + if err := os.RemoveAll(constants.BackupDir); err != nil { return fmt.Errorf("could not clean backup directory: %w", err) } diff --git a/cmd/internal/database/postgres/upgrade.go b/cmd/internal/database/postgres/upgrade.go index 7ae5515..d4f063e 100644 --- a/cmd/internal/database/postgres/upgrade.go +++ b/cmd/internal/database/postgres/upgrade.go @@ -43,7 +43,7 @@ func (db *Postgres) Upgrade() error { } // If this is a database directory, save actual postgres binaries for a later major upgrade - err := db.copyPostgresBinaries() + err := db.copyPostgresBinaries(true) if err != nil { return err } @@ -84,7 +84,7 @@ func (db *Postgres) Upgrade() error { // Check if old pg_config are present and match pgVersion oldPostgresConfigCmd := path.Join(oldPostgresBinDir, postgresConfigCmd) if ok := db.isCommandPresent(oldPostgresConfigCmd); !ok { - db.log.Infof("%q is not present, skipping upgrade", oldPostgresConfigCmd) + db.log.Infof("%q is not present, please make sure that at least one backup was taken with the old postgres version or restart the backup-restore-sidecar container with the old postgres version before running an upgrade, skipping upgrade", oldPostgresConfigCmd) return nil } @@ -264,7 +264,8 @@ func (db *Postgres) getBinDir(pgConfigCmd string) (string, error) { return strings.TrimSpace(string(out)), nil } -func (db *Postgres) copyPostgresBinaries() error { +// copyPostgresBinaries is needed to save old postgres binaries for a later major upgrade +func (db *Postgres) copyPostgresBinaries(override bool) error { binDir, err := db.getBinDir(postgresConfigCmd) if err != nil { return err @@ -277,6 +278,13 @@ func (db *Postgres) copyPostgresBinaries() error { pgBinDir := path.Join(db.datadir, fmt.Sprintf("%s%d", postgresBinBackupPrefix, version)) + if !override { + if _, err := os.Stat(path.Join(pgBinDir, postgresConfigCmd)); err == nil { + db.log.Info("postgres binaries for later upgrade already in place, not copying") + return nil + } + } + err = os.RemoveAll(pgBinDir) if err != nil { return fmt.Errorf("unable to remove old pg bin dir: %w", err)