Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
fix backups to disk; (#209)
Browse files Browse the repository at this point in the history
Signed-off-by: SimonLi <[email protected]>
  • Loading branch information
Simon-Li authored and prydie committed Aug 24, 2018
1 parent bc88e14 commit ee732cf
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions pkg/backup/executor/mysqldump/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit ee732cf

Please sign in to comment.