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

Add tests for building the postgres backup command

This commit is contained in:
kolaente
2021-08-18 22:27:21 +02:00
parent 1bc3fc85ab
commit 45e7560e0b
2 changed files with 125 additions and 5 deletions

View File

@@ -15,8 +15,7 @@ func NewPostgresDumper(container *types.ContainerJSON) *PostgresDumper {
}
}
func (d *PostgresDumper) Dump() error {
fmt.Printf("Dumping postgres database from container %s...\n", d.Container.Name)
func (d *PostgresDumper) buildConnStr() string {
env := parseEnv(d.Container.Config.Env)
user := "root"
@@ -24,7 +23,7 @@ func (d *PostgresDumper) Dump() error {
user = u
}
db := ""
db := "postgres"
if d, has := env["POSTGRES_DB"]; has {
db = d
}
@@ -41,9 +40,13 @@ func (d *PostgresDumper) Dump() error {
host := d.Container.NetworkSettings.DefaultNetworkSettings.IPAddress
connStr := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s", user, pw, host, port, db)
return fmt.Sprintf("postgresql://%s:%s@%s:%s/%s", user, pw, host, port, db)
}
// TODO: Check postgres image version and use the correct pg_dump --> Test!
func (d *PostgresDumper) Dump() error {
fmt.Printf("Dumping postgres database from container %s...\n", d.Container.Name)
connStr := d.buildConnStr()
return runAndSaveCommand(getDumpFilename(d.Container.Name), "pg_dump", "--dbname", connStr)
}