Skip to content

Commit ca7d94d

Browse files
authored
feat(rdb): add to backup download an export step in case it was not done previously (#2498)
1 parent 68fae1d commit ca7d94d

6 files changed

+1024
-202
lines changed

internal/namespaces/rdb/v1/custom_backup.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,30 @@ func backupDownloadCommand() *core.Command {
226226
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
227227
args := argsI.(*backupDownloadArgs)
228228
api := rdb.NewAPI(core.ExtractClient(ctx))
229-
backup, err := api.WaitForDatabaseBackup(&rdb.WaitForDatabaseBackupRequest{
229+
backupRequest := &rdb.WaitForDatabaseBackupRequest{
230230
DatabaseBackupID: args.BackupID,
231231
Region: args.Region,
232232
Timeout: scw.TimeDurationPtr(backupActionTimeout),
233233
RetryInterval: core.DefaultRetryInterval,
234-
})
234+
}
235+
backup, err := api.WaitForDatabaseBackup(backupRequest)
235236
if err != nil {
236237
return nil, err
237238
}
238239
if backup.DownloadURL == nil {
239-
return nil, fmt.Errorf("no download URL found")
240+
exportRequest := rdb.ExportDatabaseBackupRequest{
241+
DatabaseBackupID: args.BackupID,
242+
Region: args.Region,
243+
}
244+
_, err = api.ExportDatabaseBackup(&exportRequest)
245+
if err != nil {
246+
return nil, err
247+
}
248+
}
249+
250+
backup, err = api.WaitForDatabaseBackup(backupRequest)
251+
if err != nil {
252+
return nil, err
240253
}
241254

242255
httpClient := core.ExtractHTTPClient(ctx)

internal/namespaces/rdb/v1/custom_backup_test.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,40 @@ func Test_DownloadBackup(t *testing.T) {
9595
"scw rdb backup export {{ .Backup.ID }} --wait",
9696
),
9797
),
98-
Cmd: "scw rdb backup download {{ .Backup.ID }} output=dump",
98+
Cmd: "scw rdb backup download {{ .Backup.ID }} output=simple_dump",
9999
Check: core.TestCheckCombine(
100100
core.TestCheckGolden(),
101101
core.TestCheckExitCode(0),
102102
),
103103
AfterFunc: core.AfterFuncCombine(
104104
deleteInstance(),
105105
func(ctx *core.AfterFuncCtx) error {
106-
err := os.Remove("dump")
106+
err := os.Remove("simple_dump")
107+
return err
108+
},
109+
),
110+
DefaultRegion: scw.RegionNlAms,
111+
TmpHomeDir: true,
112+
}))
113+
114+
t.Run("With no previous export backup", core.Test(&core.TestConfig{
115+
Commands: GetCommands(),
116+
BeforeFunc: core.BeforeFuncCombine(
117+
createInstance(engine),
118+
core.ExecStoreBeforeCmd(
119+
"Backup",
120+
"scw rdb backup create name=foobar expires-at=2999-01-02T15:04:05-07:00 instance-id={{ .Instance.ID }} database-name=rdb --wait",
121+
),
122+
),
123+
Cmd: "scw rdb backup download {{ .Backup.ID }} output=no_previous_export_dump",
124+
Check: core.TestCheckCombine(
125+
core.TestCheckGolden(),
126+
core.TestCheckExitCode(0),
127+
),
128+
AfterFunc: core.AfterFuncCombine(
129+
deleteInstance(),
130+
func(ctx *core.AfterFuncCtx) error {
131+
err := os.Remove("no_previous_export_dump")
107132
return err
108133
},
109134
),

0 commit comments

Comments
 (0)