Skip to content

Commit

Permalink
Merge pull request #63 from sapcc/pgdump-version
Browse files Browse the repository at this point in the history
Use pgdump matching postgresql server version
  • Loading branch information
majewsky authored Sep 13, 2023
2 parents 4d36ffb + 0f7c519 commit ee2b415
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN addgroup -g 4200 appgroup \
&& adduser -h /home/appuser -s /sbin/nologin -G appgroup -D -u 4200 appuser
# upgrade all installed packages to fix potential CVEs in advance
RUN apk upgrade --no-cache --no-progress \
&& apk add --no-cache --no-progress ca-certificates postgresql15-client curl jq
&& apk add --no-cache --no-progress ca-certificates postgresql12-client postgresql15-client curl jq
COPY --from=builder /pkg/ /usr/

ARG BININFO_BUILD_DATE BININFO_COMMIT_HASH BININFO_VERSION
Expand Down
9 changes: 6 additions & 3 deletions Makefile.maker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ dockerfile:
extraDirectives:
- "ENV ENV=/usr/bin/motd.sh" # `kubectl exec` is not a login shell, so we need to use this instead of /etc/profile or $HOME/.profile
extraPackages:
- postgresql15-client # for psql, pg_dump
- curl # required for backup-tools.sh
- jq # required for backup-tools.sh
# for psql, pg_dump
- postgresql12-client
- postgresql15-client
# required for backup-tools.sh
- curl
- jq

golang:
enableVendoring: true
Expand Down
24 changes: 23 additions & 1 deletion internal/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package backup

import (
"context"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -54,6 +55,10 @@ const (
retentionTime time.Duration = 10 * 24 * time.Hour // 10 days
)

func getPgdumpForVersion(majorVersion string) string {
return fmt.Sprintf("/usr/libexec/postgresql%s/pg_dump", majorVersion)
}

// Create creates a backup unconditionally. The provided `reason` is used
// in log messages to explain why the backup was created.
func Create(cfg *core.Configuration, reason string) (nowTime time.Time, returnedError error) {
Expand Down Expand Up @@ -90,12 +95,29 @@ func Create(cfg *core.Configuration, reason string) (nowTime time.Time, returned
//Close() the writer side in order for LargeObject.Append() to return on
//the reader side.

// determine postgresql server version
cmd := exec.CommandContext(ctx, "psql", //nolint:gosec // input is user supplied and self executed
"-h", cfg.PgHostname, "-U", cfg.PgUsername, //NOTE: PGPASSWORD comes via inherited env variable
"--csv", "--tuples-only", "-c", "SHOW SERVER_VERSION") // output not decoration or padding
output, err := cmd.Output()
if err != nil {
return nowTime, fmt.Errorf("could not determine postgresql server version: %w", err)
}

majorVersion := strings.Split(string(output), ".")[0]
pgdump := getPgdumpForVersion(majorVersion)

// if the pgdump version was not found fallback to pgdump version 12
if _, err := os.Stat(pgdump); errors.Is(err, os.ErrNotExist) {
pgdump = getPgdumpForVersion("12")
}

//run pg_dump
pipeReader, pipeWriter := io.Pipe()
errChan := make(chan error, 1) //must be buffered to ensure that `pipewriter.Close()` runs immediately
go func() {
defer pipeWriter.Close()
cmd := exec.CommandContext(ctx, "pg_dump", //nolint:gosec // input is user supplied and self executed
cmd := exec.CommandContext(ctx, pgdump,
"-h", cfg.PgHostname, "-U", cfg.PgUsername, //NOTE: PGPASSWORD comes via inherited env variable
"-c", "--if-exist", "-C", "-Z", "5", databaseName)
logg.Info(">> " + shellquote.Join(cmd.Args...))
Expand Down

0 comments on commit ee2b415

Please sign in to comment.