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

Commit ee732cf

Browse files
Simon-Liprydie
authored andcommitted
fix backups to disk; (#209)
Signed-off-by: SimonLi <simon.jiyou@gmail.com>
1 parent bc88e14 commit ee732cf

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

pkg/backup/executor/mysqldump/provider.go

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ import (
1818
"compress/gzip"
1919
"fmt"
2020
"io"
21-
"os"
22-
"path"
23-
"path/filepath"
2421
"sync"
2522
"time"
2623

2724
"github.com/golang/glog"
25+
"github.com/pkg/errors"
2826

2927
utilexec "k8s.io/utils/exec"
3028

@@ -81,31 +79,24 @@ func (ex *Executor) Backup(backupDir string, clusterName string) (io.ReadCloser,
8179
mu.Lock()
8280
defer mu.Unlock()
8381

84-
tmpFile := path.Join(
85-
backupDir,
86-
fmt.Sprintf("%s.%s.sql.gz", clusterName, time.Now().UTC().Format("20060102150405")))
82+
backupName := fmt.Sprintf("%s.%s.sql.gz", clusterName, time.Now().UTC().Format("20060102150405"))
8783

88-
f, err := os.Create(tmpFile)
89-
if err != nil {
90-
return nil, "", err
91-
}
92-
defer f.Close()
93-
94-
zw := gzip.NewWriter(f)
95-
defer zw.Close()
84+
pr, pw := io.Pipe()
85+
zw := gzip.NewWriter(pw)
9686
cmd.SetStdout(zw)
9787

98-
glog.V(4).Infof("running cmd: '%v'", cmd)
99-
err = cmd.Run()
100-
if err != nil {
101-
return nil, "", err
102-
}
103-
104-
content, err := os.Open(tmpFile)
105-
if err != nil {
106-
return nil, "", err
107-
}
108-
return content, filepath.Base(tmpFile), nil
88+
go func() {
89+
glog.V(4).Infof("running cmd: '%v'", cmd)
90+
err = cmd.Run()
91+
zw.Close()
92+
if err != nil {
93+
pw.CloseWithError(errors.Wrap(err, "executing backup"))
94+
} else {
95+
pw.Close()
96+
}
97+
}()
98+
99+
return pr, backupName, nil
109100
}
110101

111102
// Restore a cluster from a mysqldump.

0 commit comments

Comments
 (0)