-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.go
61 lines (48 loc) · 1.35 KB
/
dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"compress/gzip"
"fmt"
"os"
"os/exec"
"time"
)
type DatabaseConfig struct {
Host string `json:"host"`
User string `json:"user"`
Password string `json:"password"`
Database string `json:"database"`
}
type MysqlDump struct {
conf *DatabaseConfig
}
func NewMysqlDump(conf *DatabaseConfig) Dumper {
return &MysqlDump{conf: conf}
}
func (m *MysqlDump) Dump() (string, error) {
db := m.conf
fmt.Printf("Start backup database %s\n", db.Database)
dumpCmd := fmt.Sprintf("mysqldump -h %s -u %s -p%s %s", db.Host, db.User, db.Password, db.Database)
cmd := exec.Command("bash", "-c", dumpCmd)
fileName := fmt.Sprintf("%s_%s.gz", db.Database, time.Now().Format("20060102150405"))
tmpFile, err := os.CreateTemp("", fileName)
if err != nil {
return "", fmt.Errorf("create temp file failed: %w", err)
}
defer tmpFile.Close()
gzWriter := gzip.NewWriter(tmpFile)
defer gzWriter.Close()
cmd.Stdout = gzWriter
if e := cmd.Run(); e != nil {
_ = os.Remove(tmpFile.Name())
return "", fmt.Errorf("dump database %s failed: %w", db.Database, e)
}
if e := gzWriter.Close(); e != nil {
_ = os.Remove(tmpFile.Name())
return "", fmt.Errorf("close gzip writer failed: %w", e)
}
if e := tmpFile.Close(); e != nil {
_ = os.Remove(tmpFile.Name())
return "", fmt.Errorf("close temp file failed: %w", e)
}
return tmpFile.Name(), nil
}