Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
Download a backup locally.

USAGE:
scw rdb backup download <backup-id ...> [arg=value ...]

EXAMPLES:
Download a backup
scw rdb backup download 11111111-1111-1111-1111-111111111111

ARGS:
backup-id ID of the backup you want to download.
[output] Destination to write to
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams)

FLAGS:
-h, --help help for download

GLOBAL FLAGS:
-c, --config string The path to the config file
-D, --debug Enable debug mode
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
-p, --profile string The config profile to use
1 change: 1 addition & 0 deletions cmd/scw/testdata/test-all-usage-rdb-backup-usage.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ USAGE:
AVAILABLE COMMANDS:
create Create a database backup
delete Delete a database backup
download Download a backup locally
export Export a database backup
get Get a database backup
list List database backups
Expand Down
1 change: 1 addition & 0 deletions internal/namespaces/rdb/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func GetCommands() *core.Commands {
instanceWaitCommand(),
instanceConnectCommand(),
backupWaitCommand(),
backupDownloadCommand(),
))
cmds.MustFind("rdb", "acl", "add").Override(aclAddBuilder)
cmds.MustFind("rdb", "acl", "delete").Override(aclDeleteBuilder)
Expand Down
100 changes: 100 additions & 0 deletions internal/namespaces/rdb/v1/custom_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package rdb

import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"reflect"
"strings"
"time"

"github.com/fatih/color"
Expand Down Expand Up @@ -112,3 +118,97 @@ func backupRestoreBuilder(c *core.Command) *core.Command {

return c
}

func getDefaultFileName(rawURL string) (string, error) {
u, err := url.Parse(rawURL)
if err != nil {
return "", err
}
splitURL := strings.Split(u.Path, "/")
filename := splitURL[len(splitURL)-1]
return filename, nil
}

func backupDownloadCommand() *core.Command {
type backupDownloadArgs struct {
BackupID string
Region scw.Region
Output string
}

return &core.Command{
Short: `Download a backup locally`,
Long: `Download a backup locally.`,
Namespace: "rdb",
Resource: "backup",
Verb: "download",
ArgsType: reflect.TypeOf(backupDownloadArgs{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
args := argsI.(*backupDownloadArgs)
api := rdb.NewAPI(core.ExtractClient(ctx))
backup, err := api.WaitForDatabaseBackup(&rdb.WaitForDatabaseBackupRequest{
DatabaseBackupID: args.BackupID,
Region: args.Region,
Timeout: scw.TimeDurationPtr(backupActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
if err != nil {
return nil, err
}
if backup.DownloadURL == nil {
return nil, fmt.Errorf("no download URL found")
}

res, err := http.Get(*backup.DownloadURL)
if err != nil {
return nil, err
}
defer res.Body.Close()

// Create the file
filename, err := getDefaultFileName(*backup.DownloadURL)
if err != nil {
return nil, err
}
if args.Output != "" {
Comment thread
remyleone marked this conversation as resolved.
filename = args.Output
}

out, err := os.Create(filename)
if err != nil {
return nil, err
}
defer out.Close()

// Write the body to file
size, err := io.Copy(out, res.Body)
if err != nil {
return nil, err
}
sizeStr, err := human.Marshal(scw.Size(size), nil)
if err != nil {
return nil, err
}
return fmt.Sprintf("Backup downloaded to %s successfully (%s written)", filename, sizeStr), nil
Comment thread
remyleone marked this conversation as resolved.
Outdated
},
ArgSpecs: core.ArgSpecs{
{
Name: "backup-id",
Short: `ID of the backup you want to download.`,
Required: true,
Positional: true,
},
{
Name: "output",
Short: "Destination to write to",
Comment thread
remyleone marked this conversation as resolved.
Outdated
},
core.RegionArgSpec(scw.RegionFrPar, scw.RegionNlAms),
},
Examples: []*core.Example{
{
Short: "Download a backup",
ArgsJSON: `{"backup_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
}
}
30 changes: 30 additions & 0 deletions internal/namespaces/rdb/v1/custom_backup_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rdb

import (
"path"
"testing"
"time"

Expand Down Expand Up @@ -81,3 +82,32 @@ func Test_ExportBackup(t *testing.T) {
DefaultRegion: scw.RegionNlAms,
}))
}

func Test_DownloadBackup(t *testing.T) {
t.Run("Simple", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
createInstance(engine),
core.ExecStoreBeforeCmd(
"Backup",
"scw rdb backup create name=foobar expires-at=2999-01-02T15:04:05-07:00 instance-id={{ .Instance.ID }} database-name=rdb --wait",
),
core.ExecStoreBeforeCmd(
"BackupExport",
"scw rdb backup export {{ .Backup.ID }} --wait",
),
func(ctx *core.BeforeFuncCtx) error {
ctx.Meta["DEST"] = path.Join(ctx.OverrideEnv["HOME"], "dump")
return nil
},
),
Cmd: "scw rdb backup download {{ .Backup.ID }} output={{ .DEST }}",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: deleteInstance(),
DefaultRegion: scw.RegionNlAms,
TmpHomeDir: true,
}))
}
Loading