From ee732cf526e71e79bfd1611bccdedc139178c10a Mon Sep 17 00:00:00 2001 From: Simon Li Date: Fri, 24 Aug 2018 17:56:34 +0800 Subject: [PATCH] fix backups to disk; (#209) Signed-off-by: SimonLi --- pkg/backup/executor/mysqldump/provider.go | 41 +++++++++-------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/pkg/backup/executor/mysqldump/provider.go b/pkg/backup/executor/mysqldump/provider.go index c9d35df54..b7253bb86 100644 --- a/pkg/backup/executor/mysqldump/provider.go +++ b/pkg/backup/executor/mysqldump/provider.go @@ -18,13 +18,11 @@ import ( "compress/gzip" "fmt" "io" - "os" - "path" - "path/filepath" "sync" "time" "github.com/golang/glog" + "github.com/pkg/errors" utilexec "k8s.io/utils/exec" @@ -81,31 +79,24 @@ func (ex *Executor) Backup(backupDir string, clusterName string) (io.ReadCloser, mu.Lock() defer mu.Unlock() - tmpFile := path.Join( - backupDir, - fmt.Sprintf("%s.%s.sql.gz", clusterName, time.Now().UTC().Format("20060102150405"))) + backupName := fmt.Sprintf("%s.%s.sql.gz", clusterName, time.Now().UTC().Format("20060102150405")) - f, err := os.Create(tmpFile) - if err != nil { - return nil, "", err - } - defer f.Close() - - zw := gzip.NewWriter(f) - defer zw.Close() + pr, pw := io.Pipe() + zw := gzip.NewWriter(pw) cmd.SetStdout(zw) - glog.V(4).Infof("running cmd: '%v'", cmd) - err = cmd.Run() - if err != nil { - return nil, "", err - } - - content, err := os.Open(tmpFile) - if err != nil { - return nil, "", err - } - return content, filepath.Base(tmpFile), nil + go func() { + glog.V(4).Infof("running cmd: '%v'", cmd) + err = cmd.Run() + zw.Close() + if err != nil { + pw.CloseWithError(errors.Wrap(err, "executing backup")) + } else { + pw.Close() + } + }() + + return pr, backupName, nil } // Restore a cluster from a mysqldump.