-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
317 lines (289 loc) · 10 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"errors"
"flag"
"fmt"
"net/http"
"os"
"os/exec"
"path"
"sort"
"github.com/dustin/go-humanize"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/mattevans/postmark-go"
"github.com/peterbourgon/ff/v3"
)
// executeBackupScript runs the database backup shell script and checks the exit code
func executeBackupScript(l log.Logger, passFilePath string, configFilePath string, scriptPath string) (string, error) {
var (
ee *exec.ExitError
pe *os.PathError
)
if scriptPath == "" {
return "", errors.New("script path is empty")
}
if configFilePath == "" {
return "", errors.New("config file path is empty")
}
cmd := exec.Command("bash", "-c", "source "+configFilePath)
out, err := cmd.CombinedOutput()
if err != nil {
level.Error(l).Log("msg", "sourcing config file", "err", err)
return string(out), err
}
cmd.Env = os.Environ()
cmd = exec.Command("/bin/bash", scriptPath)
cmd.Env = append(cmd.Env, fmt.Sprintf("PGPASSFILE=%s", passFilePath), fmt.Sprintf("CONFIG_FILE_PATH=%s", configFilePath))
out, err = cmd.CombinedOutput()
if err != nil {
return string(out), err
}
if errors.As(err, &ee) {
return string(out), fmt.Errorf("exit code error: %d", ee.ExitCode()) // ran, but non-zero exit code
} else if errors.As(err, &pe) {
return string(out), fmt.Errorf("os.PathError: %v", pe) // "no such file ...", "permission denied" etc.
} else if err != nil {
return string(out), err
} else {
return string(out), nil
}
}
type HistoryComparisonFiles struct {
FileName string `json:"file_name"`
Status string `json:"status"`
FileSizeNew int64 `json:"file_size_new"`
FileSizeOld int64 `json:"file_size_old"`
Difference int64 `json:"difference"`
DifferenceHumanized string `json:"difference_humanized"`
}
type HistoryComparison struct {
BackupName string `json:"backup_name"`
BackupComparisonName string `json:"backup_comparison_name"`
ComparedFiles []HistoryComparisonFiles `json:"compared_files"`
}
// checkBackupHistory reads the backup directory and checks the backup history, we return if a backup exists and if there's any errors
func checkBackupHistory(backupPath string) ([]HistoryComparison, bool, error) {
// Read directory contents
parentBackupPath := backupPath
files, err := os.ReadDir(parentBackupPath)
if err != nil {
return nil, false, err
}
// Create a slice to hold file info and names
var fileInfos []os.DirEntry
// Filter directories and gather info
for _, file := range files {
if file.IsDir() {
fileInfos = append(fileInfos, file)
}
}
// Sort fileInfos by directory name (descending)
sort.Slice(fileInfos, func(i, j int) bool {
return fileInfos[i].Name() > fileInfos[j].Name()
})
// If there are no backups yet, return false
if len(fileInfos) == 0 {
return nil, false, nil
}
// We can't compare to previous one if there's only one
if len(fileInfos) == 1 {
return nil, true, nil
}
type backupMeta struct {
backupName string
fileSizes map[string]int64
}
var previousBackups []backupMeta
for i, fileInfo := range fileInfos {
if i < 2 {
files, err := os.ReadDir(path.Join(parentBackupPath, fileInfo.Name()))
if err != nil {
return nil, true, err
}
// Create a map to hold filename and filesize
fileSizeMap := make(map[string]int64)
// Iterate over directory entries
for _, file := range files {
if !file.IsDir() {
info, err := file.Info()
if err != nil {
fmt.Println("Error getting file info:", err)
continue
}
fileSizeMap[file.Name()] = info.Size()
}
}
previousBackups = append(previousBackups, backupMeta{
backupName: fileInfo.Name(),
fileSizes: fileSizeMap,
})
}
}
var historyComparisons []HistoryComparison
for i := 0; i < len(previousBackups)-1; i++ {
current := previousBackups[i].fileSizes
next := previousBackups[i+1].fileSizes
//fmt.Printf("Comparing entry %s with entry %s:\n", previousBackups[i].backupName, previousBackups[i+1].backupName)
diff := HistoryComparison{
BackupName: previousBackups[i].backupName,
BackupComparisonName: previousBackups[i+1].backupName,
}
// Check keys in the current map
for key, currentValue := range current {
if nextValue, exists := next[key]; exists {
//fmt.Printf("Key '%s' exists in both. Difference: %d\n", key, nextValue-currentValue)
hcf := HistoryComparisonFiles{
FileName: key,
Status: "existing",
FileSizeNew: nextValue,
FileSizeOld: currentValue,
Difference: nextValue - currentValue,
}
var difference string
if hcf.Difference > 0 {
difference = humanize.Bytes(uint64(hcf.Difference))
} else {
difference = fmt.Sprintf("-%s", humanize.Bytes(uint64(hcf.Difference*-1)))
}
if hcf.Difference > 0 {
hcf.DifferenceHumanized = fmt.Sprintf("Difference: %s (Old: %s, New: %s)", difference, humanize.Bytes(uint64(hcf.FileSizeOld)), humanize.Bytes(uint64(hcf.FileSizeNew)))
} else {
hcf.DifferenceHumanized = fmt.Sprintf("Difference: None (Current: %s)", humanize.Bytes(uint64(hcf.FileSizeOld)))
}
diff.ComparedFiles = append(diff.ComparedFiles, hcf)
} else {
//fmt.Printf("Key '%s' was deleted.\n", key)
diff.ComparedFiles = append(diff.ComparedFiles, HistoryComparisonFiles{
FileName: key,
Status: "deleted",
})
}
}
// Check for new keys in the next map
for key := range next {
if _, exists := current[key]; !exists {
//fmt.Printf("Key '%s' is new.\n", key)
diff.ComparedFiles = append(diff.ComparedFiles, HistoryComparisonFiles{
FileName: key,
Status: "new",
})
}
}
historyComparisons = append(historyComparisons, diff)
}
return historyComparisons, true, nil
}
// printBackupHistory prints the backup history, it ignores everything below the threshold in bytes
func printBackupHistory(history []HistoryComparison, ignoreThreshold int64) {
for _, comparison := range history {
fmt.Printf("Comparison between %s and %s:\n", comparison.BackupName, comparison.BackupComparisonName)
for _, file := range comparison.ComparedFiles {
fmt.Println("-------------------------------------------------")
fmt.Println("File:", file.FileName)
if file.Status != "existing" {
fmt.Println("Status:", file.Status)
}
if file.Difference != 0 {
var differenceUnsigned int64
if file.Difference > 0 {
differenceUnsigned = file.Difference
} else {
differenceUnsigned = file.Difference * -1
}
if differenceUnsigned > ignoreThreshold {
fmt.Println(file.DifferenceHumanized)
}
continue
}
fmt.Println(file.DifferenceHumanized)
}
}
}
// sendEmail sends an email with the backup history
func sendEmail(client *postmark.Client, from string, to string, templateID int, history []HistoryComparison) error {
var subjectBackupName string
for _, comparison := range history {
subjectBackupName = comparison.BackupName
break
}
emailReq := &postmark.Email{
From: from,
To: to,
TemplateID: templateID,
TemplateModel: map[string]interface{}{
"history": history,
"backup_name": subjectBackupName,
},
}
_, response, err := client.Email.Send(emailReq)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", response.StatusCode)
}
return nil
}
func main() {
var l log.Logger
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
l = log.With(l, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
fs := flag.NewFlagSet("backup-health-notifier", flag.ContinueOnError)
var (
postmarkToken = fs.String("postmark-token", "", "The postmarkapp.com api token")
postmarkTemplateID = fs.Int("postmark-template-id", 0, "The id of the template to be used for the email")
backupPath = fs.String("backup-path", "", "The absolute path to the Postgres backup location")
configFilePath = fs.String("config-file-path", "", "The path to the config file used in the Postgres backup script")
passFilePath = fs.String("pass-file-path", "", "The path to the Postgres pass file")
backupScriptPath = fs.String("backup-script-path", "", "The path to the backup script that should be executed")
fromMailAddress = fs.String("from-email-address", "", "The email address we are sending the notification from, make sure it's set up in Postmark")
toMailAddress = fs.String("to-email-address", "", "The email address we are sending the notification from, make sure it's set up in Postmark")
)
if err := ff.Parse(fs, os.Args[1:],
ff.WithEnvVars(),
); err != nil {
level.Error(l).Log("msg", "error parsing flags", "err", err)
return
}
if *configFilePath == "" || *passFilePath == "" || *backupScriptPath == "" {
level.Error(l).Log("msg", "missing backup environment values for config-file-path, backup-script-path or pass-file-path.")
return
}
if *postmarkToken == "" || *postmarkTemplateID == 0 {
level.Error(l).Log("msg", "missing postmark token or template id")
return
}
if *backupPath == "" {
level.Error(l).Log("msg", "missing backup path")
return
}
if *fromMailAddress == "" || *toMailAddress == "" {
level.Error(l).Log("msg", "missing from or to mail address to send notification emails")
return
}
client := postmark.NewClient(
postmark.WithClient(&http.Client{
Transport: &postmark.AuthTransport{Token: *postmarkToken},
}),
)
_, err := executeBackupScript(l, *passFilePath, *configFilePath, *backupScriptPath)
if err != nil {
level.Error(l).Log("msg", "error running backup", "err", err)
return
}
history, exists, err := checkBackupHistory(*backupPath)
if err != nil {
level.Error(l).Log("msg", "error checking backup history", "err", err)
return
}
if exists {
level.Info(l).Log("msg", "successfully checked backup, send out health status message")
if err := sendEmail(client, *fromMailAddress, *toMailAddress, *postmarkTemplateID, history); err != nil {
level.Error(l).Log("msg", "error sending email", "err", err)
return
}
return
}
level.Info(l).Log("msg", "backup does not exist, no health status message sent")
}