-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathbarman.go
More file actions
319 lines (264 loc) · 8.34 KB
/
barman.go
File metadata and controls
319 lines (264 loc) · 8.34 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package flypg
import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"strings"
"time"
"github.com/fly-apps/postgres-flex/internal/flypg/state"
"github.com/fly-apps/postgres-flex/internal/utils"
)
const (
providerDefault = "aws-s3"
// DefaultAuthProfile is the default AWS profile used for barman operations.
DefaultAuthProfile = "barman"
// RestoreAuthProfile is the AWS profile used for barman restore operations.
RestoreAuthProfile = "restore"
)
type Barman struct {
appName string
provider string
endpoint string
bucket string
bucketDirectory string
authProfile string
store *state.Store
*BarmanConfig
}
type Backup struct {
ID string `json:"backup_id"`
Name string `json:"backup_name"`
Status string `json:"status"`
StartTime string `json:"begin_time"`
EndTime string `json:"end_time"`
BeginWal string `json:"begin_wal"`
EndWal string `json:"end_wal"`
}
type BackupList struct {
Backups []Backup `json:"backups_list"`
}
// NewBarman creates a new Barman instance.
// The configURL is expected to be in the format:
// https://s3-access-key:s3-secret-key@s3-endpoint/bucket/bucket-directory
func NewBarman(store *state.Store, configURL, authProfile string) (*Barman, error) {
parsedURL, err := url.Parse(configURL)
if err != nil {
return nil, fmt.Errorf("invalid credential url: %w", err)
}
endpoint := parsedURL.Host
if endpoint == "" {
return nil, fmt.Errorf("object storage endpoint missing")
}
path := strings.TrimLeft(parsedURL.Path, "/")
if path == "" {
return nil, fmt.Errorf("bucket and directory missing")
}
pathSlice := strings.Split(path, "/")
if len(pathSlice) != 2 {
return nil, fmt.Errorf("invalid bucket and directory format")
}
// Extract user information for credentials (not used here but necessary for the complete parsing)
username := parsedURL.User.Username()
password, _ := parsedURL.User.Password()
// Ensure the credentials are not empty
if username == "" || password == "" {
return nil, fmt.Errorf("access key or secret key is missing")
}
return &Barman{
appName: os.Getenv("FLY_APP_NAME"),
provider: providerDefault,
endpoint: fmt.Sprintf("https://%s", endpoint),
bucket: pathSlice[0],
bucketDirectory: pathSlice[1],
authProfile: authProfile,
store: store,
}, nil
}
func (b *Barman) LoadConfig(configDir string) error {
barCfg, err := NewBarmanConfig(b.store, configDir)
if err != nil {
return err
}
b.BarmanConfig = barCfg
return nil
}
func (b *Barman) BucketURL() string {
return fmt.Sprintf("s3://%s", b.bucket)
}
type BackupConfig struct {
Name string // A customized name for the backup.
ImmediateCheckpoint bool // Force an immediate checkpoint.
}
func (b *Barman) Backup(ctx context.Context, cfg BackupConfig) ([]byte, error) {
args := []string{
"--cloud-provider", providerDefault,
"--endpoint-url", b.endpoint,
"--profile", b.authProfile,
"--host", fmt.Sprintf("%s.internal", b.appName),
"--user", "repmgr",
b.BucketURL(),
b.bucketDirectory,
}
if cfg.ImmediateCheckpoint {
args = append(args, "--immediate-checkpoint")
}
if cfg.Name != "" {
// Ensure the alias is unique, otherwise we won't be able to restore using it.
if err := b.validateBackupName(ctx, cfg.Name); err != nil {
return nil, err
}
args = append(args, "-n", cfg.Name)
}
return utils.RunCmd(ctx, "postgres", "with_tmpdir", append([]string{"/data/barman.tmp.XXXXXXXX", "barman-cloud-backup"}, args...)...)
}
// RestoreBackup returns the command string used to restore a base backup.
func (b *Barman) RestoreBackup(ctx context.Context, name string) ([]byte, error) {
args := []string{
"--cloud-provider", providerDefault,
"--endpoint-url", b.endpoint,
"--profile", b.authProfile,
b.BucketURL(),
b.bucketDirectory,
name,
defaultRestoreDir,
}
return utils.RunCmd(ctx, "postgres", "with_tmpdir", append([]string{"/data/barman.tmp.XXXXXXXX", "barman-cloud-restore"}, args...)...)
}
func (b *Barman) ListBackups(ctx context.Context) (BackupList, error) {
args := []string{
"--cloud-provider", providerDefault,
"--endpoint-url", b.endpoint,
"--profile", b.authProfile,
"--format", "json",
b.BucketURL(),
b.bucketDirectory,
}
backupsBytes, err := utils.RunCmd(ctx, "postgres", "barman-cloud-backup-list", args...)
if err != nil {
return BackupList{}, fmt.Errorf("failed to list backups: %s", err)
}
return b.parseBackups(backupsBytes)
}
// ListRawBackups returns the raw output of the backups list command.
func (b *Barman) ListRawBackups(ctx context.Context) ([]byte, error) {
args := []string{
"--cloud-provider", providerDefault,
"--endpoint-url", b.endpoint,
"--profile", b.authProfile,
"--format", "json",
b.BucketURL(),
b.bucketDirectory,
}
return utils.RunCmd(ctx, "postgres", "barman-cloud-backup-list", args...)
}
func (b *Barman) ShowBackup(ctx context.Context, id string) ([]byte, error) {
args := []string{
"--cloud-provider", providerDefault,
"--endpoint-url", b.endpoint,
"--profile", b.authProfile,
b.BucketURL(),
b.bucketDirectory,
id,
}
return utils.RunCmd(ctx, "postgres", "barman-cloud-backup-show", args...)
}
func (b *Barman) WALArchiveDelete(ctx context.Context) ([]byte, error) {
args := []string{
"--cloud-provider", providerDefault,
"--endpoint-url", b.endpoint,
"--profile", b.authProfile,
"--retention", b.Settings.RecoveryWindow,
"--minimum-redundancy", b.Settings.MinimumRedundancy,
b.BucketURL(),
b.bucketDirectory,
}
return utils.RunCmd(ctx, "postgres", "barman-cloud-backup-delete", args...)
}
func (b *Barman) ListCompletedBackups(ctx context.Context) (BackupList, error) {
backups, err := b.ListBackups(ctx)
if err != nil {
return BackupList{}, fmt.Errorf("failed to list backups: %s", err)
}
var completedBackups BackupList
for _, backup := range backups.Backups {
if backup.Status == "DONE" {
completedBackups.Backups = append(completedBackups.Backups, backup)
}
}
return completedBackups, nil
}
func (b *Barman) LastCompletedBackup(ctx context.Context) (time.Time, error) {
backups, err := b.ListCompletedBackups(ctx)
if err != nil {
return time.Time{}, fmt.Errorf("failed to list backups: %s", err)
}
if len(backups.Backups) == 0 {
return time.Time{}, nil
}
// Layout used by barman.
layout := "Mon Jan 2 15:04:05 2006"
var latestBackupTime time.Time
// Sort the backups start time
for _, backup := range backups.Backups {
startTime, err := time.Parse(layout, backup.StartTime)
if err != nil {
return time.Time{}, fmt.Errorf("failed to parse backup start time: %s", err)
}
if latestBackupTime.IsZero() || startTime.After(latestBackupTime) {
latestBackupTime = startTime
}
}
return latestBackupTime, nil
}
func (b *Barman) walArchiveCommand() string {
// TODO - Make compression configurable
return fmt.Sprintf("barman-cloud-wal-archive --cloud-provider %s --gzip --endpoint-url %s --profile %s %s %s %%p",
b.provider,
b.endpoint,
b.authProfile,
b.BucketURL(),
b.bucketDirectory,
)
}
// walRestoreCommand returns the command string used to restore WAL files.
// The %f and %p placeholders are replaced with the file path and file name respectively.
func (b *Barman) walRestoreCommand() string {
return fmt.Sprintf("barman-cloud-wal-restore --cloud-provider %s --endpoint-url %s --profile %s %s %s %%f %%p",
b.provider,
b.endpoint,
b.authProfile,
b.BucketURL(),
b.bucketDirectory,
)
}
func (*Barman) parseBackups(backupBytes []byte) (BackupList, error) {
var backupList BackupList
if err := json.Unmarshal(backupBytes, &backupList); err != nil {
return BackupList{}, fmt.Errorf("failed to parse backups: %s", err)
}
return backupList, nil
}
func (b *Barman) validateBackupName(ctx context.Context, name string) error {
backupList, err := b.ListBackups(ctx)
if err != nil {
return fmt.Errorf("failed to list backups: %s", err)
}
for _, backup := range backupList.Backups {
if backup.Name == name {
return fmt.Errorf("backup name '%s' already exists", name)
}
}
return nil
}
func formatTimestamp(timestamp string) (string, error) {
if strings.HasSuffix(timestamp, "Z") {
timestamp = strings.TrimSuffix(timestamp, "Z") + "+00:00"
}
parsedTime, err := time.Parse(time.RFC3339, timestamp)
if err != nil {
return "", fmt.Errorf("failed to parse timestamp: %s", err)
}
return parsedTime.Format("2006-01-02T15:04:05-07:00"), nil
}