/
main.go
264 lines (217 loc) · 6.25 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
package main
import (
"flag"
"fmt"
"log"
"math"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
type tagslice []*string
func (t tagslice) String() string {
var str string
for i, key := range t {
if i != 0 {
str += ", "
}
str += *key
}
return str
}
func (t *tagslice) Set(value string) error {
*t = append(*t, &value)
return nil
}
const (
PurgeAfterKey = "PurgeAfter"
PurgeAllowKey = "PurgeAllow"
PurgeAfterFormat = time.RFC3339
MinSnapshotInterval = 15 //seconds
MaxSnapshotRetries = 3
)
var (
tags tagslice
region = flag.String("region", "", "AWS region to use")
tagPrefix = flag.String("tagPrefix", "auto-snap", "String to prefix to tag name, description")
purgeAfterDays = flag.Int("k", 0, "Purge snapshot after this many days. Zero value means never purge")
purgeSnapshots = flag.Bool("p", true, "Enable purging of snapshots")
)
func main() {
flag.Var(&tags, "tags", "Select EBS volumes using these tag keys e.g. 'Daily-Backup'. Tag values should be == 'true'")
flag.Parse()
if len(tags) == 0 {
fmt.Println("You must specify at least one tag")
flag.PrintDefaults()
os.Exit(1)
}
config := aws.NewConfig()
if *region != "" {
config.Region = region
}
svc := ec2.New(session.New(), config)
err := CreateSnapshots(svc)
if err != nil {
log.Fatal(err)
}
if *purgeSnapshots {
err = PurgeSnapshots(svc)
if err != nil {
log.Fatal(err)
}
}
}
func CreateSnapshots(svc *ec2.EC2) error {
volumes, err := GetBackupVolumes(svc)
if err != nil {
return err
}
if len(volumes) == 0 {
log.Printf("No volumes found matching tags: %s\n", tags)
}
log.Printf("\nSnapshotting volumes...\n")
for i, volume := range volumes {
csi := ec2.CreateSnapshotInput{}
csi.VolumeId = volume.VolumeId
volname, _ := getTagValue("Name", volume.Tags)
var description string
if volname == "" {
description = fmt.Sprintf("%s: %s", *tagPrefix, *volume.VolumeId)
} else {
description = fmt.Sprintf("%s: %s (%s)", *tagPrefix, volname, *volume.VolumeId)
}
csi.Description = &description
var err error
var snap *ec2.Snapshot
retries := 0
for {
snap, err = svc.CreateSnapshot(&csi)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
retries++
if retries > MaxSnapshotRetries {
return fmt.Errorf("Maximum snapshot retries reached for volume %s", *volume.VolumeId)
}
if aerr.Code() == "SnapshotCreationPerVolumeRateExceeded" {
sleep := time.Duration(MinSnapshotInterval+math.Pow(float64(5), float64(retries))) * time.Second
log.Printf("SnapshotCreationPerVolumeRateExceeded, sleeping for %f\n", sleep.Seconds())
time.Sleep(sleep)
}
} else {
return err
}
} else {
break
}
}
fmt.Printf("Name: %-15s VolumeId: %-22s Size: %4d GiB\n", volname, *volume.VolumeId, *volume.Size)
err = CreateSnapshotTags(svc, *snap.SnapshotId, volname, *volume.VolumeId)
if err != nil {
return err
}
// wait before kicking off next snapshot
if i < (len(volumes) - 1) {
time.Sleep(time.Duration(MinSnapshotInterval) * time.Second)
}
}
return nil
}
func PurgeSnapshots(svc *ec2.EC2) error {
dsi := ec2.DescribeSnapshotsInput{}
filter := &ec2.Filter{}
filterName := "tag:" + PurgeAllowKey
filter.Name = &filterName
value := "true"
filter.Values = []*string{&value}
dsi.Filters = append(dsi.Filters, filter)
dso, err := svc.DescribeSnapshots(&dsi)
if err != nil {
return fmt.Errorf("describeSnapshots error, %s", err)
}
log.Printf("\nPurging Snapshots...\n")
purgeCount := 0
for _, snapshot := range dso.Snapshots {
var paVal string
var found bool
if paVal, found = getTagValue(PurgeAfterKey, snapshot.Tags); !found {
log.Printf("snapshot ID %s has tag '%s' but does not have a '%s' tag. Skipping purge...", *snapshot.SnapshotId, PurgeAllowKey, PurgeAfterKey)
continue
}
pa, err := time.Parse(PurgeAfterFormat, paVal)
if err != nil {
return err
}
nowDate := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)
if pa.Before(nowDate) {
deli := ec2.DeleteSnapshotInput{}
deli.SnapshotId = snapshot.SnapshotId
_, err := svc.DeleteSnapshot(&deli)
if err != nil {
return fmt.Errorf("error purging Snapshot ID %s, err %s", *snapshot.SnapshotId, err)
}
fmt.Printf("ID: %-22s Size: %4d GiB\n", *snapshot.SnapshotId, *snapshot.VolumeSize)
purgeCount++
}
}
if purgeCount == 0 {
fmt.Printf("No snapshots were purged\n")
}
return nil
}
func CreateSnapshotTags(svc *ec2.EC2, resourceID, volumeName, volumeID string) error {
var nKey, nVal string
var tags []*ec2.Tag
nKey = "Name"
if volumeName == "" {
nVal = fmt.Sprintf("%s: %s, %s", *tagPrefix, volumeID, time.Now().Format("2006-01-02"))
} else {
nVal = fmt.Sprintf("%s: %s, %s", *tagPrefix, volumeName, time.Now().Format("2006-01-02"))
}
tags = append(tags, &ec2.Tag{Key: &nKey, Value: &nVal})
if *purgeAfterDays > 0 {
var paKey, paVal string
var pKey, pVal string
paKey = PurgeAfterKey
paDate := time.Now().Add(time.Duration(*purgeAfterDays*24) * time.Hour)
paVal = time.Date(paDate.Year(), paDate.Month(), paDate.Day(), 0, 0, 0, 0, time.UTC).Format(PurgeAfterFormat)
tags = append(tags, &ec2.Tag{Key: &paKey, Value: &paVal})
pKey = PurgeAllowKey
pVal = "true"
tags = append(tags, &ec2.Tag{Key: &pKey, Value: &pVal})
}
cti := ec2.CreateTagsInput{Tags: tags}
cti.Resources = append(cti.Resources, &resourceID)
_, err := svc.CreateTags(&cti)
if err != nil {
return err
}
return nil
}
func GetBackupVolumes(svc *ec2.EC2) ([]*ec2.Volume, error) {
dvi := ec2.DescribeVolumesInput{}
for _, tag := range tags {
filter := &ec2.Filter{}
filterName := "tag:" + *tag
filter.Name = &filterName
value := "true"
filter.Values = []*string{&value}
dvi.Filters = append(dvi.Filters, filter)
}
dvo, err := svc.DescribeVolumes(&dvi)
if err != nil {
return nil, fmt.Errorf("describeVolumes error, %s", err)
}
return dvo.Volumes, nil
}
// getTagValue returns the value for the first matched key
func getTagValue(key string, tags []*ec2.Tag) (string, bool) {
for _, tag := range tags {
if *tag.Key == key {
return *tag.Value, true
}
}
return "", false
}