fix: directly write dump to file without buffering in memory
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Resolves #5
This commit is contained in:
parent
1b17dedc47
commit
5982f1ef07
38
save.go
38
save.go
@ -1,14 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/pkg/stdcopy"
|
||||
"io"
|
||||
"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 {
|
||||
@ -31,33 +30,20 @@ func runAndSaveCommandInContainer(filename string, c *client.Client, container *
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user