-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_ebs_snapshot.go
381 lines (331 loc) · 11.7 KB
/
mod_ebs_snapshot.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
/******************************************************************************\
* Copyright (C) 2024-2024 The Molibackup Authors. All rights reserved. *
* Licensed under the Apache version 2.0 License *
* Homepage: https://github.com/fdupoux/molibackup *
\******************************************************************************/
package main
import (
"fmt"
"regexp"
"sort"
"time"
"github.com/gookit/slog"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
)
// Structure of the job configuration for this specific module
type JobConfigEbsSnapshot struct {
Module string `koanf:"module"`
Enabled any `koanf:"enabled"`
DryRun bool `koanf:"dryrun"`
Retention int64 `koanf:"retention"`
AwsRegion string `koanf:"aws_region"`
AccessKeyId string `koanf:"accesskey_id"`
AccessKeySecret string `koanf:"accesskey_secret"`
InstanceId string `koanf:"instance_id"`
InstanceTags any `koanf:"instance_tags"`
VolumeTags any `koanf:"volume_tags"`
LockMode string `koanf:"lock_mode"`
LockDuration int32 `koanf:"lock_duration"`
}
type backup_ebs_snapshot struct {
config JobConfigEbsSnapshot
cfg aws.Config
client *ec2.Client
volumes []ProviderAwsEbsVolume
}
var validateConfigJobdef = []ConfigEntryValidation{
{
entryname: "module",
entrytype: "string",
mandatory: true,
allowedval: []string{"ebs-snapshot"},
},
{
entryname: "enabled",
entrytype: "",
mandatory: false,
defaultval: "true",
allowedval: []string{"true", "false"},
},
{
entryname: "dryrun",
entrytype: "bool",
mandatory: false,
defaultval: "false",
allowedval: []string{"true", "false"},
},
{
entryname: "retention",
entrytype: "int",
mandatory: false,
defaultval: "30",
allowedval: nil,
},
{
entryname: "aws_region",
entrytype: "string",
mandatory: true,
allowedval: nil,
},
{
entryname: "accesskey_id",
entrytype: "string",
mandatory: false,
defaultval: "",
allowedval: nil,
},
{
entryname: "accesskey_secret",
entrytype: "string",
mandatory: false,
defaultval: "",
allowedval: nil,
},
{
entryname: "instance_id",
entrytype: "string",
mandatory: false,
defaultval: "",
allowedval: nil,
},
{
entryname: "instance_tags",
entrytype: "",
mandatory: false,
defaultval: "",
allowedval: nil,
},
{
entryname: "volume_tags",
entrytype: "",
mandatory: false,
defaultval: "",
allowedval: nil,
},
{
entryname: "lock_mode",
entrytype: "string",
mandatory: false,
defaultval: "",
allowedval: []string{"compliance", "governance"},
},
{
entryname: "lock_duration",
entrytype: "int",
mandatory: false,
defaultval: "7",
allowedval: nil,
},
}
func (b *backup_ebs_snapshot) LoadConfiguration(jobname string) error {
// Original job config before validation and defaults
var origconf JobConfigEbsSnapshot
// Path of the job config section relative to the root of the config file
jobpath := fmt.Sprintf("jobs.%s", jobname)
slog.Debugf("Getting original job configuration (before validation and defaults) ...")
if err := kconfig.Unmarshal(jobpath, &origconf); err != nil {
return fmt.Errorf("failed to unmarshal path %s: %v", jobpath, err)
}
slog.Debugf("Dump of the initial configuration:")
slog.Debugf("- Module=\"%v\"", origconf.Module)
slog.Debugf("- Enabled=%v", origconf.Enabled)
slog.Debugf("- DryRun=%v", origconf.DryRun)
slog.Debugf("- Retention=%v", origconf.Retention)
slog.Debugf("- AwsRegion=\"%v\"", origconf.AwsRegion)
slog.Debugf("- AccessKeyId=\"%v\"", origconf.AccessKeyId)
slog.Debugf("- AccessKeySecret=\"%v\"", origconf.AccessKeySecret)
slog.Debugf("- InstanceId=\"%v\"", origconf.InstanceId)
slog.Debugf("- InstanceTags=\"%v\"", origconf.InstanceTags)
slog.Debugf("- VolumeTags=\"%v\"", origconf.VolumeTags)
slog.Debugf("- LockMode=\"%v\"", origconf.LockMode)
slog.Debugf("- LockDuration=\"%v\"", origconf.LockDuration)
slog.Debugf("Validating the job configuration and setting default values ...")
if err := configValidateAndSetDefaults(jobpath, validateConfigJobdef); err != nil {
return fmt.Errorf("failed to validate job configuration: %w", err)
}
slog.Debugf("Getting processed job configuration (after validation and defaults) ...")
if err := kconfig.Unmarshal(jobpath, &b.config); err != nil {
return fmt.Errorf("failed to unmarshal path %s: %v", jobpath, err)
}
slog.Debugf("Advanced validation of the job configuration ...")
if b.config.InstanceId != "" {
matched, _ := regexp.MatchString("^(local|i-[a-z0-9]{17})$", b.config.InstanceId)
if matched == false {
return fmt.Errorf("Option \"instance_id\" must be either \"local\" or in the \"i-0123456789abcdef0\" format")
}
}
if b.config.Retention <= 0 {
return fmt.Errorf("Option \"retention\" must be a valid number greater than 0")
}
slog.Debugf("Dump of the processed configuration:")
slog.Debugf("- Module=\"%v\"", b.config.Module)
slog.Debugf("- Enabled=%v", b.config.Enabled)
slog.Debugf("- DryRun=%v", b.config.DryRun)
slog.Debugf("- Retention=%v", b.config.Retention)
slog.Debugf("- AwsRegion=\"%v\"", b.config.AwsRegion)
slog.Debugf("- AccessKeyId=\"%v\"", b.config.AccessKeyId)
slog.Debugf("- AccessKeySecret=\"%v\"", b.config.AccessKeySecret)
slog.Debugf("- InstanceId=\"%v\"", b.config.InstanceId)
slog.Debugf("- InstanceTags=\"%v\"", origconf.InstanceTags)
slog.Debugf("- VolumeTags=\"%v\"", b.config.VolumeTags)
slog.Debugf("- LockMode=\"%v\"", origconf.LockMode)
slog.Debugf("- LockDuration=\"%v\"", origconf.LockDuration)
return nil
}
func (b *backup_ebs_snapshot) InitialiseModule() error {
var err error
// Load the configuration using an access key pair if it has been provided in the configuration
b.cfg, err = ProviderAwsLoadConfig(b.config.AwsRegion, b.config.AccessKeyId, b.config.AccessKeySecret)
if err != nil {
return fmt.Errorf("%w", err)
}
// Dynamically determine the EC2 Instance ID if requested in the configuration
if b.config.InstanceId == "local" {
slog.Debugf("Trying to detect the instance ID of the local instance ...")
b.config.InstanceId, err = ProviderAwsGetCurrentInstance(b.cfg)
if err != nil {
return fmt.Errorf("failed to detect the instance ID of the local instance: %w", err)
}
slog.Debugf("Have detected the instance ID of the local instance as %s", b.config.InstanceId)
}
// Create a client
b.client = ProviderAwsNewEc2Client(b.cfg)
// Find list of all EBS volumes that match the conditions specific in the configuration
err = b.findRelevantVolumes()
if err != nil {
return fmt.Errorf("%w", err)
}
return nil
}
func (b *backup_ebs_snapshot) findRelevantVolumes() error {
var results []ProviderAwsEbsVolume
instags := make(map[string]string)
voltags := make(map[string]string)
// Parse "instance_tags" option
if b.config.InstanceTags != "" {
tags, ok := b.config.InstanceTags.(map[string]any)
if ok == true {
for key, val := range tags {
instags[key] = fmt.Sprintf("%v", val)
}
}
}
// Parse "volume_tags" option
if b.config.VolumeTags != "" {
tags, ok := b.config.VolumeTags.(map[string]any)
if ok == true {
for key, val := range tags {
voltags[key] = fmt.Sprintf("%v", val)
}
}
}
// Get list of instances that match the conditions specified
slog.Debugf("Listing instances based on instance_id=\"%s\" and instance_tags=\"%v\" ...", b.config.InstanceId, instags)
instances, err := ProviderAwsGetEc2Instances(b.client, b.config.InstanceId, instags)
if err != nil {
return fmt.Errorf("%w", err)
}
if len(instances) == 0 {
slog.Warnf("Have not found any instance matching the conditions")
}
// Go through each instance
for _, instance := range instances {
slog.Debugf("Listing volumes attached to instance \"%s\" with volume_tags=\"%v\" ...", instance.instanceId, voltags)
volumes, err := ProviderAwsGetEbsVolumes(b.client, instance.instanceId, voltags)
if err != nil {
return fmt.Errorf("%w", err)
}
// Go through each volume
for _, curvol := range volumes {
slog.Debugf("Found volume: volumeId=\"%s\" volumeName=\"%s\" instanceId=\"%s\"",
curvol.volumeId, curvol.volumeName, instance.instanceId)
results = append(results, curvol)
}
}
if len(results) == 0 {
slog.Warnf("Have not found any volume matching the conditions")
}
b.volumes = results
return nil
}
func (b *backup_ebs_snapshot) CreateBackup() error {
var basename string
for _, curvol := range b.volumes {
slog.Debugf("Considering backup for volume: volumeId=\"%s\" volumeName=\"%s\" ...", curvol.volumeId, curvol.volumeName)
if curvol.volumeName != "" {
basename = curvol.volumeName
} else {
basename = curvol.volumeId
}
curtime := time.Now()
snapname := fmt.Sprintf("%s-%s", basename, curtime.Format(time.RFC3339))
snapdate := fmt.Sprintf("%04d%02d%02d", curtime.Year(), curtime.Month(), curtime.Day())
snaptime := fmt.Sprintf("%v", curtime.Unix())
if b.config.DryRun == false {
snapshotId, err := ProviderAwsCreateEbsSnapshot(b.client, curvol.volumeId, snapname, snapdate, snaptime, b.config.LockMode, b.config.LockDuration)
if err != nil {
return fmt.Errorf("%w", err)
}
slog.Infof("Successfully created snapshot \"%s\" of volume \"%s\"", snapshotId, curvol.volumeId)
} else {
slog.Infof("Dryrun: Not creating snapshot of volume \"%s\"", curvol.volumeId)
}
}
return nil
}
func (b *backup_ebs_snapshot) ListBackups() ([]BackupItem, error) {
var resultsOrdered []BackupItem
var snapshotNames []string
resultsUnordered := make(map[string]BackupItem)
// Enumerate volumes and their snapshots to get a list of relevant snapshots
for _, curvol := range b.volumes {
slog.Debugf("Listing snapshots from volume: volumeId=\"%s\" ...", curvol.volumeId)
snapshots, err := ProviderAwsGetEbsSnapshots(b.client, curvol.volumeId)
if err != nil {
return nil, err
}
for _, snapshot := range snapshots {
item := BackupItem{}
item.identifier = snapshot.snapshotId
item.description = snapshot.snapshotDesc
item.timestamp = snapshot.snapshotTime
snapshotNames = append(snapshotNames, item.description)
resultsUnordered[item.description] = item
snaptime := time.Unix(snapshot.snapshotTime, 0)
slog.Debugf("Found snapshot: id=\"%s\" desc=\"%s\" created=\"%v\" vol=\"%s\"",
snapshot.snapshotId, snapshot.snapshotDesc, snaptime.Format(time.RFC3339), snapshot.volumeId)
}
}
// Reorder the snapshots names alphabetically
sort.Strings(snapshotNames)
// Create the final list of items in the alphabetical order
for _, snapname := range snapshotNames {
resultsOrdered = append(resultsOrdered, resultsUnordered[snapname])
}
return resultsOrdered, nil
}
func (b *backup_ebs_snapshot) DeleteOldBackups(bkpitems []BackupItem) error {
retention := b.config.Retention
curtime := time.Now().Unix()
for _, item := range bkpitems {
snapshotAge := (curtime - item.timestamp) / 86400
snapDelete := snapshotAge > retention
slog.Debugf("Considering deletion of snapshot: id=\"%s\" desc=\"%s\" age=%v retention=%v ...",
item.identifier, item.description, snapshotAge, retention)
if snapDelete == true {
if b.config.DryRun == false {
err := ProviderAwsDeleteEbsSnapshot(b.client, item.identifier)
if err != nil {
return fmt.Errorf("%w", err)
}
slog.Infof("Deleted snapshot: id=\"%s\" desc=\"%s\" age=%v retention=%v", item.identifier, item.description, snapshotAge, retention)
} else {
slog.Infof("Dryrun: Not deleting snapshot: id=\"%s\" desc=\"%s\" age=%d retention=%v", item.identifier, item.description, snapshotAge, retention)
}
} else {
slog.Infof("Keeping snapshot: id=\"%s\" desc=\"%s\" age=%d retention=%d", item.identifier, item.description, snapshotAge, retention)
}
}
return nil
}