generated from cybozu-go/neco-template
/
backup.go
67 lines (59 loc) · 1.49 KB
/
backup.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
package bkop
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"github.com/cybozu-go/moco/pkg/constants"
)
func (o operator) DumpFull(ctx context.Context, dir string) error {
args := []string{
fmt.Sprintf("mysql://%s@%s:%d", o.user, o.host, o.port),
"--passwords-from-stdin",
"--save-passwords=never",
"-C", "False",
"--",
"util",
"dump-instance",
dir,
"--excludeUsers=" + strings.Join(constants.MocoUsers, ","),
"--threads=" + fmt.Sprint(o.threads),
}
cmd := exec.CommandContext(ctx, "mysqlsh", args...)
cmd.Stdin = strings.NewReader(o.password)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func (o operator) GetBinlogs(ctx context.Context) ([]string, error) {
var binlogs []showBinaryLogs
if err := o.db.SelectContext(ctx, &binlogs, `SHOW BINARY LOGS`); err != nil {
return nil, fmt.Errorf("failed to show binary logs: %w", err)
}
r := make([]string, len(binlogs))
for i, row := range binlogs {
r[i] = row.LogName
}
return r, nil
}
func (o operator) DumpBinlog(ctx context.Context, dir, binlogName, filterGTID string) error {
args := []string{
"-h", o.host,
"--port", fmt.Sprint(o.port),
"--protocol=tcp",
"-u", o.user,
"-p" + o.password,
"--get-server-public-key",
"--read-from-remote-master=BINLOG-DUMP-GTIDS",
"--exclude-gtids=" + filterGTID,
"-t",
"--raw",
"--result-file=" + dir + "/",
binlogName,
}
cmd := exec.CommandContext(ctx, "mysqlbinlog", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}