-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.go
226 lines (181 loc) · 7.04 KB
/
backup.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
package server
import (
"context"
"os"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
api "github.com/chef/automate/api/interservice/deployment"
"github.com/chef/automate/components/automate-deployment/pkg/backup"
)
// CreateBackup creates an Automate Backup
func (s *server) CreateBackup(ctx context.Context, req *api.CreateBackupRequest) (*api.CreateBackupResponse, error) {
if !s.HasConfiguredDeployment() {
return nil, ErrorNotConfigured
}
// lock the deployment for the duration of the backup
// this gives us the following properties:
// - only one backup can take place at a time
// - we don't converge the deployment during a backup
// this means that we won't run into the situation where
// we're deploying new services or configuration while
// we're trying to back up data
//
// NOTE: server.DumpDB doesn't require a lock, so we're
// okay here
err := s.acquireLock(ctx) // Unlocked by the backup runner
if err != nil {
logrus.WithError(err).Error("Failed to acquire lock")
return nil, err
}
sender := s.newEventSender()
task, err := s.backupRunner.CreateBackup(ctx, s.deployment, sender)
if err != nil {
// CreateBackup doesn't look like it can return an error. But if it did,
// the mutex needs to be unlocked?
logrus.WithError(err).Error("Failed to create backup")
return nil, status.Error(codes.Internal, err.Error())
}
s.senderStore.Set(task.TaskID(), sender)
return &api.CreateBackupResponse{Backup: task}, nil
}
// ListBackups lists Automate Backups
func (s *server) ListBackups(ctx context.Context, req *api.ListBackupsRequest) (*api.ListBackupsResponse, error) {
if !s.HasConfiguredDeployment() {
return nil, ErrorNotConfigured
}
backups, err := s.backupRunner.ListBackups(ctx)
if err != nil {
logrus.WithError(err).Error("Failed to list backup")
return nil, status.Error(codes.Internal, err.Error())
}
return &api.ListBackupsResponse{Backups: backups}, nil
}
// ShowBackup shows details about an AutomateBackup
func (s *server) ShowBackup(ctx context.Context, req *api.ShowBackupRequest) (*api.ShowBackupResponse, error) {
if !s.HasConfiguredDeployment() {
return nil, ErrorNotConfigured
}
description, err := s.backupRunner.ShowBackup(ctx, req.Backup)
if err != nil {
logrus.WithError(err).Error("Failed to list backup")
return nil, status.Error(codes.Internal, err.Error())
}
return &api.ShowBackupResponse{Backup: req.Backup, Description: description}, nil
}
// CancelBackup cancels the running backup operation
func (s *server) CancelBackup(ctx context.Context, req *api.CancelBackupRequest) (*api.CancelBackupResponse, error) {
if !s.HasConfiguredDeployment() {
return nil, ErrorNotConfigured
}
err := s.backupRunner.Cancel(ctx)
if err != nil {
logrus.WithError(err).Error("failed to cancle backup operation")
return nil, status.Error(codes.Internal, err.Error())
}
return &api.CancelBackupResponse{}, nil
}
// DeleteBackups deletes a set of 1 or more Automate Backups
func (s *server) DeleteBackups(ctx context.Context, req *api.DeleteBackupsRequest) (*api.DeleteBackupsResponse, error) {
err := s.acquireLock(ctx) // unlocked by the backupRunner
if err != nil {
logrus.WithError(err).Error("Failed to acquire lock")
return nil, err
}
if err := s.backupRunner.DeleteBackups(ctx, s.deployment, req.GetBackups()); err != nil {
logrus.WithError(err).Error("Failed to delete backup")
return nil, status.Error(codes.Internal, err.Error())
}
return &api.DeleteBackupsResponse{}, nil
}
// Restore backup restore an Automate backup
func (s *server) RestoreBackup(ctx context.Context, req *api.RestoreBackupRequest) (*api.RestoreBackupResponse, error) {
res := &api.RestoreBackupResponse{}
// Update the config and reload the backup runner because our config has
// changed.
if err := s.updateUserOverrideConfigFromRestoreBackupRequest(req); err != nil {
return res, err
}
if err := s.reloadBackupRunner(); err != nil {
logrus.WithError(err).Error("failed to load backup runner")
return res, status.Error(codes.Internal, err.Error())
}
// In local mode the default bucket is backups
bucket := "backups"
basePath := ""
// If we are restoring from S3 we'll override the bucket and base path
// with the configuration that has been given.
if req.Restore.GetS3BackupLocation().GetBucketName() != "" {
bucket = req.Restore.GetS3BackupLocation().GetBucketName()
basePath = req.Restore.GetS3BackupLocation().GetBasePath()
}
bgwLocationSpec, err := backup.NewBackupGatewayLocationSpec(
s.deployment.BackupGatewayEndpoint(),
bucket,
basePath,
[]byte(s.deployment.CA().RootCert()),
s.secretStore,
)
if err != nil {
logrus.WithError(err).Error("generate backup-gateway location specification")
return res, status.Error(codes.InvalidArgument, err.Error())
}
remoteRestoreSpec := backup.NewRemoteLocationSpecificationFromRestoreTask(req.Restore)
sender := s.newEventSender()
// Lock the deployment and stop the converge loop. The runner will
// handle unlocking the deployment and removing the converge disable
// file.
err = s.acquireLock(ctx)
if err != nil {
s.deployment.Unlock()
logrus.WithError(err).Error("Failed to acquire lock")
return nil, err
}
// This file should have already been written during the deployment-service
// restore, but if somebody were to trigger the server side restore without
// first restoring the deployment-service it's possible this file won't
// be there so we'll write it out.
f, err := os.OpenFile(api.ConvergeDisableFilePath, os.O_RDONLY|os.O_CREATE, 0700)
if err != nil {
return res, status.Error(codes.Internal, err.Error())
}
defer f.Close() // nolint errcheck
task, err := s.backupRunner.RestoreBackup(ctx, s.deployment, sender, bgwLocationSpec, remoteRestoreSpec, req.Restore)
if err != nil {
logrus.WithError(err).Error("Failed to restore backup")
return res, status.Error(codes.Internal, err.Error())
}
s.senderStore.Set(task.TaskID(), sender)
res.Restore = task
return res, nil
}
// BackupStatus shows the current status of the backup runner
func (s *server) BackupStatus(ctx context.Context, req *api.BackupStatusRequest) (*api.BackupStatusResponse, error) {
if !s.HasConfiguredDeployment() {
return nil, ErrorNotConfigured
}
task := s.backupRunner.RunningTask(ctx)
if task == nil {
logrus.Error("failed to get running backup task")
return nil, status.Error(codes.Internal, "failed to get running backup task")
}
return task.Status, nil
}
func (s *server) backupGatewayLocationSpec() (backup.LocationSpecification, error) {
var bucket string
var basePath string
switch s.deployment.Config.GetGlobal().GetV1().GetBackups().GetLocation().GetValue() {
case "s3":
bucket = s.deployment.Config.GetGlobal().GetV1().GetBackups().GetS3().GetBucket().GetName().GetValue()
basePath = s.deployment.Config.GetGlobal().GetV1().GetBackups().GetS3().GetBucket().GetBasePath().GetValue()
default:
bucket = "backups"
}
return backup.NewBackupGatewayLocationSpec(
s.deployment.BackupGatewayEndpoint(),
bucket,
basePath,
[]byte(s.deployment.CA().RootCert()),
s.secretStore,
)
}