Skip to content

Commit

Permalink
actually pass global opts to restic cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
Hermsi1337 committed Mar 20, 2020
1 parent 250a391 commit dfb7171
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pkg/restic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewResticClient(logger *log.Entry, hostname string, backupPaths ...string)
func (c *Client) DoResticBackup(ctx context.Context) error {
c.Logger.Info("running 'restic backup'")

_, err := initBackup()
_, err := initBackup(ctx, c.Config.Global)
if err == ErrRepoAlreadyInitialized {
c.Logger.Info("restic repo is already initialized")
} else if err != nil {
Expand All @@ -59,7 +59,7 @@ func (c *Client) DoResticBackup(ctx context.Context) error {
c.Logger.Info("restic repo initialized successfully")
}

_, _, err = CreateBackup(ctx, c.Config.Backup, true)
_, _, err = CreateBackup(ctx, c.Config.Global, c.Config.Backup, true)
if err != nil {
return errors.WithStack(fmt.Errorf("error while while running restic backup: %s", err.Error()))
}
Expand All @@ -72,7 +72,7 @@ func (c *Client) DoResticBackup(ctx context.Context) error {
func (c *Client) DoResticForget(ctx context.Context) error {
c.Logger.Info("running 'restic forget'")

removedSnapshots, output, err := Forget(ctx, c.Config.Forget)
removedSnapshots, output, err := Forget(ctx, c.Config.Global, c.Config.Forget)
if err != nil {
return errors.WithStack(fmt.Errorf("%s - %s", err.Error(), output))
}
Expand Down
41 changes: 30 additions & 11 deletions pkg/restic/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -41,8 +40,14 @@ func init() {
}

// InitBackup executes "restic init"
func initBackup() ([]byte, error) {
out, err := exec.Command(binary, "init").CombinedOutput()
func initBackup(ctx context.Context, globalOpts *GlobalOptions) ([]byte, error) {
cmd := cli.CommandType{
Binary: binary,
Command: "init",
Args: cli.StructToCLI(globalOpts),
}

out, err := cli.RunWithTimeout(ctx, cmd, cmdTimeout)
if err != nil {
// s3 init-check
if strings.Contains(string(out), "config already initialized") {
Expand Down Expand Up @@ -73,7 +78,7 @@ func parseSnapshotOut(str string) (BackupResult, error) {
}

// CreateBackup executes "restic backup" and returns the parent snapshot id (if available) and the snapshot id
func CreateBackup(ctx context.Context, opts *BackupOptions, unlock bool) (BackupResult, []byte, error) {
func CreateBackup(ctx context.Context, globalOpts *GlobalOptions, backupOpts *BackupOptions, unlock bool) (BackupResult, []byte, error) {
var out []byte
var err error

Expand All @@ -83,16 +88,20 @@ func CreateBackup(ctx context.Context, opts *BackupOptions, unlock bool) (Backup
RemoveAll: false,
},
}
out, err = Unlock(ctx, &unlockOpts)
out, err = Unlock(ctx, globalOpts, &unlockOpts)
if err != nil {
return BackupResult{}, out, err
}
}

var args []string
args = cli.StructToCLI(globalOpts)
args = append(args, cli.StructToCLI(backupOpts)...)

cmd := cli.CommandType{
Binary: binary,
Command: "backup",
Args: cli.StructToCLI(opts),
Args: args,
}
out, err = cli.RunWithTimeout(ctx, cmd, cmdTimeout)
if err != nil {
Expand Down Expand Up @@ -268,13 +277,19 @@ func Check(ctx context.Context, flags *CheckFlags) ([]byte, error) {
}

// Forget executes "restic forget"
func Forget(ctx context.Context, opts *ForgetOptions) (removedSnapshots []string, output []byte, err error) {
opts.Flags.Compact = true // make sure compact mode is enabled to parse result correctly
func Forget(ctx context.Context, globalOpts *GlobalOptions, forgetOpts *ForgetOptions) (removedSnapshots []string, output []byte, err error) {
forgetOpts.Flags.Compact = true // make sure compact mode is enabled to parse result correctly

var args []string
args = cli.StructToCLI(globalOpts)
args = append(args, cli.StructToCLI(forgetOpts)...)

cmd := cli.CommandType{
Binary: binary,
Command: "forget",
Args: cli.StructToCLI(opts),
Args: args,
}

out, err := cli.Run(ctx, cmd)
if err != nil {
return nil, out, err
Expand Down Expand Up @@ -356,11 +371,15 @@ func RestoreBackup(ctx context.Context, opts *RestoreOptions) ([]byte, error) {
}

// Unlock executes "restic unlock"
func Unlock(ctx context.Context, opts *UnlockOptions) ([]byte, error) {
func Unlock(ctx context.Context, globalOpts *GlobalOptions, unlockOpts *UnlockOptions) ([]byte, error) {
var args []string
args = cli.StructToCLI(globalOpts)
args = append(args, cli.StructToCLI(unlockOpts)...)

cmd := cli.CommandType{
Binary: binary,
Command: "unlock",
Args: cli.StructToCLI(opts),
Args: args,
}
return cli.Run(ctx, cmd)
}
Expand Down

0 comments on commit dfb7171

Please sign in to comment.