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] Add support for restoring specific snapshot (#1927) #1960

Merged
merged 1 commit into from
Oct 4, 2023
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
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
}
9 changes: 9 additions & 0 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,12 @@ func genPassword(length int) (string, error) {
}
return string(b), nil
}

func containsString(a []string, e string) bool {
for _, s := range a {
if s == e {
return true
}
}
return false
}
Loading