Skip to content

Commit be10f0b

Browse files
authored
Add support for restoring specific snapshot (#1927) (#1960)
/cherry-pick Signed-off-by: hmsayem <hmsayem@appscode.com>
1 parent 147be12 commit be10f0b

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
@@ -238,14 +238,14 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
238238
// So, for stand-alone MongoDB and MongoDB ReplicaSet, we don't have to do anything.
239239
// We only need to update totalHosts field for sharded MongoDB
240240

241+
restoreSession, err := opt.stashClient.StashV1beta1().RestoreSessions(opt.namespace).Get(context.TODO(), opt.restoreSessionName, metav1.GetOptions{})
242+
if err != nil {
243+
return nil, err
244+
}
241245
opt.totalHosts = 1
242246
// For sharded MongoDB, parameter.ConfigServer will not be empty
243247
if parameters.ConfigServer != "" {
244248
opt.totalHosts = len(parameters.ReplicaSets) + 1 // for each shard there will be one key in parameters.ReplicaSet
245-
restoreSession, err := opt.stashClient.StashV1beta1().RestoreSessions(opt.namespace).Get(context.TODO(), opt.restoreSessionName, metav1.GetOptions{})
246-
if err != nil {
247-
return nil, err
248-
}
249249
_, err = stash_cs_util.UpdateRestoreSessionStatus(
250250
context.TODO(),
251251
opt.stashClient.StashV1beta1(),
@@ -326,9 +326,8 @@ func (opt *mongoOptions) restoreMongoDB(targetRef api_v1beta1.TargetRef) (*resti
326326
Host: hostKey,
327327
SourceHost: hostKey,
328328
FileName: opt.defaultDumpOptions.FileName,
329-
Snapshot: opt.defaultDumpOptions.Snapshot,
329+
Snapshot: opt.getSnapshotForHost(hostKey, restoreSession.Spec.Target.Rules),
330330
}
331-
332331
// setup pipe command
333332
restoreCmd := restic.Command{
334333
Name: MongoRestoreCMD,
@@ -578,3 +577,19 @@ func (opt *mongoOptions) getHostRestoreStats(err error) []api_v1beta1.HostRestor
578577

579578
return restoreStats
580579
}
580+
581+
func (opt *mongoOptions) getSnapshotForHost(hostname string, rules []api_v1beta1.Rule) string {
582+
var hostSnapshot string
583+
for _, rule := range rules {
584+
if len(rule.TargetHosts) == 0 || containsString(rule.TargetHosts, hostname) {
585+
hostSnapshot = rule.Snapshots[0]
586+
// if rule has empty targetHost then check further rules to see if any other rule with non-empty targetHost matches
587+
if len(rule.TargetHosts) == 0 {
588+
continue
589+
} else {
590+
return hostSnapshot
591+
}
592+
}
593+
}
594+
return hostSnapshot
595+
}

pkg/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,12 @@ func genPassword(length int) (string, error) {
110110
}
111111
return string(b), nil
112112
}
113+
114+
func containsString(a []string, e string) bool {
115+
for _, s := range a {
116+
if s == e {
117+
return true
118+
}
119+
}
120+
return false
121+
}

0 commit comments

Comments
 (0)