-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Thomascogez/pg-dumper
- Loading branch information
Showing
9 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
version: '3.0' | ||
|
||
services: | ||
pg_without_label: | ||
image: postgres:latest | ||
environment: | ||
POSTGRES_PASSWORD: test | ||
POSTGRES_DB: test | ||
|
||
pg_dump_disabled: | ||
image: postgres:latest | ||
environment: | ||
POSTGRES_PASSWORD: test | ||
POSTGRES_DB: test | ||
labels: | ||
- 'go.dumper.enabled=false' | ||
- 'go.dumper.databaseUser=postgres' | ||
- 'go.dumper.databaseType=pg' | ||
|
||
pg_dump_1: | ||
image: postgres:latest | ||
environment: | ||
POSTGRES_PASSWORD: test | ||
POSTGRES_DB: test | ||
labels: | ||
- 'go-dumper.enabled=true' | ||
- 'go-dumper.user=postgres' | ||
- 'go-dumper.type=pg' | ||
|
||
pg_dump_2: | ||
image: postgres:latest | ||
environment: | ||
POSTGRES_PASSWORD: test | ||
POSTGRES_DB: test | ||
labels: | ||
- 'go.dumper.enabled=true' | ||
- 'go.dumper.databaseUser=postgres' | ||
- 'go.dumper.databaseType=pg' | ||
|
||
mysql_dump: | ||
image: mysql:latest | ||
command: --default-authentication-plugin=mysql_native_password | ||
environment: | ||
MYSQL_DATABASE: 'db' | ||
MYSQL_USER: 'user' | ||
MYSQL_ROOT_PASSWORD: root | ||
labels: | ||
- 'go.dumper.enabled=true' | ||
- 'go.dumper.databaseUser=root' | ||
- 'go.dumper.databasePassword=root' | ||
- 'go.dumper.databaseName=db' | ||
- 'go.dumper.databaseType=mysql' | ||
|
||
minio: | ||
image: minio/minio | ||
entrypoint: sh | ||
command: -c 'mkdir -p ./data/s3/default && minio server ./data/s3 -console-address ":9001"' | ||
environment: | ||
- MINIO_REGION=eu-west-3 | ||
- MINIO_ACCESS_KEY=2OKVH3X1NLIGU9RA4VER | ||
- MINIO_SECRET_KEY=RSrrhXZ6LTejtHyhgZfilWO3On1ltOU8YI1B4Act | ||
ports: | ||
- 9000:9000 | ||
- 9001:9001 | ||
volumes: | ||
- ./minio:/data/s3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package dumper | ||
|
||
import "github.com/docker/docker/api/types" | ||
|
||
func ExtractDumpOptionsFromLabels(labels map[string]string) DumpOptions { | ||
return DumpOptions{ | ||
Enabled: labels["go-dumper.enabled"] == "true", | ||
User: labels["go-dumper.user"], | ||
Type: labels["go-dumper.type"], | ||
} | ||
} | ||
|
||
func BuildContainerDumpCommandArgs(containerId string, dumpOptions DumpOptions) []string { | ||
args := []string{"exec", containerId} | ||
|
||
if dumpOptions.Type == PG { | ||
args = append(args, | ||
"pg_dumpall", | ||
"-U", dumpOptions.User, | ||
"--if-exists", | ||
"-c", | ||
) | ||
} | ||
|
||
return args | ||
} | ||
|
||
func FindContainersByTypes(containers []types.Container, containerType string) []types.Container { | ||
containerToDump := make([]types.Container, 0) | ||
|
||
for _, container := range containers { | ||
containerDumpConfig := ExtractDumpOptionsFromLabels(container.Labels) | ||
|
||
if containerDumpConfig.Enabled && containerDumpConfig.Type == containerType { | ||
containerToDump = append(containerToDump, container) | ||
} | ||
} | ||
|
||
return containerToDump | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package dumper_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/Thomascogez/dump-and-dumper/dumper" | ||
) | ||
|
||
func TestExtractDumpConfigFromLabels(t *testing.T) { | ||
|
||
labels := make(map[string]string) | ||
|
||
labels["go-dumper.enabled"] = "true" | ||
labels["go-dumper.user"] = "user" | ||
labels["go-dumper.type"] = "pg" | ||
|
||
dumpConfig := dumper.ExtractDumpOptionsFromLabels(labels) | ||
|
||
if !dumpConfig.Enabled || dumpConfig.Type != "pg" || dumpConfig.User != "user" { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestBuildContainerDumpCommandArgs(t *testing.T) { | ||
containerDumpConfig := dumper.DumpOptions{ | ||
Enabled: true, | ||
User: "postgres", | ||
Type: "pg", | ||
} | ||
|
||
testContainerId := "containerId" | ||
|
||
pgContainerDumpArgs := dumper.BuildContainerDumpCommandArgs(testContainerId, containerDumpConfig) | ||
|
||
argsString := strings.Join(pgContainerDumpArgs[:], " ") | ||
|
||
if argsString != fmt.Sprintf("exec %s pg_dumpall -U %s --if-exists -c", testContainerId, containerDumpConfig.User) { | ||
t.Fail() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package dumper | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"sync" | ||
|
||
"github.com/Thomascogez/dump-and-dumper/helpers" | ||
"github.com/docker/docker/api/types" | ||
) | ||
|
||
type PgDumper struct{} | ||
|
||
func (pgDumper PgDumper) Dump(containers []types.Container) { | ||
|
||
var wg sync.WaitGroup | ||
|
||
for _, container := range containers { | ||
wg.Add(1) | ||
|
||
go func(container types.Container) { | ||
dumpConfig := ExtractDumpOptionsFromLabels(container.Labels) | ||
dumpCommandArgs := BuildContainerDumpCommandArgs(container.ID, dumpConfig) | ||
|
||
tempDumpFile, tempDumpFolderPath, tempDumpFileName := helpers.CreateTempDumpFile() | ||
|
||
dumpCommand := exec.Command("docker", dumpCommandArgs...) | ||
dumpCommand.Stdout = tempDumpFile | ||
dumpCommand.Run() | ||
|
||
println(tempDumpFileName) | ||
tempDumpFile.Close() | ||
os.RemoveAll(tempDumpFolderPath) | ||
wg.Done() | ||
}(container) | ||
} | ||
|
||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dumper | ||
|
||
import "github.com/docker/docker/api/types" | ||
|
||
type Dumper interface { | ||
Dump(container *types.Container) | ||
} | ||
|
||
const ( | ||
PG = "pg" | ||
) | ||
|
||
type DumpOptions struct { | ||
Enabled bool | ||
User string | ||
Type string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package helpers | ||
|
||
func CheckError(err error) { | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package helpers | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"time" | ||
) | ||
|
||
func FileNameFromCurrentTimestamp(fileExtension string) string { | ||
return fmt.Sprintf("%d.%s", time.Now().UnixMilli(), fileExtension) | ||
} | ||
|
||
func CreateTempDumpFile() (*os.File, string, string) { | ||
cwd, err := os.Getwd() | ||
CheckError(err) | ||
|
||
tempDumpFolderPath, err := ioutil.TempDir(cwd, "dump-") | ||
|
||
tempDumpFileName := FileNameFromCurrentTimestamp("sql") | ||
tempDumpFilePath := path.Join(tempDumpFolderPath, tempDumpFileName) | ||
tempDumpFile, err := os.Create(tempDumpFilePath) | ||
|
||
if err != nil { | ||
os.RemoveAll(tempDumpFolderPath) | ||
CheckError(err) | ||
} | ||
|
||
return tempDumpFile, tempDumpFolderPath, tempDumpFileName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Thomascogez/dump-and-dumper/dumper" | ||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/client" | ||
) | ||
|
||
func main() { | ||
cli, err := client.NewClientWithOpts(client.FromEnv) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
pgContainersToDump := dumper.FindContainersByTypes(containers, dumper.PG) | ||
|
||
dumper.PgDumper{}.Dump(pgContainersToDump) | ||
} |