Skip to content

Commit

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

Signed-off-by: sayedppqq <sayed@appscode.com>
  • Loading branch information
1gtm authored Sep 12, 2024
1 parent ffc259a commit bad8b82
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 @@ -443,13 +443,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 @@ -468,7 +462,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 @@ -355,13 +355,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 @@ -384,7 +378,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 bad8b82

Please sign in to comment.