Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cherry-pick] Fix --oplog and --db arguments error (#2173) #2182

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ""
}
Loading