Skip to content

Commit 55f8333

Browse files
authored
Fix --oplog and --db arguments error (#2173) (#2174)
/cherry-pick Signed-off-by: sayedppqq <sayed@appscode.com>
1 parent 855e9eb commit 55f8333

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

pkg/backup.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
422422
fmt.Sprintf("--sslPEMKeyFile=%s", getOptionValue(dumpCreds, "--sslPEMKeyFile")))
423423
}
424424

425-
var userArgs []string
426-
for _, arg := range strings.Fields(opt.mongoArgs) {
427-
// illegal argument combination: cannot specify --db and --uri
428-
if !strings.Contains(arg, "--db") {
429-
userArgs = append(userArgs, arg)
430-
}
431-
}
425+
userArgs := strings.Fields(opt.mongoArgs)
432426

433427
if !isStandalone {
434428
// - port is already added in mongoDSN with replicasetName/host:port format.
@@ -447,7 +441,10 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
447441
}
448442

449443
for _, arg := range userArgs {
450-
backupCmd.Args = append(backupCmd.Args, arg)
444+
// illegal argument combination: cannot specify --db and --uri
445+
if !strings.Contains(arg, "--db") {
446+
backupCmd.Args = append(backupCmd.Args, arg)
447+
}
451448
}
452449

453450
// append the backup command into the pipe

pkg/restore.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,7 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
349349
fmt.Sprintf("--sslPEMKeyFile=%s", getOptionValue(dumpCreds, "--sslPEMKeyFile")))
350350
}
351351

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-
}
358-
}
352+
userArgs := strings.Fields(opt.mongoArgs)
359353

360354
if !isStandalone {
361355
// - port is already added in mongoDSN with replicasetName/host:port format.
@@ -378,7 +372,10 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
378372
}
379373

380374
for _, arg := range userArgs {
381-
restoreCmd.Args = append(restoreCmd.Args, arg)
375+
// illegal argument combination: cannot specify --db and --uri
376+
if !strings.Contains(arg, "--db") {
377+
restoreCmd.Args = append(restoreCmd.Args, arg)
378+
}
382379
}
383380

384381
// add the restore command to the pipeline

pkg/utils.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,23 @@ func (opt *mongoOptions) buildMongoURI(mongoDSN string, port int32, isStandalone
159159
// remove "shard0/" prefix from shard0/simple-shard0-0.simple-shard0-pods.demo.svc:27017,simple-shard0-1.simple-shard0-pods.demo.svc:27017
160160
func extractHost(host string) string {
161161
index := strings.Index(host, "/")
162-
if index != -1 {
162+
if index != -1 && index+1 < len(host) {
163163
host = host[index+1:]
164164
}
165+
if index+1 >= len(host) {
166+
host = ""
167+
}
165168
return host
166169
}
167170

168171
func getBackupDB(mongoArgs string) string {
169-
backupdb := "" // full
170-
if strings.Contains(mongoArgs, "--db") {
172+
if strings.Contains(mongoArgs, "--db=") {
171173
args := strings.Fields(mongoArgs)
172174
for _, arg := range args {
173-
if strings.Contains(arg, "--db") {
174-
backupdb = strings.Split(arg, "=")[1]
175+
if strings.HasPrefix(arg, "--db=") {
176+
return strings.TrimPrefix(arg, "--db=")
175177
}
176178
}
177179
}
178-
return backupdb
180+
return ""
179181
}

0 commit comments

Comments
 (0)