Skip to content

Commit

Permalink
Add support for restoring specific snapshot (#1927)
Browse files Browse the repository at this point in the history
/cherry-pick

Signed-off-by: hmsayem <[email protected]>
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
hmsayem authored and tamalsaha committed Oct 4, 2023
1 parent 147be12 commit 7731cd5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
27 changes: 21 additions & 6 deletions pkg/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,14 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
// So, for stand-alone MongoDB and MongoDB ReplicaSet, we don't have to do anything.
// We only need to update totalHosts field for sharded MongoDB

restoreSession, err := opt.stashClient.StashV1beta1().RestoreSessions(opt.namespace).Get(context.TODO(), opt.restoreSessionName, metav1.GetOptions{})
if err != nil {
return nil, err
}
opt.totalHosts = 1
// For sharded MongoDB, parameter.ConfigServer will not be empty
if parameters.ConfigServer != "" {
opt.totalHosts = len(parameters.ReplicaSets) + 1 // for each shard there will be one key in parameters.ReplicaSet
restoreSession, err := opt.stashClient.StashV1beta1().RestoreSessions(opt.namespace).Get(context.TODO(), opt.restoreSessionName, metav1.GetOptions{})
if err != nil {
return nil, err
}
_, err = stash_cs_util.UpdateRestoreSessionStatus(
context.TODO(),
opt.stashClient.StashV1beta1(),
Expand Down Expand Up @@ -326,9 +326,8 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
Host: hostKey,
SourceHost: hostKey,
FileName: opt.defaultDumpOptions.FileName,
Snapshot: opt.defaultDumpOptions.Snapshot,
Snapshot: opt.getSnapshotForHost(hostKey, restoreSession.Spec.Target.Rules),
}

// setup pipe command
restoreCmd := restic.Command{
Name: MongoRestoreCMD,
Expand Down Expand Up @@ -578,3 +577,19 @@ func (opt *mongoOptions) getHostRestoreStats(err error) []api_v1beta1.HostRestor

return restoreStats
}

func (opt *mongoOptions) getSnapshotForHost(hostname string, rules []api_v1beta1.Rule) string {
var hostSnapshot string
for _, rule := range rules {
if len(rule.TargetHosts) == 0 || containsString(rule.TargetHosts, hostname) {
hostSnapshot = rule.Snapshots[0]
// if rule has empty targetHost then check further rules to see if any other rule with non-empty targetHost matches
if len(rule.TargetHosts) == 0 {
continue
} else {
return hostSnapshot
}
}
}
return hostSnapshot
}
19 changes: 6 additions & 13 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package pkg

import (
"crypto/rand"
"fmt"
"os/exec"
"strings"
Expand Down Expand Up @@ -96,17 +95,11 @@ func containsArg(args []string, checklist sets.String) bool {
return false
}

var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"

func genPassword(length int) (string, error) {
ll := len(chars)
b := make([]byte, length)
_, err := rand.Read(b) // generates len(b) random bytes
if err != nil {
return "", err
}
for i := 0; i < length; i++ {
b[i] = chars[int(b[i])%ll]
func containsString(a []string, e string) bool {
for _, s := range a {
if s == e {
return true
}
}
return string(b), nil
return false
}

0 comments on commit 7731cd5

Please sign in to comment.