mirror of
https://kolaente.dev/konrad/docker-db-backup.git
synced 2026-01-10 21:10:07 +01:00
fix: directly write dump to file without buffering in memory
Resolves https://kolaente.dev/konrad/docker-db-backup/issues/5
This commit is contained in:
38
save.go
38
save.go
@@ -1,14 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/docker/docker/api/types"
|
|
||||||
"github.com/docker/docker/client"
|
|
||||||
"github.com/docker/docker/pkg/stdcopy"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runAndSaveCommandInContainer(filename string, c *client.Client, container *types.ContainerJSON, command string, args ...string) error {
|
func runAndSaveCommandInContainer(filename string, c *client.Client, container *types.ContainerJSON, command string, args ...string) error {
|
||||||
@@ -31,33 +30,20 @@ func runAndSaveCommandInContainer(filename string, c *client.Client, container *
|
|||||||
}
|
}
|
||||||
defer resp.Close()
|
defer resp.Close()
|
||||||
|
|
||||||
// read the output
|
|
||||||
var outBuf, errBuf bytes.Buffer
|
|
||||||
outputDone := make(chan error)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
// StdCopy demultiplexes the stream into two buffers
|
|
||||||
_, err = stdcopy.StdCopy(&outBuf, &errBuf, resp.Reader)
|
|
||||||
outputDone <- err
|
|
||||||
}()
|
|
||||||
|
|
||||||
err = <-outputDone
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf(errBuf.String())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = c.ContainerExecInspect(ctx, r.ID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.Create(filename)
|
f, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
_, err = io.Copy(f, &outBuf)
|
_, err = io.Copy(f, resp.Reader)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
execInspect, err := c.ContainerExecInspect(ctx, r.ID)
|
||||||
|
if execInspect.ExitCode != 0 {
|
||||||
|
return fmt.Errorf("backup from container %s failed with exit code %d", container.Name, execInspect.ExitCode)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user