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

feat: add backup compression

This commit is contained in:
kolaente
2024-02-13 16:45:10 +01:00
parent 49510a6179
commit 5acf40d86e
7 changed files with 54 additions and 9 deletions

25
save.go
View File

@@ -1,6 +1,7 @@
package main
import (
"compress/gzip"
"context"
"fmt"
"io"
@@ -10,16 +11,21 @@ import (
"github.com/docker/docker/client"
)
func runAndSaveCommandInContainer(filename string, c *client.Client, container *types.ContainerJSON, command string, args ...string) error {
func runAndSaveCommandInContainer(c *client.Client, container *types.ContainerJSON, command string, args ...string) error {
ctx := context.Background()
config := types.ExecConfig{
filename := getDumpFilename(container.Name)
if config.CompressBackups {
filename += ".gz"
}
containerConfig := types.ExecConfig{
AttachStderr: true,
AttachStdout: true,
Cmd: append([]string{command}, args...),
}
r, err := c.ContainerExecCreate(ctx, container.ID, config)
r, err := c.ContainerExecCreate(ctx, container.ID, containerConfig)
if err != nil {
return err
}
@@ -36,7 +42,18 @@ func runAndSaveCommandInContainer(filename string, c *client.Client, container *
}
defer f.Close()
_, err = io.Copy(f, resp.Reader)
var target io.Writer = f
if config.CompressBackups {
gw, err := gzip.NewWriterLevel(f, gzip.BestCompression)
if err != nil {
return err
}
defer gw.Close()
target = gw
}
_, err = io.Copy(target, resp.Reader)
if err != nil {
return err
}