Skip to content

Commit 58e8213

Browse files
authored
Add support for restoring specific snapshot (#1927) (#1950)
/cherry-pick Signed-off-by: hmsayem <hmsayem@appscode.com>
1 parent e594e6e commit 58e8213

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

pkg/restore.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,14 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
226226
// So, for stand-alone MongoDB and MongoDB ReplicaSet, we don't have to do anything.
227227
// We only need to update totalHosts field for sharded MongoDB
228228

229+
restoreSession, err := opt.stashClient.StashV1beta1().RestoreSessions(opt.namespace).Get(context.TODO(), opt.restoreSessionName, metav1.GetOptions{})
230+
if err != nil {
231+
return nil, err
232+
}
229233
opt.totalHosts = 1
230234
// For sharded MongoDB, parameter.ConfigServer will not be empty
231235
if parameters.ConfigServer != "" {
232236
opt.totalHosts = len(parameters.ReplicaSets) + 1 // for each shard there will be one key in parameters.ReplicaSet
233-
restoreSession, err := opt.stashClient.StashV1beta1().RestoreSessions(opt.namespace).Get(context.TODO(), opt.restoreSessionName, metav1.GetOptions{})
234-
if err != nil {
235-
return nil, err
236-
}
237237
_, err = stash_cs_util.UpdateRestoreSessionStatus(
238238
context.TODO(),
239239
opt.stashClient.StashV1beta1(),
@@ -311,9 +311,8 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
311311
Host: hostKey,
312312
SourceHost: hostKey,
313313
FileName: opt.defaultDumpOptions.FileName,
314-
Snapshot: opt.defaultDumpOptions.Snapshot,
314+
Snapshot: opt.getSnapshotForHost(hostKey, restoreSession.Spec.Target.Rules),
315315
}
316-
317316
// setup pipe command
318317
restoreCmd := restic.Command{
319318
Name: MongoRestoreCMD,
@@ -426,3 +425,19 @@ func (opt *mongoOptions) getHostRestoreStats(err error) []api_v1beta1.HostRestor
426425

427426
return restoreStats
428427
}
428+
429+
func (opt *mongoOptions) getSnapshotForHost(hostname string, rules []api_v1beta1.Rule) string {
430+
var hostSnapshot string
431+
for _, rule := range rules {
432+
if len(rule.TargetHosts) == 0 || containsString(rule.TargetHosts, hostname) {
433+
hostSnapshot = rule.Snapshots[0]
434+
// if rule has empty targetHost then check further rules to see if any other rule with non-empty targetHost matches
435+
if len(rule.TargetHosts) == 0 {
436+
continue
437+
} else {
438+
return hostSnapshot
439+
}
440+
}
441+
}
442+
return hostSnapshot
443+
}

pkg/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,12 @@ func containsArg(args []string, checklist sets.String) bool {
9494
}
9595
return false
9696
}
97+
98+
func containsString(a []string, e string) bool {
99+
for _, s := range a {
100+
if s == e {
101+
return true
102+
}
103+
}
104+
return false
105+
}

0 commit comments

Comments
 (0)