From 3c4d0f4c7cbf75e19417a58d7b5dca2c91662742 Mon Sep 17 00:00:00 2001 From: lgtm <1gtm@users.noreply.github.com> Date: Thu, 12 Sep 2024 00:44:27 -0700 Subject: [PATCH] Fix --oplog and --db arguments error (#2173) (#2182) /cherry-pick Signed-off-by: sayedppqq --- pkg/backup.go | 13 +++++-------- pkg/restore.go | 13 +++++-------- pkg/utils.go | 14 ++++++++------ 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/pkg/backup.go b/pkg/backup.go index e5825e64f..42c8bf437 100644 --- a/pkg/backup.go +++ b/pkg/backup.go @@ -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. @@ -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 diff --git a/pkg/restore.go b/pkg/restore.go index 046a844b9..84ba0a98e 100644 --- a/pkg/restore.go +++ b/pkg/restore.go @@ -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. @@ -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 diff --git a/pkg/utils.go b/pkg/utils.go index 0f6874f0a..b7c070cc6 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -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 "" }