Skip to content

Commit f74ae14

Browse files
committed
br: support ebs offline test by openebs
Signed-off-by: BornChanger <[email protected]>
1 parent f64cc69 commit f74ae14

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

br/pkg/task/backup_ebs.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
"encoding/json"
1010
"fmt"
1111
"io"
12+
"os"
13+
"os/exec"
1214
"sort"
15+
"strings"
1316
"sync"
1417
"time"
1518

@@ -220,6 +223,38 @@ func RunBackupEBS(c context.Context, g glue.Glue, cfg *BackupConfig) error {
220223
}
221224
log.Info("async snapshots finished.")
222225
} else {
226+
if _, err := os.Stat("/usr/local/bin/create-snapshot.sh"); err == nil {
227+
log.Info("start to create snapshots")
228+
if err := os.WriteFile("backupmeta.json", []byte(backupInfo.String()), 0644); err != nil {
229+
return errors.Trace(err)
230+
}
231+
cmd := exec.Command("create-snapshot.sh", cfg.VolumeFile)
232+
cmd.Stdout = os.Stdout
233+
cmd.Stderr = os.Stderr
234+
if err := cmd.Run(); err != nil {
235+
log.Warn("failed to create snapshots", zap.Error(err))
236+
return errors.Trace(err)
237+
}
238+
// Script should write snapshot id to /snapshot-id.txt.
239+
// Each line contains: <volume-id> <snapshot-id>
240+
data, err := os.ReadFile("snapshot-ids.txt")
241+
if err != nil {
242+
return errors.Trace(err)
243+
}
244+
log.Info("create snapshots finished", zap.String("snapshot-ids", string(data)))
245+
for _, line := range strings.Split(string(data), "\n") {
246+
if strings.TrimSpace(line) == "" {
247+
continue
248+
}
249+
fields := strings.Split(line, " ")
250+
if len(fields) != 2 {
251+
log.Warn("invalid snapshot id line", zap.String("line", line))
252+
continue
253+
}
254+
volumeID, snapID := strings.TrimSpace(fields[0]), strings.TrimSpace(fields[1])
255+
snapIDMap[volumeID] = snapID
256+
}
257+
}
223258
for i := 0; i < int(storeCount); i++ {
224259
progress.IncBy(100)
225260
totalSize = 1024

br/pkg/task/restore_ebs_meta.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"crypto/tls"
1919
"encoding/json"
2020
"os"
21+
"os/exec"
2122
"strings"
2223
"time"
2324

@@ -199,6 +200,40 @@ func (h *restoreEBSMetaHelper) doRestore(ctx context.Context, progress glue.Prog
199200
}
200201

201202
if h.cfg.SkipAWS {
203+
if _, err := os.Stat("/usr/local/bin/install-snapshot.sh"); err == nil {
204+
log.Info("start to install snapshots")
205+
if err := os.WriteFile("backupmeta.json", []byte(h.metaInfo.String()), 0644); err != nil {
206+
return 0, errors.Trace(err)
207+
}
208+
cmd := exec.Command("install-snapshot.sh")
209+
cmd.Stdout = os.Stdout
210+
cmd.Stderr = os.Stderr
211+
if err := cmd.Run(); err != nil {
212+
log.Warn("failed to install snapshots", zap.Error(err))
213+
return 0, errors.Trace(err)
214+
}
215+
// Script should write the restore volume ids to /volume-ids.txt.
216+
// Each line contains: <volume-id> <restore-volume-id>
217+
data, err := os.ReadFile("volume-ids.txt")
218+
if err != nil {
219+
return 0, errors.Trace(err)
220+
}
221+
log.Info("install snapshots finished", zap.String("volume-ids", string(data)))
222+
volumeIDMap := make(map[string]string)
223+
for _, line := range strings.Split(string(data), "\n") {
224+
if strings.TrimSpace(line) == "" {
225+
continue
226+
}
227+
fields := strings.Split(line, " ")
228+
if len(fields) != 2 {
229+
log.Warn("invalid volume id line", zap.String("line", line))
230+
continue
231+
}
232+
volumeID, restoreVolumeID := strings.TrimSpace(fields[0]), strings.TrimSpace(fields[1])
233+
volumeIDMap[volumeID] = restoreVolumeID
234+
}
235+
h.metaInfo.SetRestoreVolumeIDs(volumeIDMap)
236+
}
202237
for i := 0; i < int(h.metaInfo.GetStoreCount()); i++ {
203238
progress.Inc()
204239
log.Info("mock: create volume from snapshot finished.", zap.Int("index", i))

0 commit comments

Comments
 (0)