3
0
mirror of https://kolaente.dev/konrad/docker-db-backup.git synced 2026-01-10 02:50:08 +01:00

feat: move running the actual dump command to the db container

This commit is contained in:
kolaente
2021-12-05 12:39:23 +01:00
parent c5f0b46e77
commit 2e58388a0b
7 changed files with 52 additions and 67 deletions

View File

@@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/docker/docker/api/types"
"strings"
"github.com/docker/docker/client"
)
type PostgresDumper struct {
@@ -44,23 +44,10 @@ func (d *PostgresDumper) buildConnStr() string {
return fmt.Sprintf("postgresql://%s:%s@%s:%s/%s", user, pw, host, port, db)
}
func findPgVersion(env []string) string {
for _, s := range env {
if strings.HasPrefix(s, "PG_MAJOR=") {
return strings.TrimPrefix(s, "PG_MAJOR=")
}
}
return ""
}
func (d *PostgresDumper) Dump() error {
func (d *PostgresDumper) Dump(c *client.Client) error {
fmt.Printf("Dumping postgres database from container %s...\n", d.Container.Name)
connStr := d.buildConnStr()
// The postgres version must match the one the db server is running
pgVersion := findPgVersion(d.Container.Config.Env)
return runAndSaveCommand(getDumpFilename(d.Container.Name), "pg_dump"+pgVersion, "--dbname", connStr)
return runAndSaveCommandInContainer(getDumpFilename(d.Container.Name), c, d.Container, "pg_dump", "--dbname", connStr)
}