-
Notifications
You must be signed in to change notification settings - Fork 51
/
etcdbackup.go
149 lines (132 loc) · 3.48 KB
/
etcdbackup.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
package etcdbackup
import (
"bufio"
"context"
"fmt"
"io"
"net/url"
"os"
"sort"
"time"
azstorage "github.com/Azure/azure-sdk-for-go/storage"
"github.com/coreos/etcd/clientv3"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/openshift/openshift-azure/pkg/util/azureclient/storage"
)
// EtcdBackup used to perform backup maintenance
type EtcdBackup interface {
SaveSnapshot(ctx context.Context, bname string) error
Retrieve(srcBlob, destPath string) error
Delete(name string) error
Prune() error
}
type etcdBackup struct {
etcdContainer storage.Container
etcdClient *clientv3.Client
maxBackups int
log *logrus.Entry
}
var _ EtcdBackup = &etcdBackup{}
// NewEtcdBackup create a new instance
func NewEtcdBackup(log *logrus.Entry, etcdContainer storage.Container, etcdClient *clientv3.Client, maxBackups int) EtcdBackup {
eb := etcdBackup{
etcdContainer: etcdContainer,
etcdClient: etcdClient,
maxBackups: maxBackups,
log: log,
}
return &eb
}
func (b *etcdBackup) SaveSnapshot(ctx context.Context, bname string) error {
b.log.Infof("Creating Snapshot")
rc, err := b.etcdClient.Snapshot(ctx)
if err != nil {
return errors.Wrap(err, "failed to receive snapshot")
}
defer rc.Close()
b.log.Infof("Creating New Blob")
blob := b.etcdContainer.GetBlobReference(bname)
err = blob.CreateBlockBlob(&azstorage.PutBlobOptions{})
if err != nil {
return errors.Wrapf(err, "failed to create blob %v", bname)
}
bw, err := newBlobWriter(blob)
if err != nil {
return errors.Wrap(err, "failed to create blob")
}
b.log.Infof("Copying blocks to blob")
bufferedBw := bufio.NewWriterSize(bw, 1024*1024)
_, err = io.Copy(bufferedBw, rc)
if err != nil {
return errors.Wrap(err, "failed to copy blob blocks")
}
err = bufferedBw.Flush()
if err != nil {
return errors.Wrap(err, "failed to flush to blob")
}
// Note: do not use defer, as we want to keep the err
err = bw.Close()
if err != nil {
return errors.Wrap(err, "BlobWriter.Close")
}
b.log.Infof("Snapshot saved to blob storage")
return nil
}
func (b *etcdBackup) Prune() error {
params := azstorage.ListBlobsParameters{
Prefix: "backup-",
}
blobs, err := b.etcdContainer.ListBlobs(params)
if err != nil {
return err
}
toDelete := 0
if len(blobs.Blobs) > b.maxBackups {
toDelete = len(blobs.Blobs) - b.maxBackups
} else {
return nil
}
// this should sort oldest first
sort.Slice(blobs.Blobs, func(i, j int) bool {
return time.Time(blobs.Blobs[i].Properties.LastModified).Before(time.Time(blobs.Blobs[j].Properties.LastModified))
})
for _, blob := range blobs.Blobs[:toDelete] {
b.log.Infof("pruning blob %v", blob.Name)
err = blob.Delete(nil)
if err != nil {
return errors.Wrapf(err, "error deleting blob %v : %v", blob.Name, err)
}
}
return nil
}
func (b *etcdBackup) Delete(name string) error {
blob := b.etcdContainer.GetBlobReference(name)
return blob.Delete(nil)
}
func (b *etcdBackup) Retrieve(srcBlob, destPath string) error {
b.log.Printf("copy blob %v to filesystem %v", srcBlob, destPath)
blob := b.etcdContainer.GetBlobReference(srcBlob)
var rc io.ReadCloser
var err error
for {
rc, err = blob.Get(&azstorage.GetBlobOptions{})
if err, ok := err.(*url.Error); ok && err.Timeout() {
time.Sleep(time.Second)
fmt.Println("timeout: retrying")
continue
}
break
}
if err != nil {
return err
}
defer rc.Close()
df, err := os.Create(destPath)
if err != nil {
return err
}
defer df.Close()
_, err = io.Copy(df, rc)
return err
}