@@ -204,9 +204,23 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
204
204
return nil , err
205
205
}
206
206
207
- port , err := appBinding .Port ()
208
- if err != nil {
209
- return nil , err
207
+ var isSrv bool
208
+ port := int32 (27017 )
209
+ if appBinding .Spec .ClientConfig .URL != nil {
210
+ isSrv , err = isSrvConnection (* appBinding .Spec .ClientConfig .URL )
211
+ if err != nil {
212
+ return nil , err
213
+ }
214
+ }
215
+
216
+ // Checked for Altlas and DigitalOcean srv format connection string don't give port.
217
+ // mongodump --uri format not support port.
218
+
219
+ if ! isSrv {
220
+ port , err = appBinding .Port ()
221
+ if err != nil {
222
+ return nil , err
223
+ }
210
224
}
211
225
212
226
// unmarshal parameter is the field has value
@@ -249,8 +263,12 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
249
263
return nil , err
250
264
}
251
265
}
252
-
266
+ var tlsEnable bool
253
267
if appBinding .Spec .ClientConfig .CABundle != nil {
268
+ tlsEnable = true
269
+ }
270
+
271
+ if tlsEnable {
254
272
if err := os .WriteFile (filepath .Join (opt .setupOptions .ScratchDir , MongoTLSCertFileName ), appBinding .Spec .ClientConfig .CABundle , os .ModePerm ); err != nil {
255
273
return nil , errors .Wrap (err , "failed to write key for CA certificate" )
256
274
}
@@ -261,8 +279,8 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
261
279
}
262
280
dumpCreds = []interface {}{
263
281
"--ssl" ,
264
- "--sslCAFile" , filepath .Join (opt .setupOptions .ScratchDir , MongoTLSCertFileName ),
265
- "--sslPEMKeyFile" , filepath .Join (opt .setupOptions .ScratchDir , MongoClientPemFileName ),
282
+ fmt . Sprintf ( "--sslCAFile=%s " , filepath .Join (opt .setupOptions .ScratchDir , MongoTLSCertFileName ) ),
283
+ fmt . Sprintf ( "--sslPEMKeyFile=%s " , filepath .Join (opt .setupOptions .ScratchDir , MongoClientPemFileName ) ),
266
284
}
267
285
268
286
// get certificate secret to get client certificate
@@ -289,9 +307,9 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
289
307
return nil , errors .Wrap (err , "unable to get user from ssl." )
290
308
}
291
309
userAuth := []interface {}{
292
- "-u " , user ,
293
- "--authenticationMechanism" , " MONGODB-X509" ,
294
- "--authenticationDatabase" , " $external" ,
310
+ fmt . Sprintf ( "--username=%s " , user ) ,
311
+ "--authenticationMechanism= MONGODB-X509" ,
312
+ "--authenticationDatabase= $external" ,
295
313
}
296
314
mongoCreds = append (mongoCreds , userAuth ... )
297
315
dumpCreds = append (dumpCreds , userAuth ... )
@@ -300,7 +318,7 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
300
318
userAuth := []interface {}{
301
319
fmt .Sprintf ("--username=%s" , authSecret .Data [MongoUserKey ]),
302
320
fmt .Sprintf ("--password=%s" , authSecret .Data [MongoPasswordKey ]),
303
- "--authenticationDatabase" , opt .authenticationDatabase ,
321
+ fmt . Sprintf ( "--authenticationDatabase=%s " , opt .authenticationDatabase ) ,
304
322
}
305
323
mongoCreds = append (mongoCreds , userAuth ... )
306
324
dumpCreds = append (dumpCreds , userAuth ... )
@@ -314,19 +332,32 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
314
332
FileName : opt .defaultDumpOptions .FileName ,
315
333
Snapshot : opt .getSnapshotForHost (hostKey , restoreSession .Spec .Target .Rules ),
316
334
}
335
+
336
+ uri := opt .buildMongoURI (mongoDSN , port , isStandalone , isSrv , tlsEnable )
337
+
317
338
// setup pipe command
318
339
restoreCmd := restic.Command {
319
340
Name : MongoRestoreCMD ,
320
- Args : append ( []interface {}{
321
- "--host " , mongoDSN ,
341
+ Args : []interface {}{
342
+ "--uri " , fmt . Sprintf ( " \" %s \" " , uri ) ,
322
343
"--archive" ,
323
- }, dumpCreds ... ),
344
+ },
345
+ }
346
+ if tlsEnable {
347
+ restoreCmd .Args = append (restoreCmd .Args ,
348
+ fmt .Sprintf ("--sslCAFile=%s" , getOptionValue (dumpCreds , "--sslCAFile" )),
349
+ fmt .Sprintf ("--sslPEMKeyFile=%s" , getOptionValue (dumpCreds , "--sslPEMKeyFile" )))
350
+ }
351
+
352
+ var userArgs []string
353
+ for _ , arg := range strings .Fields (opt .mongoArgs ) {
354
+ // illegal argument combination: cannot specify --db and --uri
355
+ if ! strings .Contains (arg , "--db" ) {
356
+ userArgs = append (userArgs , arg )
357
+ }
324
358
}
325
359
326
- userArgs := strings .Fields (opt .mongoArgs )
327
- if isStandalone {
328
- restoreCmd .Args = append (restoreCmd .Args , fmt .Sprintf ("--port=%d" , port ))
329
- } else {
360
+ if ! isStandalone {
330
361
// - port is already added in mongoDSN with replicasetName/host:port format.
331
362
// - oplog is enabled automatically for replicasets.
332
363
// Don't use --oplogReplay if user specify any of these arguments through opt.mongoArgs
@@ -369,11 +400,11 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
369
400
// ref: https://docs.mongodb.com/manual/tutorial/backup-sharded-cluster-with-database-dumps/
370
401
371
402
if parameters .ConfigServer != "" {
372
- opt .dumpOptions = append (opt .dumpOptions , getDumpOpts (parameters .ConfigServer , MongoConfigSVRHostKey , false ))
403
+ opt .dumpOptions = append (opt .dumpOptions , getDumpOpts (extractHost ( parameters .ConfigServer ) , MongoConfigSVRHostKey , false ))
373
404
}
374
405
375
406
for key , host := range parameters .ReplicaSets {
376
- opt .dumpOptions = append (opt .dumpOptions , getDumpOpts (host , key , false ))
407
+ opt .dumpOptions = append (opt .dumpOptions , getDumpOpts (extractHost ( host ) , key , false ))
377
408
}
378
409
379
410
// if parameters.ReplicaSets is nil, then perform normal backup with clientconfig.Service.Name mongo dsn
0 commit comments