Skip to content

Commit

Permalink
Fix --oplog and --db arguments error (#2173) (#2181)
Browse files Browse the repository at this point in the history
/cherry-pick

Signed-off-by: sayedppqq <[email protected]>
  • Loading branch information
1gtm authored Sep 12, 2024
1 parent e872cca commit bfe4acb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
13 changes: 5 additions & 8 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
fmt.Sprintf("--sslPEMKeyFile=%s", getOptionValue(dumpCreds, "--sslPEMKeyFile")))
}

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

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

for _, arg := range userArgs {
backupCmd.Args = append(backupCmd.Args, arg)
// illegal argument combination: cannot specify --db and --uri
if !strings.Contains(arg, "--db") {
backupCmd.Args = append(backupCmd.Args, arg)
}
}

// append the backup command into the pipe
Expand Down
13 changes: 5 additions & 8 deletions pkg/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,7 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
fmt.Sprintf("--sslPEMKeyFile=%s", getOptionValue(dumpCreds, "--sslPEMKeyFile")))
}

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

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

for _, arg := range userArgs {
restoreCmd.Args = append(restoreCmd.Args, arg)
// illegal argument combination: cannot specify --db and --uri
if !strings.Contains(arg, "--db") {
restoreCmd.Args = append(restoreCmd.Args, arg)
}
}

// add the restore command to the pipeline
Expand Down
14 changes: 8 additions & 6 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,23 @@ func (opt *mongoOptions) buildMongoURI(mongoDSN string, port int32, isStandalone
// remove "shard0/" prefix from shard0/simple-shard0-0.simple-shard0-pods.demo.svc:27017,simple-shard0-1.simple-shard0-pods.demo.svc:27017
func extractHost(host string) string {
index := strings.Index(host, "/")
if index != -1 {
if index != -1 && index+1 < len(host) {
host = host[index+1:]
}
if index+1 >= len(host) {
host = ""
}
return host
}

func getBackupDB(mongoArgs string) string {
backupdb := "" // full
if strings.Contains(mongoArgs, "--db") {
if strings.Contains(mongoArgs, "--db=") {
args := strings.Fields(mongoArgs)
for _, arg := range args {
if strings.Contains(arg, "--db") {
backupdb = strings.Split(arg, "=")[1]
if strings.HasPrefix(arg, "--db=") {
return strings.TrimPrefix(arg, "--db=")
}
}
}
return backupdb
return ""
}

0 comments on commit bfe4acb

Please sign in to comment.