@@ -84,7 +84,6 @@ func NewCmdBackup() *cobra.Command {
84
84
masterURL string
85
85
kubeconfigPath string
86
86
opt = mongoOptions {
87
- totalHosts : 1 ,
88
87
waitTimeout : 300 ,
89
88
setupOptions : restic.SetupOptions {
90
89
ScratchDir : restic .DefaultScratchDir ,
@@ -150,8 +149,14 @@ func NewCmdBackup() *cobra.Command {
150
149
if err != nil {
151
150
backupOutput = & restic.BackupOutput {
152
151
BackupTargetStatus : api_v1beta1.BackupTargetStatus {
153
- Ref : targetRef ,
154
- Stats : opt .getHostBackupStats (err ),
152
+ Ref : targetRef ,
153
+ Stats : []api_v1beta1.HostBackupStats {
154
+ {
155
+ Hostname : opt .defaultBackupOptions .Host ,
156
+ Phase : api_v1beta1 .HostBackupFailed ,
157
+ Error : err .Error (),
158
+ },
159
+ },
155
160
},
156
161
}
157
162
}
@@ -295,9 +300,9 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
295
300
// 3. For sharded MongoDB, totalHosts=(number of shard + 1) // extra 1 for config server
296
301
// So, for stand-alone MongoDB and MongoDB ReplicaSet, we don't have to do anything.
297
302
// We only need to update totalHosts field for sharded MongoDB
303
+
298
304
// For sharded MongoDB, parameter.ConfigServer will not be empty
299
305
if parameters .ConfigServer != "" {
300
- opt .totalHosts = len (parameters .ReplicaSets ) + 1 // for each shard there will be one key in parameters.ReplicaSet
301
306
backupSession , err := opt .stashClient .StashV1beta1 ().BackupSessions (opt .namespace ).Get (context .TODO (), opt .backupSessionName , metav1.GetOptions {})
302
307
if err != nil {
303
308
return nil , err
@@ -309,7 +314,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
309
314
opt .stashClient .StashV1beta1 (),
310
315
backupSession .ObjectMeta ,
311
316
func (status * api_v1beta1.BackupSessionStatus ) (types.UID , * api_v1beta1.BackupSessionStatus ) {
312
- status .Targets [i ].TotalHosts = pointer .Int32P (int32 (opt . totalHosts ))
317
+ status .Targets [i ].TotalHosts = pointer .Int32P (int32 (len ( parameters . ReplicaSets ) + 1 )) // for each shard there will be one key in parameters.ReplicaSet
313
318
return backupSession .UID , status
314
319
},
315
320
metav1.UpdateOptions {},
@@ -371,7 +376,15 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
371
376
"--authenticationDatabase" , "$external" ,
372
377
}
373
378
mongoCreds = append (mongoCreds , userAuth ... )
374
- dumpCreds = append (dumpCreds , userAuth ... )
379
+ username := StashUserName
380
+ if parameters .ConfigServer == "" {
381
+ username = string (authSecret .Data [MongoUserKey ])
382
+ }
383
+ dumpCreds = append (dumpCreds , []interface {}{
384
+ fmt .Sprintf ("--username=%s" , username ),
385
+ fmt .Sprintf ("--password=%s" , authSecret .Data [MongoPasswordKey ]),
386
+ "--authenticationDatabase" , opt .authenticationDatabase ,
387
+ }... )
375
388
376
389
} else {
377
390
userAuth := []interface {}{
@@ -380,7 +393,15 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
380
393
"--authenticationDatabase" , opt .authenticationDatabase ,
381
394
}
382
395
mongoCreds = append (mongoCreds , userAuth ... )
383
- dumpCreds = append (dumpCreds , userAuth ... )
396
+ username := StashUserName
397
+ if parameters .ConfigServer == "" {
398
+ username = string (authSecret .Data [MongoUserKey ])
399
+ }
400
+ dumpCreds = append (dumpCreds , []interface {}{
401
+ fmt .Sprintf ("--username=%s" , username ),
402
+ fmt .Sprintf ("--password=%s" , authSecret .Data [MongoPasswordKey ]),
403
+ "--authenticationDatabase" , opt .authenticationDatabase ,
404
+ }... )
384
405
}
385
406
386
407
getBackupOpt := func (mongoDSN , hostKey string , isStandalone bool ) restic.BackupOptions {
@@ -568,12 +589,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
568
589
// hide password, don't print cmd
569
590
resticWrapper .HideCMD ()
570
591
571
- out , err := resticWrapper .RunParallelBackup (opt .backupOptions , targetRef , opt .maxConcurrency )
572
- if err != nil {
573
- klog .Warningln ("backup failed!" , err .Error ())
574
- }
575
- // error not returned, error is encoded into output
576
- return out , nil
592
+ return resticWrapper .RunParallelBackup (opt .backupOptions , targetRef , opt .maxConcurrency )
577
593
}
578
594
579
595
// cleanup usually unlocks the locked servers
@@ -585,32 +601,6 @@ func cleanup() {
585
601
}
586
602
}
587
603
588
- func (opt * mongoOptions ) getHostBackupStats (err error ) []api_v1beta1.HostBackupStats {
589
- var backupStats []api_v1beta1.HostBackupStats
590
-
591
- errMsg := fmt .Sprintf ("failed to start backup: %s" , err .Error ())
592
- for _ , backupOpt := range opt .backupOptions {
593
- backupStats = append (backupStats , api_v1beta1.HostBackupStats {
594
- Hostname : backupOpt .Host ,
595
- Phase : api_v1beta1 .HostBackupFailed ,
596
- Error : errMsg ,
597
- })
598
- }
599
-
600
- if opt .totalHosts > len (backupStats ) {
601
- rem := opt .totalHosts - len (backupStats )
602
- for i := 0 ; i < rem ; i ++ {
603
- backupStats = append (backupStats , api_v1beta1.HostBackupStats {
604
- Hostname : fmt .Sprintf ("unknown-%s" , strconv .Itoa (i )),
605
- Phase : api_v1beta1 .HostBackupFailed ,
606
- Error : errMsg ,
607
- })
608
- }
609
- }
610
-
611
- return backupStats
612
- }
613
-
614
604
func getSSLUser (path string ) (string , error ) {
615
605
data , err := sh .Command (OpenSSLCMD , "x509" , "-in" , path , "-inform" , "PEM" , "-subject" , "-nameopt" , "RFC2253" , "-noout" ).Output ()
616
606
if err != nil {
@@ -687,7 +677,7 @@ func disabelBalancer(mongosHost string) error {
687
677
"config" ,
688
678
"--host" , mongosHost ,
689
679
"--quiet" ,
690
- "--eval" , "while(sh.isBalancerRunning()){ print('waiting for balancer to stop...'); sleep(1000);}" ,
680
+ "--eval" , "while(sh.isBalancerRunning().mode != 'off' ){ print('waiting for balancer to stop...'); sleep(1000);}" ,
691
681
}, mongoCreds ... )
692
682
if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).Run (); err != nil {
693
683
return err
@@ -731,7 +721,7 @@ func lockConfigServer(configSVRDSN, secondaryHost string) error {
731
721
"config" ,
732
722
"--host" , configSVRDSN ,
733
723
"--quiet" ,
734
- "--eval" , "db.BackupControl.findAndModify({query: { _id: 'BackupControlDocument' }, update: { $inc: { counter : 1 } }, new: true, upsert: true, writeConcern: { w: 'majority', wtimeout: 15000 }});" ,
724
+ "--eval" , "JSON.stringify( db.BackupControl.findAndModify({query: { _id: 'BackupControlDocument' }, update: { $inc: { counter : 1 } }, new: true, upsert: true, writeConcern: { w: 'majority', wtimeout: 15000 }}) );" ,
735
725
}, mongoCreds ... )
736
726
if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
737
727
return err
@@ -750,7 +740,7 @@ func lockConfigServer(configSVRDSN, secondaryHost string) error {
750
740
"config" ,
751
741
"--host" , secondaryHost ,
752
742
"--quiet" ,
753
- "--eval" , "rs.secondaryOk(); db.BackupControl.find({ '_id' : 'BackupControlDocument' }).readConcern('majority');" ,
743
+ "--eval" , "rs.secondaryOk(); JSON.stringify( db.BackupControl.find({ '_id' : 'BackupControlDocument' }).readConcern('majority').toArray() );" ,
754
744
}, mongoCreds ... )
755
745
756
746
if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v2 ); err != nil {
0 commit comments