-
Notifications
You must be signed in to change notification settings - Fork 4
/
restore.go
345 lines (304 loc) · 13.3 KB
/
restore.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
Copyright The Stash Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pkg
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
api_v1beta1 "stash.appscode.dev/apimachinery/apis/stash/v1beta1"
stash_cs "stash.appscode.dev/apimachinery/client/clientset/versioned"
stash_cs_util "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1/util"
"stash.appscode.dev/apimachinery/pkg/restic"
"github.com/appscode/go/flags"
"github.com/appscode/go/log"
"github.com/appscode/go/types"
"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
v1 "kmodules.xyz/offshoot-api/api/v1"
"kubedb.dev/apimachinery/apis/config/v1alpha1"
)
func NewCmdRestore() *cobra.Command {
var (
masterURL string
kubeconfigPath string
opt = mongoOptions{
waitTimeout: 300,
setupOptions: restic.SetupOptions{
ScratchDir: restic.DefaultScratchDir,
EnableCache: false,
},
defaultDumpOptions: restic.DumpOptions{
Host: restic.DefaultHost,
FileName: MongoDumpFile,
},
}
)
cmd := &cobra.Command{
Use: "restore-mongo",
Short: "Restores MongoDB Backup",
DisableAutoGenTag: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
return checkCommandExists()
},
RunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "appbinding", "provider", "secret-dir")
// prepare client
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
if err != nil {
return err
}
opt.stashClient, err = stash_cs.NewForConfig(config)
if err != nil {
return err
}
opt.kubeClient, err = kubernetes.NewForConfig(config)
if err != nil {
return err
}
opt.catalogClient, err = appcatalog_cs.NewForConfig(config)
if err != nil {
return err
}
var restoreOutput *restic.RestoreOutput
restoreOutput, err = opt.restoreMongoDB()
if err != nil {
restoreOutput = &restic.RestoreOutput{
HostRestoreStats: []api_v1beta1.HostRestoreStats{
{
Hostname: opt.defaultDumpOptions.Host,
Phase: api_v1beta1.HostRestoreFailed,
Error: err.Error(),
},
},
}
}
// If output directory specified, then write the output in "output.json" file in the specified directory
if opt.outputDir != "" {
return restoreOutput.WriteOutput(filepath.Join(opt.outputDir, restic.DefaultOutputFileName))
}
return nil
},
}
cmd.Flags().StringVar(&opt.mongoArgs, "mongo-args", opt.mongoArgs, "Additional arguments")
cmd.Flags().Int32Var(&opt.waitTimeout, "wait-timeout", opt.waitTimeout, "Number of seconds to wait for the database to be ready")
cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
cmd.Flags().StringVar(&opt.namespace, "namespace", "default", "Namespace of Backup/Restore Session")
cmd.Flags().StringVar(&opt.appBindingName, "appbinding", opt.appBindingName, "Name of the app binding")
cmd.Flags().StringVar(&opt.restoreSessionName, "restoresession", opt.restoreSessionName, "Name of the respective RestoreSession object")
cmd.Flags().IntVar(&opt.maxConcurrency, "max-concurrency", 3, "maximum concurrent backup process to run to take backup from each replicasets")
cmd.Flags().StringVar(&opt.setupOptions.Provider, "provider", opt.setupOptions.Provider, "Backend provider (i.e. gcs, s3, azure etc)")
cmd.Flags().StringVar(&opt.setupOptions.Bucket, "bucket", opt.setupOptions.Bucket, "Name of the cloud bucket/container (keep empty for local backend)")
cmd.Flags().StringVar(&opt.setupOptions.Endpoint, "endpoint", opt.setupOptions.Endpoint, "Endpoint for s3/s3 compatible backend or REST server URL")
cmd.Flags().StringVar(&opt.setupOptions.Region, "region", opt.setupOptions.Region, "Region for s3/s3 compatible backend")
cmd.Flags().StringVar(&opt.setupOptions.Path, "path", opt.setupOptions.Path, "Directory inside the bucket where backup will be stored")
cmd.Flags().StringVar(&opt.setupOptions.SecretDir, "secret-dir", opt.setupOptions.SecretDir, "Directory where storage secret has been mounted")
cmd.Flags().StringVar(&opt.setupOptions.ScratchDir, "scratch-dir", opt.setupOptions.ScratchDir, "Temporary directory")
cmd.Flags().BoolVar(&opt.setupOptions.EnableCache, "enable-cache", opt.setupOptions.EnableCache, "Specify whether to enable caching for restic")
cmd.Flags().Int64Var(&opt.setupOptions.MaxConnections, "max-connections", opt.setupOptions.MaxConnections, "Specify maximum concurrent connections for GCS, Azure and B2 backend")
cmd.Flags().StringVar(&opt.defaultDumpOptions.Host, "hostname", opt.defaultDumpOptions.Host, "Name of the host machine")
cmd.Flags().StringVar(&opt.defaultDumpOptions.SourceHost, "source-hostname", opt.defaultDumpOptions.SourceHost, "Name of the host whose data will be restored")
cmd.Flags().StringVar(&opt.defaultDumpOptions.Snapshot, "snapshot", opt.defaultDumpOptions.Snapshot, "Snapshot to dump")
cmd.Flags().StringVar(&opt.outputDir, "output-dir", opt.outputDir, "Directory where output.json file will be written (keep empty if you don't need to write output in file)")
return cmd
}
func (opt *mongoOptions) restoreMongoDB() (*restic.RestoreOutput, error) {
// apply nice, ionice settings from env
var err error
opt.setupOptions.Nice, err = v1.NiceSettingsFromEnv()
if err != nil {
return nil, err
}
opt.setupOptions.IONice, err = v1.IONiceSettingsFromEnv()
if err != nil {
return nil, err
}
// get app binding
appBinding, err := opt.catalogClient.AppcatalogV1alpha1().AppBindings(opt.namespace).Get(context.TODO(), opt.appBindingName, metav1.GetOptions{})
if err != nil {
return nil, err
}
// get secret
appBindingSecret, err := opt.kubeClient.CoreV1().Secrets(opt.namespace).Get(context.TODO(), appBinding.Spec.Secret.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
// transform secret
err = appBinding.TransformSecret(opt.kubeClient, appBindingSecret.Data)
if err != nil {
return nil, err
}
// unmarshal parameter is the field has value
parameters := v1alpha1.MongoDBConfiguration{}
if appBinding.Spec.Parameters != nil {
if err = json.Unmarshal(appBinding.Spec.Parameters.Raw, ¶meters); err != nil {
log.Errorf("unable to unmarshal appBinding.Spec.Parameters.Raw. Reason: %v", err)
}
}
// Stash operator does not know how many hosts this plugin will restore. It sets totalHosts field of respective RestoreSession to 1.
// We must update the totalHosts field to the actual number of hosts it will restore.
// Otherwise, RestoreSession will stuck in "Running" state.
// Total hosts for MongoDB:
// 1. For stand-alone MongoDB, totalHosts=1.
// 2. For MongoDB ReplicaSet, totalHosts=1.
// 3. For sharded MongoDB, totalHosts=(number of shard + 1) // extra 1 for config server
// So, for stand-alone MongoDB and MongoDB ReplicaSet, we don't have to do anything.
// We only need to update totalHosts field for sharded MongoDB
// For sharded MongoDB, parameter.ConfigServer will not be empty
if parameters.ConfigServer != "" {
restoreSession, err := opt.stashClient.StashV1beta1().RestoreSessions(opt.namespace).Get(context.TODO(), opt.restoreSessionName, metav1.GetOptions{})
if err != nil {
return nil, err
}
_, err = stash_cs_util.UpdateRestoreSessionStatus(
context.TODO(),
opt.stashClient.StashV1beta1(),
restoreSession.ObjectMeta,
func(status *api_v1beta1.RestoreSessionStatus) *api_v1beta1.RestoreSessionStatus {
status.TotalHosts = types.Int32P(int32(len(parameters.ReplicaSets) + 1)) // for each shard there will be one key in parameters.ReplicaSet
return status
},
metav1.UpdateOptions{},
)
if err != nil {
return nil, err
}
}
if appBinding.Spec.ClientConfig.CABundle != nil {
if err := ioutil.WriteFile(filepath.Join(opt.setupOptions.ScratchDir, MongoTLSCertFileName), appBinding.Spec.ClientConfig.CABundle, os.ModePerm); err != nil {
return nil, errors.Wrap(err, "failed to write key for CA certificate")
}
mongoCreds = []interface{}{
"--tls",
"--tlsCAFile", filepath.Join(opt.setupOptions.ScratchDir, MongoTLSCertFileName),
"--tlsCertificateKeyFile", filepath.Join(opt.setupOptions.ScratchDir, MongoClientPemFileName),
}
dumpCreds = []interface{}{
"--ssl",
"--sslCAFile", filepath.Join(opt.setupOptions.ScratchDir, MongoTLSCertFileName),
"--sslPEMKeyFile", filepath.Join(opt.setupOptions.ScratchDir, MongoClientPemFileName),
}
// get certificate secret to get client certificate
data, ok := appBindingSecret.Data[MongoClientPemFileName]
if !ok {
return nil, errors.Wrap(err, "unable to get client certificate from secret.")
}
if err := ioutil.WriteFile(filepath.Join(opt.setupOptions.ScratchDir, MongoClientPemFileName), data, os.ModePerm); err != nil {
return nil, errors.Wrap(err, "failed to write client certificate")
}
user, err := getSSLUser(filepath.Join(opt.setupOptions.ScratchDir, MongoClientPemFileName))
if err != nil {
return nil, errors.Wrap(err, "unable to get user from ssl.")
}
userAuth := []interface{}{
"-u", user,
"--authenticationMechanism", "MONGODB-X509",
"--authenticationDatabase", "$external",
}
mongoCreds = append(mongoCreds, userAuth...)
dumpCreds = append(dumpCreds, userAuth...)
} else {
userAuth := []interface{}{
"--username", string(appBindingSecret.Data[MongoUserKey]),
"--password", string(appBindingSecret.Data[MongoPasswordKey]),
"--authenticationDatabase", "admin",
}
mongoCreds = append(mongoCreds, userAuth...)
dumpCreds = append(dumpCreds, userAuth...)
}
getDumpOpts := func(mongoDSN, hostKey string, isStandalone bool) restic.DumpOptions {
log.Infoln("processing backupOptions for ", mongoDSN)
dumpOpt := restic.DumpOptions{
Host: hostKey,
SourceHost: hostKey,
FileName: opt.defaultDumpOptions.FileName,
Snapshot: opt.defaultDumpOptions.Snapshot,
}
// setup pipe command
dumpOpt.StdoutPipeCommand = restic.Command{
Name: MongoRestoreCMD,
Args: append([]interface{}{
"--host", mongoDSN,
"--archive",
}, dumpCreds...),
}
userArgs := strings.Fields(opt.mongoArgs)
if isStandalone {
dumpOpt.StdoutPipeCommand.Args = append(dumpOpt.StdoutPipeCommand.Args, "--port="+fmt.Sprint(appBinding.Spec.ClientConfig.Service.Port))
} else {
// - port is already added in mongoDSN with replicasetName/host:port format.
// - oplog is enabled automatically for replicasets.
// Don't use --oplogReplay if user specify any of these arguments through opt.mongoArgs
// 1. --db
// 2. --collection
// 3. --nsInclude
// 4. --nsExclude
// xref: https://docs.mongodb.com/manual/reference/program/mongorestore/#cmdoption-mongorestore-oplogreplay
forbiddenArgs := sets.NewString(
"-d", "--db",
"-c", "--collection",
"--nsInclude",
"--nsExclude",
)
if !containsArg(userArgs, forbiddenArgs) {
dumpOpt.StdoutPipeCommand.Args = append(dumpOpt.StdoutPipeCommand.Args, "--oplogReplay")
}
}
for _, arg := range userArgs {
dumpOpt.StdoutPipeCommand.Args = append(dumpOpt.StdoutPipeCommand.Args, arg)
}
return dumpOpt
}
// set opt.maxConcurrency
if len(parameters.ReplicaSets) <= 1 {
opt.maxConcurrency = 1
}
// If parameters.ReplicaSets is not empty, then replicaset hosts are given in key:value pair,
// where, keys are host-0,host-1 etc and values are the replicaset dsn of one replicaset component
//
// Procedure of restore in a sharded or replicaset cluster
// - Restore the CSRS primary mongod data files.
// - Restore Each Shard Replica Set
// ref: https://docs.mongodb.com/manual/tutorial/backup-sharded-cluster-with-database-dumps/
if parameters.ConfigServer != "" {
opt.dumpOptions = append(opt.dumpOptions, getDumpOpts(parameters.ConfigServer, MongoConfigSVRHostKey, false))
}
for key, host := range parameters.ReplicaSets {
opt.dumpOptions = append(opt.dumpOptions, getDumpOpts(host, key, false))
}
// if parameters.ReplicaSets is nil, then perform normal backup with clientconfig.Service.Name mongo dsn
if parameters.ReplicaSets == nil {
opt.dumpOptions = append(opt.dumpOptions, getDumpOpts(appBinding.Spec.ClientConfig.Service.Name, restic.DefaultHost, true))
}
log.Infoln("processing restore.")
// wait for DB ready
waitForDBReady(appBinding.Spec.ClientConfig.Service.Name, appBinding.Spec.ClientConfig.Service.Port, opt.waitTimeout)
// init restic wrapper
resticWrapper, err := restic.NewResticWrapper(opt.setupOptions)
if err != nil {
return nil, err
}
// hide password, don't print cmd
resticWrapper.HideCMD()
// Run dump
return resticWrapper.ParallelDump(opt.dumpOptions, opt.maxConcurrency)
}