-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.go
158 lines (137 loc) · 3.32 KB
/
bucket.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
package main
import (
"compress/gzip"
"errors"
"io"
"os"
"path/filepath"
"sync"
"time"
"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/s3manager"
"github.com/cheggaaa/pb/v3"
)
// Bucket in S3 or compatible system.
type Bucket struct {
sess *session.Session
srv *s3.S3
Name string
contents []*s3.Object
}
// NewBucket sets up a new bucket for access.
func NewBucket(region, name string) (*Bucket, error) {
awscfg := &aws.Config{
Region: aws.String(region),
}
// Use shared credentials in ~/.aws/credentials or from envvars.
// Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
sess, err := session.NewSession(awscfg)
if err != nil {
return nil, err
}
_, err = sess.Config.Credentials.Get()
if err != nil {
return nil, err
}
b := &Bucket{
sess: sess,
srv: s3.New(sess),
Name: name,
}
_, err = b.List()
return b, err
}
// List contents of bucket.
func (b *Bucket) List() ([]*s3.Object, error) {
res, err := b.srv.ListObjectsV2(&s3.ListObjectsV2Input{Bucket: aws.String(b.Name)})
if err != nil {
return nil, err
}
b.contents = res.Contents
return b.contents, nil
}
// Upload a file to S3, calling UploadGzip with a path argument.
func (b *Bucket) Upload(fn string) error {
return b.UploadGzip("db", fn)
}
// UploadGzip compresses a file to S3 with progress.
// The filename will have .gz appended to it.
func (b *Bucket) UploadGzip(path, fn string) error {
f, err := os.Open(fn)
if err != nil {
return err
}
defer f.Close()
piper, pipew := io.Pipe()
wg := sync.WaitGroup{}
wg.Add(1)
name := filepath.Join(path, filepath.Base(fn)+".gz")
go func() {
uploader := s3manager.NewUploader(b.sess)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(b.Name),
Key: aws.String(name),
Body: piper,
})
if err != nil {
pr("Error uploading: %s", err.Error())
time.Sleep(time.Millisecond * 500)
}
wg.Done()
}()
fi, err := f.Stat()
if err != nil {
return err
}
bar := pb.Full.Start64(fi.Size())
reader := bar.NewProxyReader(f)
gzw, _ := gzip.NewWriterLevel(pipew, gzip.BestCompression)
_, err = io.Copy(gzw, reader)
if err != nil {
gzw.Close()
return err
}
gzw.Close()
pipew.Close()
defer piper.Close()
wg.Wait()
bar.Finish()
return nil
}
// Prune files older than a certain number of days.
func (b *Bucket) Prune(base string, days int) (int, error) {
count := 0
list, err := b.List()
if err != nil {
return 0, err
}
backups := []string{}
for _, o := range list {
s := *o.Key
if s[0:3+len(base)] == "db/"+base {
backups = append(backups, *o.Key)
}
}
oldest := time.Now().AddDate(0, 0, -days).Format(base + "-20060102T150405.sql.gz")
pr("Removing older than %s", oldest)
for _, s := range backups {
if filepath.Base(s) < oldest {
pr("\tDeleting %s", filepath.Base(s))
_, err = b.srv.DeleteObject(&s3.DeleteObjectInput{Bucket: aws.String(b.Name), Key: aws.String(s)})
if err != nil {
return count, errors.New("Unable to delete " + s + ": " + err.Error())
}
err = b.srv.WaitUntilObjectNotExists(&s3.HeadObjectInput{
Bucket: aws.String(b.Name),
Key: aws.String(s),
})
if err != nil {
return count, errors.New(s + " did not get deleted: " + err.Error())
}
count++
}
}
return count, nil
}