-
Notifications
You must be signed in to change notification settings - Fork 0
/
restic_executor.go
96 lines (81 loc) · 2.32 KB
/
restic_executor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package bkp
import (
"fmt"
"runtime"
"strconv"
"strings"
"github.com/jojomi/go-script/v2"
"github.com/rs/zerolog/log"
)
type ResticExecutor struct {
DryRun bool
context *script.Context
target *Target
cacheDir string
hasNiceCommand bool
hasIONiceCommand bool
}
func NewResticExecutor() *ResticExecutor {
ex := ResticExecutor{}
ex.context = script.NewContext()
ex.hasNiceCommand = ex.context.CommandExists("nice")
ex.hasIONiceCommand = ex.context.CommandExists("ionice")
return &ex
}
func (e *ResticExecutor) SetCacheDir(cacheDir string) {
e.cacheDir = cacheDir
}
func (e *ResticExecutor) SetTarget(t *Target) {
e.target = t
if t.Password != "" {
e.context.SetEnv("RESTIC_PASSWORD", t.Password)
}
e.context.SetEnv("RESTIC_REPOSITORY", t.Type+":"+t.Path)
}
func (e *ResticExecutor) Command(command string, args ...string) (*script.ProcessResult, error) {
var fullArgs []string
if e.context.CommandExists("nice") {
var niceValue int
switch runtime.GOOS {
case "linux":
niceValue = 19
case "darwin":
niceValue = 20
default:
niceValue = 19
}
niceArgs := []string{"nice", "-n", strconv.Itoa(niceValue)}
log.Debug().Strs("args", niceArgs).Msg("nice is available, using it")
fullArgs = append(fullArgs, niceArgs...)
}
if e.context.CommandExists("ionice") {
ioniceArgs := []string{"ionice", "-c2", "-n7"}
log.Debug().Strs("args", ioniceArgs).Msg("ionice is available, using it")
fullArgs = append(fullArgs, ioniceArgs...)
}
resticBaseArgs := []string{"restic", command}
log.Debug().Strs("args", resticBaseArgs).Msg("building basic restic command")
fullArgs = append(fullArgs, resticBaseArgs...)
if e.cacheDir != "" {
cacheArgs := []string{"--cache-dir", e.cacheDir}
log.Debug().Strs("args", cacheArgs).Msg("adding cache args")
fullArgs = append(fullArgs, cacheArgs...)
}
log.Debug().Strs("args", args).Msg("adding restic command args")
fullArgs = append(fullArgs, args...)
localCommand := script.NewLocalCommand()
localCommand.AddAll(fullArgs...)
log.Info().
Str("command", localCommand.String()).
Strs("environment", e.context.GetCustomEnv()).
Msg("full command")
if e.DryRun {
fmt.Println(strings.Join(fullArgs, " "))
return nil, nil
}
return e.context.Execute(script.CommandConfig{
RawStdout: true,
RawStderr: true,
ConnectStdin: false,
}, localCommand)
}