-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
218 lines (197 loc) · 5.17 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
package main
import (
"fmt"
"os"
"time"
log "github.com/sirupsen/logrus"
"github.com/araddon/dateparse"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"gopkg.in/urfave/cli.v1"
)
func setVersion(svc s3iface.S3API, bucket string, key string, versionId string) error {
log.Info("Setting version: ", bucket, " ", key, " ", versionId)
if versionId == "DELETE" {
input := &s3.DeleteObjectInput{
Bucket: &bucket,
Key: &key,
}
_, err := svc.DeleteObject(input)
if err != nil {
return err
}
} else {
src := "/" + bucket + "/" + key + "?versionId=" + versionId
input := &s3.CopyObjectInput{
CopySource: &src,
Bucket: &bucket,
Key: &key,
}
_, err := svc.CopyObject(input)
if err != nil {
return err
}
}
return nil
}
func makeDeleteMarker(obj *s3.ObjectVersion) s3.ObjectVersion {
fakeVersionID := "DELETE"
return s3.ObjectVersion{
Key: obj.Key,
LastModified: obj.LastModified,
VersionId: &fakeVersionID,
}
}
func makeDeleteMarker2(obj *s3.DeleteMarkerEntry) s3.ObjectVersion {
fakeVersionID := "DELETE"
return s3.ObjectVersion{
Key: obj.Key,
LastModified: obj.LastModified,
VersionId: &fakeVersionID,
}
}
func processVersion(final *map[string]s3.ObjectVersion, target time.Time, obj *s3.ObjectVersion) {
log.Print("Processing: ", obj)
key := *obj.Key
curObj, exists := (*final)[key]
log.Info("Key ", key, " exists ", exists)
if !exists {
log.Info("Adding key that doesn't exist", key)
// insert a delete marker for things that didn't exist
if obj.LastModified.After(target) {
(*final)[key] = makeDeleteMarker(obj)
return
}
(*final)[key] = *obj
return
}
if obj.LastModified.After(target) {
return
}
// This is our object if
// If it is newer than what is currently in place
// b) curObj is currently a future delete marker
if obj.LastModified.After(*(*final)[key].LastModified) || curObj.LastModified.After(target) {
(*final)[key] = *obj
}
}
func processDeleteMarker(final *map[string]s3.ObjectVersion, target time.Time, obj *s3.DeleteMarkerEntry) {
if obj.LastModified.After(target) {
return
}
key := *obj.Key
curObj, exists := (*final)[key]
if obj.LastModified.After(target) {
return
}
if !exists || obj.LastModified.After(*(*final)[key].LastModified) || curObj.LastModified.After(target) {
(*final)[key] = makeDeleteMarker2(obj)
}
}
func buildVersionDictionary(svc s3iface.S3API, bucket string, target time.Time) (final map[string]s3.ObjectVersion, err error) {
final = make(map[string]s3.ObjectVersion)
log.Print("Getting versions...")
err = svc.ListObjectVersionsPages(&s3.ListObjectVersionsInput{
Bucket: &bucket,
}, func(p *s3.ListObjectVersionsOutput, last bool) (shouldContinue bool) {
for _, obj := range p.Versions {
processVersion(&final, target, obj)
}
for _, obj := range p.DeleteMarkers {
processDeleteMarker(&final, target, obj)
}
return true
})
if err != nil {
fmt.Println("failed to list objects", err)
return
}
return
}
func processDictionary(svc s3iface.S3API, bucket string, final map[string]s3.ObjectVersion) error {
log.Info("Processing final map", final)
for key, value := range final {
err := setVersion(svc, bucket, key, *value.VersionId)
if err != nil {
return err
}
}
return nil
}
func process(c *cli.Context) error {
fmt.Println("Beginning processing")
endpointurl := c.GlobalString("endpoint-url")
awsConfig := &aws.Config{
Endpoint: &endpointurl,
}
sess := session.Must(session.NewSession(awsConfig))
if sess == nil {
fmt.Println("Failed to create aws session")
}
svc := s3.New(sess, awsConfig)
if c.GlobalString("bucket") == "" {
fmt.Println("No bucket specified")
return nil
}
targetTime, err := dateparse.ParseAny(c.GlobalString("time"))
log.Print("Parsed time ", targetTime)
if err != nil {
fmt.Println("Invalid time string", err)
return err
}
objects, err := buildVersionDictionary(svc, c.GlobalString("bucket"), targetTime)
if err != nil {
fmt.Println("Failed building dictionary", err)
return err
}
fmt.Println("Found ", len(objects), " keys")
err = processDictionary(svc, c.GlobalString("bucket"), objects)
if err != nil {
fmt.Println("Failed processing dictionary", err)
}
fmt.Println("Done")
return err
}
func init() {
log.SetOutput(os.Stderr)
// Only log the warning severity or above.
log.SetLevel(log.ErrorLevel)
}
func main() {
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "bucket",
Usage: "required: s3 bucket to process",
},
cli.StringFlag{
Name: "time",
Usage: "required: time to restore to. Use format: 2006-01-02T15:04:05.999999999Z07:00",
},
cli.StringFlag{
Name: "endpoint-url",
Usage: "S3 compatible endpoint url",
},
cli.BoolFlag{
Name: "debug",
Usage: "Use to enable debug logging",
},
}
app.Commands = []cli.Command{
{
Name: "restore",
Usage: "restore the s3 bucket to the time specified",
Action: process,
},
}
log.Print("Running App..")
app.Before = func(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
return nil
}
app.Run(os.Args)
}