/
walg.go
197 lines (164 loc) · 4.97 KB
/
walg.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package helpers
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/wal-g/tracelog"
"github.com/wal-g/wal-g/internal"
)
type Sentinel struct {
StartLocalTime time.Time `json:"StartLocalTime,omitempty"`
FinishLocalTime time.Time `json:"FinishLocalTime,omitempty"`
UserData interface{} `json:"UserData,omitempty"`
MongoMeta BackupMeta `json:"MongoMeta,omitempty"`
}
type NodeMeta struct {
LastTS OpTimestamp `json:"LastTS,omitempty"`
LastMajTS OpTimestamp `json:"LastMajTS,omitempty"`
}
type BackupMeta struct {
Before NodeMeta `json:"Before,omitempty"`
After NodeMeta `json:"After,omitempty"`
}
type OpTimestamp struct {
TS uint32
Inc uint32
}
type Archive struct {
StartTS OpTimestamp
EndTS OpTimestamp
}
func (ots OpTimestamp) String() string {
return fmt.Sprintf("%d.%d", ots.TS, ots.Inc)
}
func LessTS(ots1, ots2 OpTimestamp) bool {
if ots1.TS < ots2.TS {
return true
}
if ots1.TS > ots2.TS {
return false
}
return ots1.Inc < ots2.Inc
}
func TimestampFromStr(s string) (OpTimestamp, error) {
strs := strings.Split(s, ".")
if len(strs) != 2 {
return OpTimestamp{}, fmt.Errorf("can not split oplog ts string '%s': two parts expected", s)
}
ts, err := strconv.ParseUint(strs[0], 10, 32)
if err != nil {
return OpTimestamp{}, fmt.Errorf("can not convert ts string '%v': %w", ts, err)
}
inc, err := strconv.ParseUint(strs[1], 10, 32)
if err != nil {
return OpTimestamp{}, fmt.Errorf("can not convert inc string '%v': %w", inc, err)
}
return OpTimestamp{TS: uint32(ts), Inc: uint32(inc)}, nil
}
func BackupNamesFromListing(output string) []string {
re := regexp.MustCompile("stream_[0-9]{8}T[0-9]{6}Z")
return re.FindAllString(output, -1)
}
func BackupNameFromCreate(output string) string {
return strings.Trim(strings.Split(output, "FILE PATH: ")[1], " ")
}
type WalgUtil struct {
ctx context.Context
host string
cliPath string
confPath string
mongoMaj string
}
func NewWalgUtil(ctx context.Context, host, cliPath, confPath, mongoMaj string) *WalgUtil {
return &WalgUtil{ctx, host, cliPath, confPath, mongoMaj}
}
func (w *WalgUtil) runCmd(run []string) (ExecResult, error) {
command := []string{w.cliPath, "--config", w.confPath}
command = append(command, run...)
exc, err := RunCommandStrict(w.ctx, w.host, command)
return exc, err
}
func (w *WalgUtil) PushBackup() (string, error) {
PgDataSettingString, ok := internal.GetSetting(internal.PgDataSetting)
if ok == false {
tracelog.InfoLogger.Print("\nPGDATA is not set in the conf.\n")
}
if w.cliPath != PgDataSettingString {
tracelog.WarningLogger.Printf("cliPath '%s' differ from conf PGDATA '%s'\n", w.cliPath, PgDataSettingString)
}
exec, err := w.runCmd([]string{"backup-push"})
if err != nil {
return "", err
}
return BackupNameFromCreate(exec.Combined()), nil
}
func (w *WalgUtil) FetchBackupByNum(backupNum int) error {
backups, err := w.Backups()
if err != nil {
return err
}
if backupNum >= len(backups) {
return fmt.Errorf("only %d backups exists, backup #%d is not found", len(backups), backupNum)
}
_, err = w.runCmd([]string{"backup-fetch", backups[backupNum]})
return err
}
func (w *WalgUtil) BackupMeta(backupNum int) (Sentinel, error) {
backups, err := w.Backups()
if err != nil {
return Sentinel{}, err
}
if backupNum >= len(backups) {
return Sentinel{}, fmt.Errorf("only %d backups exists, backup #%d is not found", len(backups), backupNum)
}
exec, err := w.runCmd([]string{"backup-show", backups[backupNum]})
if err != nil {
return Sentinel{}, fmt.Errorf("backup show failed: %v", err)
}
var sentinel Sentinel
err = json.Unmarshal([]byte(exec.Stdout()), &sentinel)
return sentinel, err
}
func (w *WalgUtil) Backups() ([]string, error) {
exec, err := w.runCmd([]string{"backup-list"})
if err != nil {
return nil, err
}
return BackupNamesFromListing(exec.Combined()), nil
}
func (w *WalgUtil) PurgeRetain(keepNumber int) error {
_, err := w.runCmd([]string{
"delete",
"--retain-count", strconv.Itoa(keepNumber),
"--retain-after", time.Now().Format("2006-01-02T15:04:05Z"),
"--purge-oplog",
"--confirm"})
return err
}
func (w *WalgUtil) DeleteBackup(backupName string) error {
_, err := w.runCmd([]string{"backup-delete", backupName, "--confirm"})
return err
}
func (w *WalgUtil) OplogPush() error {
cmd := []string{w.cliPath, "--config", w.confPath, "oplog-push"}
cmdLine := strings.Join(cmd, " ")
exc, err := RunCommand(w.ctx, w.host, cmd)
if err != nil || exc.ExitCode != 0 {
tracelog.DebugLogger.Printf("'%s' failed with error %s, exit code %d, stdout:\n%s\nstderr:\n%s\n",
cmdLine, err, exc.ExitCode, exc.Stdout(), exc.Stderr())
return fmt.Errorf("%s exit code: %d", cmdLine, exc.ExitCode)
}
return nil
}
func (w *WalgUtil) OplogReplay(from, until OpTimestamp) error {
_, err := w.runCmd([]string{"oplog-replay", from.String(), until.String()})
return err
}
func (w *WalgUtil) OplogPurge() error {
_, err := w.runCmd([]string{"oplog-purge", "--confirm"})
return err
}