Skip to content

Commit

Permalink
Also copy postgres binary in backup step. (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Aug 23, 2023
1 parent 2d116e0 commit 8488c2e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
9 changes: 9 additions & 0 deletions cmd/internal/database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 11 additions & 3 deletions cmd/internal/database/postgres/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 8488c2e

Please sign in to comment.