Skip to content

Commit

Permalink
refactored after reviewed by Dipta vai
Browse files Browse the repository at this point in the history
  • Loading branch information
hossainemruz committed Mar 4, 2019
1 parent 47694c1 commit ca429a1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pkg/restic/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (w *ResticWrapper) RunBackup(backupOption *BackupOptions) (*BackupOutput, e

// Backup all target directories
for _, dir := range backupOption.BackupDirs {
out, err := w.backup(dir, nil)
out, err := w.backup(dir, backupOption.Host, nil)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/restic/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func (w *ResticWrapper) initRepositoryIfAbsent() ([]byte, error) {
return nil, nil
}

func (w *ResticWrapper) backup(path string, tags []string) ([]byte, error) {
func (w *ResticWrapper) backup(path, host string, tags []string) ([]byte, error) {
log.Infoln("Backing up target data")
args := []interface{}{"backup", path}
if w.config.Hostname != "" {
if host != "" {
args = append(args, "--host")
args = append(args, w.config.Hostname)
args = append(args, host)
}
// add tags if any
for _, tag := range tags {
Expand Down Expand Up @@ -123,8 +123,8 @@ func (w *ResticWrapper) restore(path, host, snapshotID string) ([]byte, error) {

// Remove last part from the path.
// https://github.com/appscode/stash/issues/392
args = append(args, "--target")
args = append(args, filepath.Dir(path))
args = append(args, "--target", "/")
// args = append(args, filepath.Dir(path))

args = w.appendCacheDirFlag(args)
args = w.appendCaCertFlag(args)
Expand Down
7 changes: 4 additions & 3 deletions pkg/restic/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package restic

import (
v1beta1_api "github.com/appscode/stash/apis/stash/v1beta1"
shell "github.com/codeskyblue/go-sh"
)

Expand All @@ -11,12 +10,15 @@ type ResticWrapper struct {
}

type BackupOptions struct {
Host string
BackupDirs []string
Cleanup CleanupOptions
}

type RestoreOptions struct {
Rules []v1beta1_api.Rule
Host string
RestoreDirs []string
Snapshots []string // when Snapshots are specified Host and RestoreDirs will not be used
}

type SetupOptions struct {
Expand All @@ -28,7 +30,6 @@ type SetupOptions struct {
CacertFile string
ScratchDir string
EnableCache bool
Hostname string
}

type CleanupOptions struct {
Expand Down
8 changes: 4 additions & 4 deletions pkg/restic/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ type RestoreOutput struct {
// WriteOutput write output of backup process into output.json file in the directory
// specified by outputDir parameter
func (out *BackupOutput) WriteOutput(fileName string) error {
jsonOuput, err := json.MarshalIndent(out, "", " ")
jsonOutput, err := json.MarshalIndent(out, "", " ")
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Base(fileName), 0755); err != nil {
if err := os.MkdirAll(filepath.Dir(fileName), 0755); err != nil {
return err
}
if err := ioutil.WriteFile(fileName, jsonOuput, 0755); err != nil {
if err := ioutil.WriteFile(fileName, jsonOutput, 0755); err != nil {
return err
}
return nil
Expand All @@ -85,7 +85,7 @@ func (out *RestoreOutput) WriteOutput(fileName string) error {
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Base(fileName), 0755); err != nil {
if err := os.MkdirAll(filepath.Dir(fileName), 0755); err != nil {
return err
}
if err := ioutil.WriteFile(fileName, jsonOuput, 0755); err != nil {
Expand Down
60 changes: 34 additions & 26 deletions pkg/restic/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,26 @@ import (
"time"

"github.com/appscode/go/strings"
"github.com/appscode/stash/apis/stash/v1beta1"
)

func (w *ResticWrapper) RunRestore(restoreOptions RestoreOptions) (*RestoreOutput, error) {
// Start clock to measure total restore duration
startTime := time.Now()

restoreOutput := &RestoreOutput{}

// Restore data according to rules. Restore will proceed only for first matching rule.
for _, rule := range restoreOptions.Rules {
// Check if Hosts field is empty. Empty Hosts filed will match any host.
if len(rule.Hosts) == 0 || strings.Contains(rule.Hosts, w.config.Hostname) {
if len(rule.Snapshots) != 0 {
for _, snapshot := range rule.Snapshots {
// if snapshot is specified then host and path does not matter.
_, err := w.restore("", "", snapshot)
if err != nil {
return nil, err
}
}
} else if len(rule.Paths) != 0 {
host := ""
if len(rule.Hosts) != 0 {
host = w.config.Hostname
}
for _, path := range rule.Paths {
_, err := w.restore(path, host, "")
if err != nil {
return nil, err
}
}
if len(restoreOptions.Snapshots) != 0 {
for _, snapshot := range restoreOptions.Snapshots {
// if snapshot is specified then host and path does not matter.
if _, err := w.restore("", "", snapshot); err != nil {
return nil, err
}
}
} else if len(restoreOptions.RestoreDirs) != 0 {
for _, path := range restoreOptions.RestoreDirs {
if _, err := w.restore(path, restoreOptions.Host, ""); err != nil {
return nil, err
}
// one rule is matched. so, stop rule matching.
break
}
}

Expand All @@ -47,3 +33,25 @@ func (w *ResticWrapper) RunRestore(restoreOptions RestoreOptions) (*RestoreOutpu

return restoreOutput, nil
}

// return first matching rule
// if hosts is empty for any rule, it will match any hostname
func RestoreOptionsForHost(hostname string, rules []v1beta1.Rule) RestoreOptions {
for _, rule := range rules {
if len(rule.Hosts) == 0 {
return RestoreOptions{
Host: "",
RestoreDirs: rule.Paths,
Snapshots: rule.Snapshots,
}
}
if strings.Contains(rule.Hosts, hostname) {
return RestoreOptions{
Host: hostname,
RestoreDirs: rule.Paths,
Snapshots: rule.Snapshots,
}
}
}
return RestoreOptions{}
}

0 comments on commit ca429a1

Please sign in to comment.