From d4bb1352f572dbc1f046cb7713534bb261341c85 Mon Sep 17 00:00:00 2001 From: sayedppqq Date: Tue, 27 Aug 2024 10:58:21 +0600 Subject: [PATCH] fix Signed-off-by: sayedppqq --- pkg/backup.go | 10 ++++++++-- pkg/restore.go | 14 +++++++++++--- pkg/utils.go | 27 +++++++++++++++++++++++---- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/pkg/backup.go b/pkg/backup.go index 1ec7219fa..e5825e64f 100644 --- a/pkg/backup.go +++ b/pkg/backup.go @@ -277,7 +277,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic } // Checked for Altlas and DigitalOcean srv format connection string don't give port. - // mongodump --uri format not support port. + // mongodump not support both --uri and --port. if !isSrv { port, err = appBinding.Port() @@ -422,7 +422,13 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic fmt.Sprintf("--sslPEMKeyFile=%s", getOptionValue(dumpCreds, "--sslPEMKeyFile"))) } - userArgs := strings.Fields(opt.mongoArgs) + 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) + } + } if !isStandalone { // - port is already added in mongoDSN with replicasetName/host:port format. diff --git a/pkg/restore.go b/pkg/restore.go index a1f8d2841..046a844b9 100644 --- a/pkg/restore.go +++ b/pkg/restore.go @@ -334,6 +334,7 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti } uri := opt.buildMongoURI(mongoDSN, port, isStandalone, isSrv, tlsEnable) + // setup pipe command restoreCmd := restic.Command{ Name: MongoRestoreCMD, @@ -348,7 +349,14 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti fmt.Sprintf("--sslPEMKeyFile=%s", getOptionValue(dumpCreds, "--sslPEMKeyFile"))) } - userArgs := strings.Fields(opt.mongoArgs) + 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) + } + } + if !isStandalone { // - port is already added in mongoDSN with replicasetName/host:port format. // - oplog is enabled automatically for replicasets. @@ -392,11 +400,11 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti // ref: https://docs.mongodb.com/manual/tutorial/backup-sharded-cluster-with-database-dumps/ if parameters.ConfigServer != "" { - opt.dumpOptions = append(opt.dumpOptions, getDumpOpts(parameters.ConfigServer, MongoConfigSVRHostKey, false)) + opt.dumpOptions = append(opt.dumpOptions, getDumpOpts(extractHost(parameters.ConfigServer), MongoConfigSVRHostKey, false)) } for key, host := range parameters.ReplicaSets { - opt.dumpOptions = append(opt.dumpOptions, getDumpOpts(host, key, false)) + opt.dumpOptions = append(opt.dumpOptions, getDumpOpts(extractHost(host), key, false)) } // if parameters.ReplicaSets is nil, then perform normal backup with clientconfig.Service.Name mongo dsn diff --git a/pkg/utils.go b/pkg/utils.go index 6f057c3ec..031f92f62 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -136,16 +136,35 @@ func (opt *mongoOptions) buildMongoURI(mongoDSN string, port int32, isStandalone if !isStandalone { portStr = "" } + backupdb := "" // full + if strings.Contains(opt.mongoArgs, "--db") { + args := strings.Fields(opt.mongoArgs) + for _, arg := range args { + if strings.Contains(arg, "--db") { + backupdb = strings.Split(arg, "=")[1] + } + } + } authDbName := getOptionValue(dumpCreds, "--authenticationDatabase") userName := getOptionValue(dumpCreds, "--username") + if !tlsEnable { password := getOptionValue(dumpCreds, "--password") - return fmt.Sprintf("%s://%s:%s@%s%s/dbnew?authSource=%s", - prefix, userName, password, mongoDSN, portStr, authDbName) + return fmt.Sprintf("%s://%s:%s@%s%s/%s?authSource=%s", + prefix, userName, password, mongoDSN, portStr, backupdb, authDbName) } authMechanism := getOptionValue(dumpCreds, "--authenticationMechanism") - return fmt.Sprintf("%s://%s@%s%s/dbnew?authSource=%s&authMechanism=%s&ssl=true", - prefix, userName, mongoDSN, portStr, authDbName, authMechanism) + return fmt.Sprintf("%s://%s@%s%s/%s?authSource=%s&authMechanism=%s&ssl=true", + prefix, userName, mongoDSN, portStr, backupdb, authDbName, authMechanism) +} + +// 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 { + host = host[index+1:] + } + return host }