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