From e85384d0dacc83f35446558cf9241011488f39bf Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 18 Aug 2021 21:50:04 +0200 Subject: [PATCH] Add postgres dumper --- .gitignore | 1 + config.go | 2 +- dump_postgres.go | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index e85d567..9129bed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/ docker-db-backup *.sql +./backups diff --git a/config.go b/config.go index b147518..c206078 100644 --- a/config.go +++ b/config.go @@ -44,7 +44,7 @@ func init() { func updateFullBackupPath() { dumpTime = time.Now() config.fullCurrentBackupPath = config.Folder + dumpTime.Format("02-01-2006_15-04-05") + "/" - err := os.MkdirAll(config.fullCurrentBackupPath, 0644) + err := os.MkdirAll(config.fullCurrentBackupPath, 0744) if err != nil { log.Fatalf("Could not create backup folder: %s\n", err) } diff --git a/dump_postgres.go b/dump_postgres.go index e530355..fded4b9 100644 --- a/dump_postgres.go +++ b/dump_postgres.go @@ -1,6 +1,9 @@ package main -import "github.com/docker/docker/api/types" +import ( + "fmt" + "github.com/docker/docker/api/types" +) type PostgresDumper struct { Container *types.ContainerJSON @@ -12,6 +15,35 @@ func NewPostgresDumper(container *types.ContainerJSON) *PostgresDumper { } } -func (p *PostgresDumper) Dump() error { - panic("implement me") +func (d *PostgresDumper) Dump() error { + fmt.Printf("Dumping postgres database from container %s...\n", d.Container.Name) + env := parseEnv(d.Container.Config.Env) + + user := "root" + if u, has := env["POSTGRES_USER"]; has { + user = u + } + + db := "" + if d, has := env["POSTGRES_DB"]; has { + db = d + } + + pw := env["POSTGRES_ROOT_PASSWORD"] + if p, has := env["POSTGRES_PASSWORD"]; has { + pw = p + } + + port := "5432" + if p, has := env["POSTGRES_PORT"]; has { + port = p + } + + host := d.Container.NetworkSettings.DefaultNetworkSettings.IPAddress + + connStr := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s", user, pw, host, port, db) + + // TODO: Check postgres image version and use the correct pg_dump + + return runAndSaveCommand(getDumpFilename(d.Container.Name), "pg_dump", "--dbname", connStr) }