Add postgres dumper

This commit is contained in:
kolaente 2021-08-18 21:50:04 +02:00
parent 7a24e57e09
commit e85384d0da
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 37 additions and 4 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea/
docker-db-backup
*.sql
./backups

View File

@ -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)
}

View File

@ -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)
}