@@ -100,10 +100,10 @@ func NewCmdBackup() *cobra.Command {
100
100
RunE : func (cmd * cobra.Command , args []string ) error {
101
101
defer cleanup ()
102
102
103
- flags .EnsureRequiredFlags (cmd , "appbinding" , "provider" , "secret-dir " )
103
+ flags .EnsureRequiredFlags (cmd , "appbinding" , "provider" , "storage- secret-name" , "storage-secret-namespace " )
104
104
105
105
// catch sigkill signals and gracefully terminate so that cleanup functions are executed.
106
- sigChan := make (chan os.Signal )
106
+ sigChan := make (chan os.Signal , 1 )
107
107
signal .Notify (sigChan , os .Interrupt , syscall .SIGTERM )
108
108
go func () {
109
109
rcvSig := <- sigChan
@@ -179,11 +179,13 @@ func NewCmdBackup() *cobra.Command {
179
179
cmd .Flags ().StringVar (& opt .setupOptions .Endpoint , "endpoint" , opt .setupOptions .Endpoint , "Endpoint for s3/s3 compatible backend or REST server URL" )
180
180
cmd .Flags ().StringVar (& opt .setupOptions .Region , "region" , opt .setupOptions .Region , "Region for s3/s3 compatible backend" )
181
181
cmd .Flags ().StringVar (& opt .setupOptions .Path , "path" , opt .setupOptions .Path , "Directory inside the bucket where backup will be stored" )
182
- cmd .Flags ().StringVar (& opt .setupOptions .SecretDir , "secret-dir" , opt .setupOptions .SecretDir , "Directory where storage secret has been mounted" )
183
182
cmd .Flags ().StringVar (& opt .setupOptions .ScratchDir , "scratch-dir" , opt .setupOptions .ScratchDir , "Temporary directory" )
184
183
cmd .Flags ().BoolVar (& opt .setupOptions .EnableCache , "enable-cache" , opt .setupOptions .EnableCache , "Specify whether to enable caching for restic" )
185
184
cmd .Flags ().Int64Var (& opt .setupOptions .MaxConnections , "max-connections" , opt .setupOptions .MaxConnections , "Specify maximum concurrent connections for GCS, Azure and B2 backend" )
186
185
186
+ cmd .Flags ().StringVar (& opt .storageSecret .Name , "storage-secret-name" , opt .storageSecret .Name , "Name of the storage secret" )
187
+ cmd .Flags ().StringVar (& opt .storageSecret .Namespace , "storage-secret-namespace" , opt .storageSecret .Namespace , "Namespace of the storage secret" )
188
+ cmd .Flags ().StringVar (& opt .authenticationDatabase , "authentication-database" , "admin" , "Specify the authentication database" )
187
189
cmd .Flags ().StringVar (& opt .defaultBackupOptions .Host , "hostname" , opt .defaultBackupOptions .Host , "Name of the host machine" )
188
190
189
191
cmd .Flags ().Int64Var (& opt .defaultBackupOptions .RetentionPolicy .KeepLast , "retention-keep-last" , opt .defaultBackupOptions .RetentionPolicy .KeepLast , "Specify value for retention strategy" )
@@ -202,6 +204,12 @@ func NewCmdBackup() *cobra.Command {
202
204
}
203
205
204
206
func (opt * mongoOptions ) backupMongoDB (targetRef api_v1beta1.TargetRef ) (* restic.BackupOutput , error ) {
207
+ var err error
208
+ opt .setupOptions .StorageSecret , err = opt .kubeClient .CoreV1 ().Secrets (opt .storageSecret .Namespace ).Get (context .TODO (), opt .storageSecret .Name , metav1.GetOptions {})
209
+ if err != nil {
210
+ return nil , err
211
+ }
212
+
205
213
// if any pre-backup actions has been assigned to it, execute them
206
214
actionOptions := api_util.ActionOptions {
207
215
StashClient : opt .stashClient ,
@@ -210,7 +218,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
210
218
BackupSessionName : opt .backupSessionName ,
211
219
Namespace : opt .namespace ,
212
220
}
213
- err : = api_util .ExecutePreBackupActions (actionOptions )
221
+ err = api_util .ExecutePreBackupActions (actionOptions )
214
222
if err != nil {
215
223
return nil , err
216
224
}
@@ -229,25 +237,32 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
229
237
return nil , err
230
238
}
231
239
232
- // get app binding
233
240
appBinding , err := opt .catalogClient .AppcatalogV1alpha1 ().AppBindings (opt .namespace ).Get (context .TODO (), opt .appBindingName , metav1.GetOptions {})
234
241
if err != nil {
235
242
return nil , err
236
243
}
237
- // get secret
244
+
238
245
appBindingSecret , err := opt .kubeClient .CoreV1 ().Secrets (opt .namespace ).Get (context .TODO (), appBinding .Spec .Secret .Name , metav1.GetOptions {})
239
246
if err != nil {
240
247
return nil , err
241
248
}
242
249
243
- // transform secret
244
250
err = appBinding .TransformSecret (opt .kubeClient , appBindingSecret .Data )
245
251
if err != nil {
246
252
return nil , err
247
253
}
248
254
249
- // wait for DB ready
250
- waitForDBReady (appBinding .Spec .ClientConfig .Service .Name , appBinding .Spec .ClientConfig .Service .Port , opt .waitTimeout )
255
+ hostname , err := appBinding .Hostname ()
256
+ if err != nil {
257
+ return nil , err
258
+ }
259
+
260
+ port , err := appBinding .Port ()
261
+ if err != nil {
262
+ return nil , err
263
+ }
264
+
265
+ waitForDBReady (hostname , port , opt .waitTimeout )
251
266
252
267
// unmarshal parameter is the field has value
253
268
parameters := v1alpha1.MongoDBConfiguration {}
@@ -335,7 +350,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
335
350
adminCreds = []interface {}{
336
351
"--username" , string (appBindingSecret .Data [MongoUserKey ]),
337
352
"--password" , string (appBindingSecret .Data [MongoPasswordKey ]),
338
- "--authenticationDatabase" , "admin" ,
353
+ "--authenticationDatabase" , opt . authenticationDatabase ,
339
354
}
340
355
}
341
356
@@ -359,7 +374,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
359
374
userArgs := strings .Fields (opt .mongoArgs )
360
375
361
376
if isStandalone {
362
- backupCmd .Args = append (backupCmd .Args , "--port=" + fmt . Sprint ( appBinding . Spec . ClientConfig . Service . Port ))
377
+ backupCmd .Args = append (backupCmd .Args , fmt . Sprintf ( "--port=%d" , port ))
363
378
} else {
364
379
// - port is already added in mongoDSN with replicasetName/host:port format.
365
380
// - oplog is enabled automatically for replicasets.
@@ -404,17 +419,16 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
404
419
405
420
if parameters .ConfigServer != "" {
406
421
// sharded cluster. so disable the balancer first. then perform the 'usual' tasks.
407
-
408
422
primary , secondary , err := getPrimaryNSecondaryMember (parameters .ConfigServer )
409
423
if err != nil {
410
424
return nil , err
411
425
}
412
426
413
427
// connect to mongos to disable/enable balancer
414
- err = disabelBalancer (appBinding . Spec . ClientConfig . Service . Name )
428
+ err = disabelBalancer (hostname )
415
429
cleanupFuncs = append (cleanupFuncs , func () error {
416
430
// even if error occurs, try to enable the balancer on exiting the program.
417
- return enableBalancer (appBinding . Spec . ClientConfig . Service . Name )
431
+ return enableBalancer (hostname )
418
432
})
419
433
if err != nil {
420
434
return nil , err
@@ -428,14 +442,15 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
428
442
}
429
443
430
444
err = lockConfigServer (parameters .ConfigServer , secondary )
445
+
431
446
cleanupFuncs = append (cleanupFuncs , func () error {
432
447
// even if error occurs, try to unlock the server
433
448
return unlockSecondaryMember (secondary )
434
449
})
435
450
if err != nil {
451
+ klog .Errorf ("error while locking config server. error: %v" , err )
436
452
return nil , err
437
453
}
438
-
439
454
opt .backupOptions = append (opt .backupOptions , getBackupOpt (backupHost , MongoConfigSVRHostKey , false ))
440
455
}
441
456
@@ -470,7 +485,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
470
485
// if parameters.ReplicaSets is nil, then the mongodb database doesn't have replicasets or sharded replicasets.
471
486
// In this case, perform normal backup with clientconfig.Service.Name mongo dsn
472
487
if parameters .ReplicaSets == nil {
473
- opt .backupOptions = append (opt .backupOptions , getBackupOpt (appBinding . Spec . ClientConfig . Service . Name , restic .DefaultHost , true ))
488
+ opt .backupOptions = append (opt .backupOptions , getBackupOpt (hostname , restic .DefaultHost , true ))
474
489
}
475
490
476
491
klog .Infoln ("processing backup." )
@@ -482,7 +497,6 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
482
497
// hide password, don't print cmd
483
498
resticWrapper .HideCMD ()
484
499
485
- // Run backup
486
500
return resticWrapper .RunParallelBackup (opt .backupOptions , targetRef , opt .maxConcurrency )
487
501
}
488
502
@@ -604,12 +618,12 @@ func enableBalancer(mongosHost string) error {
604
618
605
619
func lockConfigServer (configSVRDSN , secondaryHost string ) error {
606
620
klog .Infoln ("Attempting to lock configserver" , configSVRDSN )
621
+
607
622
if secondaryHost == "" {
608
623
klog .Warningln ("locking configserver is skipped. secondary host is empty" )
609
624
return nil
610
625
}
611
626
v := make (map [string ]interface {})
612
-
613
627
// findAndModify BackupControlDocument. skip single quote inside single quote: https://stackoverflow.com/a/28786747/4628962
614
628
args := append ([]interface {}{
615
629
"config" ,
@@ -620,12 +634,10 @@ func lockConfigServer(configSVRDSN, secondaryHost string) error {
620
634
if err := sh .Command (MongoCMD , args ... ).Command ("tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
621
635
return err
622
636
}
623
-
624
637
val , ok := v ["counter" ].(float64 )
625
638
if ! ok || int (val ) == 0 {
626
639
return fmt .Errorf ("unable to modify BackupControlDocument. got response: %v" , v )
627
640
}
628
-
629
641
val2 := float64 (0 )
630
642
timer := 0 // wait approximately 5 minutes.
631
643
for timer < 60 && (int (val2 ) == 0 || int (val ) != int (val2 )) {
@@ -635,8 +647,9 @@ func lockConfigServer(configSVRDSN, secondaryHost string) error {
635
647
"config" ,
636
648
"--host" , secondaryHost ,
637
649
"--quiet" ,
638
- "--eval" , "rs.slaveOk (); db.BackupControl.find({ '_id' : 'BackupControlDocument' }).readConcern('majority');" ,
650
+ "--eval" , "rs.secondaryOk (); db.BackupControl.find({ '_id' : 'BackupControlDocument' }).readConcern('majority');" ,
639
651
}, adminCreds ... )
652
+
640
653
if err := sh .Command (MongoCMD , args ... ).UnmarshalJSON (& v ); err != nil {
641
654
return err
642
655
}
@@ -645,7 +658,6 @@ func lockConfigServer(configSVRDSN, secondaryHost string) error {
645
658
if ! ok {
646
659
return fmt .Errorf ("unable to get BackupControlDocument. got response: %v" , v )
647
660
}
648
-
649
661
if int (val ) != int (val2 ) {
650
662
klog .V (5 ).Infof ("BackupDocument counter in secondary is not same. Expected %v, but got %v. Full response: %v" , val , val2 , v )
651
663
time .Sleep (time .Second * 5 )
@@ -654,7 +666,6 @@ func lockConfigServer(configSVRDSN, secondaryHost string) error {
654
666
if timer >= 60 {
655
667
return fmt .Errorf ("timeout while waiting for BackupDocument counter in secondary to be same as primary. Expected %v, but got %v. Full response: %v" , val , val2 , v )
656
668
}
657
-
658
669
// lock secondary
659
670
return lockSecondaryMember (secondaryHost )
660
671
}
0 commit comments