-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
204 lines (191 loc) · 4.25 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"log/syslog"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"path/filepath"
"regexp"
"strconv"
"github.com/jrick/ss/keyfile"
"golang.org/x/term"
)
const FormatVersion = uint16(1)
var (
syslogDebug bool
sysLog *syslog.Writer
)
func usage() {
fmt.Fprintln(os.Stderr, "backup\nrestore /RESTOREPATH [file] [level]\ncat <inc-file>")
}
func main() {
var err error
sysLog, err = syslog.New(syslog.LOG_EMERG|syslog.LOG_DAEMON, "")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create syslog logger: %v", err)
os.Exit(1)
}
cfg, err := loadConfig()
if err != nil {
err = fmt.Errorf("loadConfig failed: %v", err)
log.Printf("%v", err)
sysLog.Err(err.Error())
os.Exit(1)
}
if cfg.Debug {
syslogDebug = true
}
if len(os.Args) < 2 {
usage()
os.Exit(1)
}
if cfg.Profile {
go func() {
listenAddr := "localhost:45454"
sysLog.Info(fmt.Sprintf("Creating profiling server "+
"listening on %s", listenAddr))
profileRedirect := http.RedirectHandler("/debug/pprof",
http.StatusSeeOther)
http.Handle("/", profileRedirect)
err := http.ListenAndServe(listenAddr, nil)
if err != nil {
sysLog.Err(fmt.Sprintf("%v", err))
os.Exit(1)
}
}()
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
go func() {
for sig := range signals {
fmt.Fprintf(os.Stderr, "signal received: %v", sig)
cancel()
}
}()
var gErr error
switch os.Args[1] {
case "backup":
if len(os.Args) != 2 {
usage()
os.Exit(1)
}
if len(cfg.BackupPath) == 0 {
fmt.Fprintln(os.Stderr, "backuppath not set")
os.Exit(1)
}
if len(cfg.Backup.Group) == 0 {
fmt.Fprintln(os.Stderr, "backup group not set")
os.Exit(1)
}
if len(cfg.Backup.Paths) == 0 {
fmt.Fprintln(os.Stderr, "no paths to backup")
os.Exit(1)
}
if len(cfg.Backup.PubkeyFile) == 0 {
fmt.Fprintln(os.Stderr, "pubkeyfile not set")
os.Exit(1)
}
pubKeyBytes, err := ioutil.ReadFile(cfg.Backup.PubkeyFile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
pubKey, err := keyfile.ReadPublicKey(bytes.NewReader(pubKeyBytes))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
gErr = backup(ctx, pubKey, cfg)
case "cat":
if len(os.Args) < 3 {
usage()
os.Exit(1)
}
if len(cfg.Restore.SecretFile) == 0 {
fmt.Fprintln(os.Stderr, "secretfile not set")
os.Exit(1)
}
skBytes, err := ioutil.ReadFile(cfg.Restore.SecretFile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "%q secret: ", cfg.Restore.SecretFile)
secret, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Fprint(os.Stderr, "\n")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
sk, _, err := keyfile.OpenSecretKey(bytes.NewReader(skBytes), secret)
zero(secret)
zero(skBytes)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
gErr = cat(ctx, sk, os.Args[2])
case "restore":
if len(os.Args) < 3 {
usage()
os.Exit(1)
}
destDir := filepath.Clean(os.Args[2])
var fileRegexp *regexp.Regexp
if len(os.Args) > 3 {
fileRegexp, err = regexp.Compile(os.Args[3])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
ii := int32(-1)
if len(os.Args) > 4 {
i, err := strconv.ParseUint(os.Args[4], 10, 16)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
ii = int32(i)
}
if len(cfg.Restore.SecretFile) == 0 {
fmt.Fprintln(os.Stderr, "secretfile not set")
os.Exit(1)
}
skBytes, err := ioutil.ReadFile(cfg.Restore.SecretFile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "%q secret: ", cfg.Restore.SecretFile)
secret, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Fprint(os.Stderr, "\n")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
sk, _, err := keyfile.OpenSecretKey(bytes.NewReader(skBytes), secret)
zero(secret)
zero(skBytes)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
gErr = restore(ctx, sk, cfg.BackupPath, destDir, fileRegexp, ii)
default:
usage()
os.Exit(1)
}
if gErr != nil {
sysLog.Err(gErr.Error())
os.Exit(1)
}
}