-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
255 lines (210 loc) · 4.62 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
package main
import (
"bufio"
"context"
"errors"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"time"
"github.com/coreos/go-systemd/v22/daemon"
"github.com/go-co-op/gocron/v2"
"golang.org/x/term"
"github.com/theandrew168/pg2s3/internal/config"
"github.com/theandrew168/pg2s3/internal/pg2s3"
)
func main() {
code := 0
err := run()
if err != nil {
fmt.Println(err)
code = 1
}
os.Exit(code)
}
func run() error {
conf := flag.String("conf", "pg2s3.conf", "pg2s3 config file")
flag.Parse()
cfg, err := config.ReadFile(*conf)
if err != nil {
return err
}
client, err := pg2s3.NewClient(cfg)
if err != nil {
return err
}
// check for action (default run)
args := flag.Args()
var action string
if len(args) == 0 {
action = "run"
} else {
action = args[0]
}
// backup: create a new backup
if action == "backup" {
return backup(client, cfg)
}
// restore: restore the most recent backup
if action == "restore" {
return restore(client, cfg)
}
// prune: delete the oldest backups above the retention count
if action == "prune" {
return prune(client, cfg)
}
if cfg.Backup.Schedule == "" {
// TODO: replace with logging
fmt.Println("no backup schedule specified, exiting")
return nil
}
s, err := gocron.NewScheduler(
gocron.WithLocation(time.UTC),
)
if err != nil {
return err
}
_, err = s.NewJob(
gocron.CronJob(cfg.Backup.Schedule, false),
gocron.NewTask(func() {
err := backup(client, cfg)
if err != nil {
// TODO: replace with logging
fmt.Println(err)
return
}
err = prune(client, cfg)
if err != nil {
// TODO: replace with logging
fmt.Println(err)
return
}
}),
)
if err != nil {
return err
}
// let systemd know that we are good to go (no-op if not using systemd)
daemon.SdNotify(false, daemon.SdNotifyReady)
// TODO: replace with logging
fmt.Printf("running on schedule: %s\n", cfg.Backup.Schedule)
// create a context that cancels upon receiving an interrupt signal
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
s.Start()
<-ctx.Done()
err = s.Shutdown()
if err != nil {
return err
}
return nil
}
func confirm(message string) bool {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s [y/n]: ", message)
response, err := reader.ReadString('\n')
if err != nil {
fmt.Println(err)
return false
}
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
} else {
return false
}
}
func backup(client *pg2s3.Client, cfg config.Config) error {
// generate name for backup
name, err := pg2s3.GenerateBackupName(cfg.Backup.Prefix)
if err != nil {
return err
}
// create backup
backup, err := client.CreateBackup()
if err != nil {
return err
}
// encrypt backup (if applicable)
if len(cfg.Encryption.PublicKeys) > 0 {
backup, err = client.EncryptBackup(backup, cfg.Encryption.PublicKeys)
if err != nil {
return err
}
name = name + ".age"
}
// upload backup
err = client.UploadBackup(name, backup)
if err != nil {
return err
}
fmt.Printf("created %s\n", name)
return nil
}
func restore(client *pg2s3.Client, cfg config.Config) error {
// list all backups
backups, err := client.ListBackups()
if err != nil {
return err
}
if len(backups) == 0 {
return errors.New("no backups present to restore")
}
// determine latest backup
latest := backups[0]
// download backup
backup, err := client.DownloadBackup(latest)
if err != nil {
return err
}
// decrypt backup (if applicable)
if len(cfg.Encryption.PublicKeys) > 0 {
fmt.Print("enter private key: ")
input, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return err
}
fmt.Println()
privateKey := string(input)
backup, err = client.DecryptBackup(backup, privateKey)
if err != nil {
return err
}
}
// confirm restore before applying
message := fmt.Sprintf("restore %s", latest)
if !confirm(message) {
return nil
}
// restore backup
err = client.RestoreBackup(backup)
if err != nil {
return err
}
fmt.Printf("restored %s\n", latest)
return nil
}
func prune(client *pg2s3.Client, cfg config.Config) error {
// list all backups
backups, err := client.ListBackups()
if err != nil {
return err
}
// exit early if retention is zero or limit hasn't been reached
if cfg.Backup.Retention <= 0 || len(backups) <= cfg.Backup.Retention {
return nil
}
// determine expired backups to prune
expired := backups[cfg.Backup.Retention:]
// prune old backups
for _, backup := range expired {
err = client.DeleteBackup(backup)
if err != nil {
return err
}
fmt.Printf("deleted %s\n", backup)
}
return nil
}