forked from keighl/barkup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql.go
68 lines (59 loc) · 1.61 KB
/
mysql.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
62
63
64
65
66
67
68
package barkup
import (
"fmt"
"os"
"os/exec"
"time"
)
var (
// TarCmd is the path to the `tar` executable
TarCmd = "tar"
// MysqlDumpCmd is the path to the `mysqldump` executable
MysqlDumpCmd = "mysqldump"
)
// MySQL is an `Exporter` interface that backs up a MySQL database via the `mysqldump` command
type MySQL struct {
// DB Host (e.g. 127.0.0.1)
Host string
// DB Port (e.g. 3306)
Port string
// DB Name
DB string
// DB User
User string
// DB Password
Password string
// Extra mysqldump options
// e.g []string{"--extended-insert"}
Options []string
}
// Export produces a `mysqldump` of the specified database, and creates a gzip compressed tarball archive.
func (x MySQL) Export() *ExportResult {
result := &ExportResult{MIME: "application/x-tar"}
dumpPath := fmt.Sprintf(`bu_%v_%v.sql`, x.DB, time.Now().Unix())
options := append(x.dumpOptions(), fmt.Sprintf(`-r%v`, dumpPath))
out, err := exec.Command(MysqlDumpCmd, options...).Output()
if err != nil {
result.Error = makeErr(err, string(out))
return result
}
result.Path = dumpPath + ".tar.gz"
_, err = exec.Command(TarCmd, "-czf", result.Path, dumpPath).Output()
if err != nil {
result.Error = makeErr(err, string(out))
return result
}
os.Remove(dumpPath)
return result
}
func (x MySQL) dumpOptions() []string {
options := x.Options
options = append(options, fmt.Sprintf(`-h%v`, x.Host))
options = append(options, fmt.Sprintf(`-P%v`, x.Port))
options = append(options, fmt.Sprintf(`-u%v`, x.User))
if x.Password != "" {
options = append(options, fmt.Sprintf(`-p%v`, x.Password))
}
options = append(options, x.DB)
return options
}