Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: sayedppqq <[email protected]>
  • Loading branch information
sayedppqq committed Aug 27, 2024
1 parent 59e7950 commit d4bb135
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
10 changes: 8 additions & 2 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 11 additions & 3 deletions pkg/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
27 changes: 23 additions & 4 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit d4bb135

Please sign in to comment.