-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestic.go
83 lines (72 loc) · 1.66 KB
/
restic.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
package main
import (
"fmt"
"os"
"os/exec"
"strconv"
)
type ForgetConfig struct {
Days int `default:"5"`
Weeks int `default:"4"`
Months int `default:"3"`
Years int `default:"2"`
}
type ResticConfig struct {
Repo string
Password string
Env map[string]string
BackupArgs []string `yaml:"backup_args"`
Forget ForgetConfig
}
func (r *ResticConfig) cmd(args ...string) *exec.Cmd {
logger.Debug("restic call", "args", args)
cmd := exec.Command("restic", args...)
cmd.Env = cmd.Environ()
cmd.Env = append(
cmd.Env,
fmt.Sprintf("%s=%s", "RESTIC_REPOSITORY", r.Repo),
fmt.Sprintf("%s=%s", "RESTIC_PASSWORD", r.Password),
)
for k, v := range r.Env {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
}
// let the child processes have our STDIO
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}
func (r *ResticConfig) init() error {
cmd := r.cmd("init")
return cmd.Run()
}
func (r *ResticConfig) backup_check() error {
cmd := r.cmd("check")
return cmd.Run()
}
func (r *ResticConfig) config_check() error {
cmd := r.cmd("cat", "config")
return cmd.Run()
}
func (r *ResticConfig) backup() error {
backupArgs := []string{"backup", "--verbose"}
backupArgs = append(backupArgs, r.BackupArgs...)
cmd := r.cmd(backupArgs...)
return cmd.Run()
}
func (r *ResticConfig) command(args []string) error {
cmd := r.cmd(args...)
return cmd.Run()
}
func (r *ResticConfig) forget() error {
cmd := r.cmd(
"forget",
"-d", strconv.Itoa(r.Forget.Days),
"-w", strconv.Itoa(r.Forget.Weeks),
"-m", strconv.Itoa(r.Forget.Months),
"-y", strconv.Itoa(r.Forget.Years),
"--prune",
"--compact",
)
return cmd.Run()
}