-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
266 lines (236 loc) · 8.13 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"bufio"
"bytes"
"encoding/json"
"io"
"os/exec"
"regexp"
"strings"
"sync"
"time"
_ "github.com/joho/godotenv/autoload"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
"github.com/robfig/cron"
"go.uber.org/zap"
)
type backup struct {
Schedule string `required:"true" envconfig:"SCHEDULE"` // cron schedule
Repository string `required:"true" envconfig:"RESTIC_REPOSITORY"` // repository name
Password string `required:"true" envconfig:"RESTIC_PASSWORD"` // repository password
Args string ` envconfig:"RESTIC_ARGS"` // additional args for backup command
RunOnBoot bool ` envconfig:"RUN_ON_BOOT"` // run a backup on startup
TriggerEndpoint string `default:"/trigger" envconfig:"TRIGGER_ENDPOINT"` // trigger endpoint
PrometheusEndpoint string `default:"/metrics" envconfig:"PROMETHEUS_ENDPOINT"` // metrics endpoint
PrometheusAddress string `default:":8080" envconfig:"PROMETHEUS_ADDRESS"` // metrics host:port
PreCommand string ` envconfig:"PRE_COMMAND"` // command to execute before restic is executed
PostCommand string ` envconfig:"POST_COMMAND"` // command to execute after restic was executed (successfully)
ErrorCommand string ` envconfig:"ERROR_COMMAND"` // command to execute after a failed restic execution
// lock is used to prevent concurrent backups from happening
lock sync.Mutex
// metrics defines all the different Prometheus metrics in use
metrics
}
var (
matchExists = regexp.MustCompile(`.*already (exists|initialized).*`)
)
type stats struct {
filesNew int
filesChanged int
filesUnmodified int
filesProcessed int
bytesAdded int64
bytesProcessed int64
}
func main() {
b := backup{}
err := envconfig.Process("", &b)
if err != nil {
logger.Fatal("failed to configure", zap.Error(err))
}
err = b.Ensure()
if err != nil {
logger.Fatal("failed to ensure repository", zap.Error(err))
}
b.initializeMetrics()
if b.PrometheusAddress != "" {
b.setupTrigger()
go b.startMetricsServer()
} else {
logger.Info("metrics and manual trigger disabled")
}
cr := cron.New()
err = cr.AddJob(b.Schedule, &b)
if err != nil {
logger.Fatal("failed to schedule task", zap.Error(err))
}
if b.RunOnBoot {
b.Run()
}
cr.Run()
}
// Run performs the backup
func (b *backup) Run() {
// prevent concurrent backups from happening
if !b.lock.TryLock() {
logger.Warn("backup is already running")
return
}
// ensure lock is released after backup
defer b.lock.Unlock()
logger.Info("backup started")
startTime := time.Now()
// hold the backup success
success := false
b.backupStatus.Set(backupStatusRunning)
// process metrics after backup completed
defer func() {
if success {
// last backup succeeded
b.backupsSuccessful.Inc()
b.backupStatus.Set(backupStatusIdle)
b.backupsSuccessfulTimestamp.SetToCurrentTime()
} else {
// last backup failed
b.backupsFailed.Inc()
b.backupStatus.Set(backupStatusFailed)
}
b.backupsTotal.Inc()
}()
// execute pre-command (if configured)
if len(b.PreCommand) > 0 {
logger.Debug("executing pre-command", zap.String("command", b.PreCommand))
if stdout, err := b.executePreCommand(); err != nil {
logger.Error("failed to execute pre-command: " + err.Error())
return
} else {
logger.Info("output of pre-command: " + *stdout)
}
}
// execute restic backup
cmd := exec.Command("restic", append([]string{"backup", "--json"}, parseArg(b.Args)...)...)
errbuf := bytes.NewBuffer(nil)
outbuf := bytes.NewBuffer(nil)
cmd.Stderr = errbuf
cmd.Stdout = outbuf
if err := cmd.Run(); err != nil {
logger.Error("failed to run backup",
zap.Error(err),
zap.String("output", errbuf.String()))
b.backupStatus.Set(backupStatusFailed)
b.backupsFailed.Inc()
b.backupsTotal.Inc()
// execute error-command (if configured)
if len(b.ErrorCommand) > 0 {
logger.Debug("executing error-command", zap.String("command", b.ErrorCommand))
if stdout, err := b.executeErrorCommand(); err != nil {
logger.Error("failed to execute error-command: " + err.Error())
} else if stdout != nil {
logger.Info("output of error-command: " + *stdout)
}
}
return
}
// execute post-command (if configured)
if len(b.PostCommand) > 0 {
logger.Debug("executing post-command", zap.String("command", b.PostCommand))
if stdout, err := b.executePostCommand(); err != nil {
logger.Error("failed to execute post-command: " + err.Error())
return
} else {
logger.Info("output of post-command: " + *stdout)
}
}
d := time.Since(startTime)
statistics, err := extractJsonStats(outbuf)
if err != nil {
logger.Warn("failed to extract statistics from command output",
zap.Error(err))
}
logger.Info("backup completed",
zap.Duration("duration", d),
zap.Int("filesNew", statistics.filesNew),
zap.Int("filesChanged", statistics.filesChanged),
zap.Int("filesUnmodified", statistics.filesUnmodified),
zap.Int("filesProcessed", statistics.filesProcessed),
zap.Int64("bytesAdded", statistics.bytesAdded),
zap.Int64("bytesProcessed", statistics.bytesProcessed),
)
// indicate backup success
success = true
// process result and update metrics
b.backupDuration.Observe(float64(d.Milliseconds()))
b.filesNew.Observe(float64(statistics.filesNew))
b.filesChanged.Observe(float64(statistics.filesChanged))
b.filesUnmodified.Observe(float64(statistics.filesUnmodified))
b.filesProcessed.Observe(float64(statistics.filesProcessed))
b.bytesAdded.Observe(float64(statistics.bytesAdded))
b.bytesProcessed.Observe(float64(statistics.bytesProcessed))
}
func extractJsonStats(outbuf *bytes.Buffer) (result stats, err error) {
reader := bufio.NewReader(outbuf)
for {
line, err := reader.ReadBytes('\n')
if err == io.EOF {
break
}
if err != nil {
logger.Error("error reading output buffer", zap.Error(err))
return result, err
}
var msg BackupMessage
if err := json.Unmarshal(line, &msg); err != nil {
logger.Error("error unmarshalling JSON", zap.ByteString("line", line), zap.Error(err))
return result, err
}
switch msg.MessageType {
case "summary":
var summary BackupSummaryMessage
if err := json.Unmarshal(line, &summary); err != nil {
logger.Error("error unmarshalling summary message", zap.ByteString("line", line), zap.Error(err))
return result, err
}
result.filesNew = summary.FilesNew
result.filesChanged = summary.FilesChanged
result.filesUnmodified = summary.FilesUnmodified
result.filesProcessed = summary.TotalFilesProcessed
result.bytesAdded = summary.DataAdded
result.bytesProcessed = summary.TotalBytesProcessed
case "status":
logger.Debug("received status update", zap.ByteString("line", line))
case "error":
var errorMsg BackupErrorMessage
if err := json.Unmarshal(line, &errorMsg); err != nil {
logger.Error("error unmarshalling error message", zap.ByteString("line", line), zap.Error(err))
} else {
logger.Error("backup error", zap.String("error", errorMsg.Error), zap.String("during", errorMsg.During), zap.String("item", errorMsg.Item))
}
}
}
return result, nil
}
// Ensure will create a repository if it does not already exist
func (b *backup) Ensure() error {
logger.Info("ensuring backup repository exists")
cmd := exec.Command("restic", "init", "--json")
outbuf := &bytes.Buffer{}
cmd.Stderr = outbuf
cmd.Stdout = outbuf
if err := cmd.Run(); err != nil {
outputStr := strings.Trim(outbuf.String(), " \n\r")
if matchExists.MatchString(outputStr) {
logger.Info("repository exists")
return nil
}
logger.Error("failed to initialize repository", zap.Error(err), zap.String("output", outputStr))
return errors.Wrap(err, outputStr)
}
var initOutput InitMessage
if err := json.Unmarshal(outbuf.Bytes(), &initOutput); err != nil {
logger.Error("failed to parse JSON output from restic init", zap.Error(err))
return errors.Wrap(err, "parsing JSON output")
}
logger.Info("successfully created repository", zap.String("id", initOutput.ID), zap.String("repository", initOutput.Repository))
return nil
}