forked from rancher/rke-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
442 lines (400 loc) · 11.1 KB
/
main.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
"github.com/minio/minio-go"
"github.com/minio/minio-go/pkg/credentials"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
const (
backupBaseDir = "/backup"
backupRetries = 4
s3ServerRetries = 3
contentType = "application/zip"
)
var commonFlags = []cli.Flag{
cli.StringFlag{
Name: "endpoints",
Usage: "Etcd endpoints",
Value: "127.0.0.1:2379",
},
cli.BoolFlag{
Name: "debug",
Usage: "Verbose logging information for debugging purposes",
EnvVar: "RANCHER_DEBUG",
},
cli.StringFlag{
Name: "name",
Usage: "Backup name to take once",
},
cli.StringFlag{
Name: "cacert",
Usage: "Etcd CA client certificate path",
EnvVar: "ETCD_CACERT",
},
cli.StringFlag{
Name: "cert",
Usage: "Etcd client certificate path",
EnvVar: "ETCD_CERT",
},
cli.StringFlag{
Name: "key",
Usage: "Etcd client key path",
EnvVar: "ETCD_KEY",
},
cli.BoolFlag{
Name: "s3-backup",
Usage: "Backup etcd sanpshot to your s3 server, set true or false",
EnvVar: "S3_BACKUP",
},
cli.StringFlag{
Name: "s3-endpoint",
Usage: "Specify s3 endpoint address",
EnvVar: "S3_ENDPOINT",
},
cli.StringFlag{
Name: "s3-accessKey",
Usage: "Specify s3 access key",
EnvVar: "S3_ACCESS_KEY",
},
cli.StringFlag{
Name: "s3-secretKey",
Usage: "Specify s3 secret key",
EnvVar: "S3_SECRET_KEY",
},
cli.StringFlag{
Name: "s3-bucketName",
Usage: "Specify s3 bucket name",
EnvVar: "S3_BUCKET_NAME",
},
cli.StringFlag{
Name: "s3-region",
Usage: "Specify s3 bucket region",
EnvVar: "S3_BUCKET_REGION",
},
}
type s3Server struct {
Backup bool
Endpoint string
AccessKey string
SecretKey string
BucketName string
Region string
}
func init() {
log.SetOutput(os.Stderr)
}
func main() {
err := os.Setenv("ETCDCTL_API", "3")
if err != nil {
log.Fatal(err)
}
app := cli.NewApp()
app.Name = "Etcd Wrapper"
app.Usage = "Utility services for Etcd cluster backup"
app.Commands = []cli.Command{
RollingBackupCommand(),
}
err = app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func RollingBackupCommand() cli.Command {
snapshotFlags := []cli.Flag{
cli.DurationFlag{
Name: "creation",
Usage: "Create backups after this time interval in minutes",
Value: 5 * time.Minute,
},
cli.DurationFlag{
Name: "retention",
Usage: "Retain backups within this time interval in hours",
Value: 24 * time.Hour,
},
cli.BoolFlag{
Name: "once",
Usage: "Take backup only once",
},
}
snapshotFlags = append(snapshotFlags, commonFlags...)
return cli.Command{
Name: "etcd-backup",
Usage: "Perform etcd backup tools",
Subcommands: []cli.Command{
{
Name: "save",
Usage: "Take snapshot on all etcd hosts and backup to s3 compatible storage",
Flags: snapshotFlags,
Action: RollingBackupAction,
},
{
Name: "download",
Usage: "Download specified snapshot from s3 storage server",
Flags: commonFlags,
Action: DownloadBackupAction,
},
},
}
}
func SetLoggingLevel(debug bool) {
if debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
}
func RollingBackupAction(c *cli.Context) error {
SetLoggingLevel(c.Bool("debug"))
creationPeriod := c.Duration("creation")
retentionPeriod := c.Duration("retention")
etcdCert := c.String("cert")
etcdCACert := c.String("cacert")
etcdKey := c.String("key")
etcdEndpoints := c.String("endpoints")
if len(etcdCert) == 0 || len(etcdCACert) == 0 || len(etcdKey) == 0 {
log.WithFields(log.Fields{
"etcdCert": etcdCert,
"etcdCACert": etcdCACert,
"etcdKey": etcdKey,
}).Errorf("Failed to find etcd cert or key paths")
return fmt.Errorf("Failed to find etcd cert or key paths")
}
log.WithFields(log.Fields{
"creation": creationPeriod,
"retention": retentionPeriod,
}).Info("Initializing Rolling Backups")
s3Backup := c.Bool("s3-backup")
s3Config := &s3Server{
Backup: s3Backup,
Endpoint: c.String("s3-endpoint"),
AccessKey: c.String("s3-accessKey"),
SecretKey: c.String("s3-secretKey"),
BucketName: c.String("s3-bucketName"),
Region: c.String("s3-region"),
}
client := &minio.Client{}
if s3Backup {
svc, err := setS3Service(s3Config, true)
if err != nil {
log.WithFields(log.Fields{
"s3-endpoint": s3Config.Endpoint,
"s3-bucketName": s3Config.BucketName,
"s3-accessKey": s3Config.AccessKey,
"s3-region": s3Config.Region,
}).Errorf("failed to set s3 server: %s", err)
return fmt.Errorf("faield to set s3 server: %+v", err)
}
client = svc
}
if c.Bool("once") {
backupName := c.String("name")
if len(backupName) == 0 {
backupName = fmt.Sprintf("%s_etcd", time.Now().Format(time.RFC3339))
}
return CreateBackup(backupName, etcdCACert, etcdCert, etcdKey, etcdEndpoints, client, s3Config)
}
backupTicker := time.NewTicker(creationPeriod)
for {
select {
case backupTime := <-backupTicker.C:
backupName := fmt.Sprintf("%s_etcd", backupTime.Format(time.RFC3339))
CreateBackup(backupName, etcdCACert, etcdCert, etcdKey, etcdEndpoints, client, s3Config)
DeleteBackups(backupTime, retentionPeriod)
}
}
}
func CreateBackup(backupName string, etcdCACert, etcdCert, etcdKey, endpoints string, svc *minio.Client, server *s3Server) error {
failureInterval := 15 * time.Second
backupDir := fmt.Sprintf("%s/%s", backupBaseDir, backupName)
var err error
for retries := 0; retries <= backupRetries; retries++ {
if retries > 0 {
time.Sleep(failureInterval)
}
// check if the cluster is healthy
cmd := exec.Command("etcdctl",
fmt.Sprintf("--endpoints=[%s]", endpoints),
"--cacert="+etcdCACert,
"--cert="+etcdCert,
"--key="+etcdKey,
"endpoint", "health")
data, err := cmd.CombinedOutput()
if strings.Contains(string(data), "unhealthy") {
log.WithFields(log.Fields{
"error": err,
"data": string(data),
}).Warn("Checking member health failed from etcd member")
continue
}
cmd = exec.Command("etcdctl",
fmt.Sprintf("--endpoints=[%s]", endpoints),
"--cacert="+etcdCACert,
"--cert="+etcdCert,
"--key="+etcdKey,
"snapshot", "save", backupDir)
startTime := time.Now()
data, err = cmd.CombinedOutput()
endTime := time.Now()
if err != nil {
log.WithFields(log.Fields{
"attempt": retries + 1,
"error": err,
"data": string(data),
}).Warn("Backup failed")
continue
}
log.WithFields(log.Fields{
"name": backupName,
"runtime": endTime.Sub(startTime),
}).Info("Created backup")
if server.Backup {
err := uploadBackupFile(svc, server.BucketName, backupName, backupDir)
if err == nil {
return nil
}
}
break
}
return err
}
func DeleteBackups(backupTime time.Time, retentionPeriod time.Duration) {
files, err := ioutil.ReadDir(backupBaseDir)
if err != nil {
log.WithFields(log.Fields{
"dir": backupBaseDir,
"error": err,
}).Warn("Can't read backup directory")
}
cutoffTime := backupTime.Add(retentionPeriod * -1)
for _, file := range files {
if file.IsDir() {
log.WithFields(log.Fields{
"name": file.Name(),
}).Warn("Ignored directory, expecting file")
continue
}
backupTime, err2 := time.Parse(time.RFC3339, strings.Split(file.Name(), "_")[0])
if err2 != nil {
log.WithFields(log.Fields{
"name": file.Name(),
"error": err2,
}).Warn("Couldn't parse backup")
} else if backupTime.Before(cutoffTime) {
DeleteBackup(file)
}
}
}
func DeleteBackup(file os.FileInfo) {
toDelete := fmt.Sprintf("%s/%s", backupBaseDir, file.Name())
cmd := exec.Command("rm", "-r", toDelete)
startTime := time.Now()
err2 := cmd.Run()
endTime := time.Now()
if err2 != nil {
log.WithFields(log.Fields{
"name": file.Name(),
"error": err2,
}).Warn("Delete backup failed")
} else {
log.WithFields(log.Fields{
"name": file.Name(),
"runtime": endTime.Sub(startTime),
}).Info("Deleted backup")
}
}
func setS3Service(server *s3Server, useSSL bool) (*minio.Client, error) {
// Initialize minio client object.
log.Info("invoking set s3 service client")
var err error
var svc = &minio.Client{}
for retries := 0; retries <= s3ServerRetries; retries++ {
// if the s3 access key and secret is not set use iam role
if len(server.AccessKey) == 0 && len(server.SecretKey) == 0 {
log.Info("invoking set s3 service client use IAM role")
iam := credentials.NewIAM("")
svc, err = minio.NewWithCredentials("s3.amazonaws.com", iam, true, "")
} else if len(server.Region) != 0 {
svc, err = minio.NewWithRegion(server.Endpoint, server.AccessKey, server.SecretKey, useSSL, server.Region)
} else {
svc, err = minio.New(server.Endpoint, server.AccessKey, server.SecretKey, useSSL)
}
if err != nil {
log.Infof("failed to init s3 client server: %v, retried %d times", err, retries)
if retries >= s3ServerRetries {
return nil, fmt.Errorf("failed to set s3 server: %v", err)
}
continue
}
break
}
found, err := svc.BucketExists(server.BucketName)
if err != nil {
return nil, fmt.Errorf("failed to check s3 bucket:%s, err:%v", server.BucketName, err)
}
if !found {
return nil, fmt.Errorf("bucket %s is not found", server.BucketName)
}
return svc, nil
}
func uploadBackupFile(svc *minio.Client, bucketName, fileName, filePath string) error {
// Upload the zip file with FPutObject
log.Infof("invoking uploading backup file %s to s3", fileName)
for retries := 0; retries <= s3ServerRetries; retries++ {
n, err := svc.FPutObject(bucketName, fileName, filePath, minio.PutObjectOptions{ContentType: contentType})
if err != nil {
log.Infof("failed to upload etcd snapshot file: %v, retried %d times", err, retries)
if retries >= s3ServerRetries {
return fmt.Errorf("failed to upload etcd snapshot file: %v", err)
}
continue
}
log.Infof("Successfully uploaded %s of size %d\n", fileName, n)
break
}
return nil
}
func DownloadBackupAction(c *cli.Context) error {
log.Info("Initializing Download Backups")
SetLoggingLevel(c.Bool("debug"))
s3Config := &s3Server{
Endpoint: c.String("s3-endpoint"),
AccessKey: c.String("s3-accessKey"),
SecretKey: c.String("s3-secretKey"),
BucketName: c.String("s3-bucketName"),
Region: c.String("s3-region"),
}
client, err := setS3Service(s3Config, true)
if err != nil {
log.WithFields(log.Fields{
"s3-endpoint": s3Config.Endpoint,
"s3-bucketName": s3Config.BucketName,
"s3-accessKey": s3Config.AccessKey,
"s3-region": s3Config.Region,
}).Errorf("failed to set s3 server: %s", err)
return fmt.Errorf("faield to set s3 server: %+v", err)
}
fileName := c.String("name")
if len(fileName) == 0 {
return fmt.Errorf("empty file name")
}
log.Infof("invoking downloading backup files: %s", fileName)
for retries := 0; retries <= s3ServerRetries; retries++ {
err := client.FGetObject(s3Config.BucketName, fileName, backupBaseDir+"/"+fileName, minio.GetObjectOptions{})
if err != nil {
log.Infof("failed to download etcd snapshot file: %v, retried %d times", err, retries)
if retries >= s3ServerRetries {
return fmt.Errorf("failed to download etcd snapshot file: %v", err)
}
continue
}
log.Infof("Successfully download %s from s3 server", fileName)
break
}
return nil
}