-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
472 lines (377 loc) · 12 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package main
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"hash"
"io"
"os"
"runtime"
"strings"
"sync/atomic"
"time"
"github.com/cornelk/hashmap"
"github.com/gen2brain/beeep"
"github.com/getlantern/systray"
"github.com/tawesoft/golib/v2/dialog"
)
var defaultMailSubjectTemplate = "Backup {{.Status}}"
var defaultMailBodyTemplate = `{{if .Success}}Backup complete ({{.FromattedDuration}})
Chunks New {{.NewChunks}}, Reused {{.ReusedChunks}}.{{else}}Error occurred while working, backup may be not completed.
Last error is: {{.ErrorStr}}{{end}}`
var didxMagic = []byte{28, 145, 78, 165, 25, 186, 179, 205}
type ChunkState struct {
assignments []string
assignments_offset []uint64
pos uint64
wrid uint64
chunkcount uint64
chunkdigests hash.Hash
current_chunk []byte
C Chunker
newchunk *atomic.Uint64
reusechunk *atomic.Uint64
knownChunks *hashmap.Map[string, bool]
}
type DidxEntry struct {
offset uint64
digest []byte
}
func (c *ChunkState) Init(newchunk *atomic.Uint64 , reusechunk *atomic.Uint64, knownChunks *hashmap.Map[string, bool] ) {
c.assignments = make([]string, 0)
c.assignments_offset = make([]uint64, 0)
c.pos = 0
c.chunkcount = 0
c.chunkdigests = sha256.New()
c.current_chunk = make([]byte, 0)
c.C = Chunker{}
c.C.New(1024 * 1024 * 4)
c.reusechunk = reusechunk
c.newchunk = newchunk
c.knownChunks = knownChunks
}
func (c *ChunkState) HandleData(b []byte, client *PBSClient){
chunkpos := c.C.Scan(b)
if chunkpos == 0 {
//No break happened, just append data
c.current_chunk = append(c.current_chunk, b...)
} else {
for chunkpos > 0 {
//Append data until break position
c.current_chunk = append(c.current_chunk, b[:chunkpos]...)
h := sha256.New()
// TODO: error handling inside callback
h.Write(c.current_chunk)
bindigest := h.Sum(nil)
shahash := hex.EncodeToString(bindigest)
if _, ok := c.knownChunks.GetOrInsert(shahash, true); !ok {
fmt.Printf("New chunk[%s] %d bytes\n", shahash, len(c.current_chunk))
c.newchunk.Add(1)
client.UploadCompressedChunk(c.wrid, shahash, c.current_chunk)
} else {
fmt.Printf("Reuse chunk[%s] %d bytes\n", shahash, len(c.current_chunk))
c.reusechunk.Add(1)
}
// TODO: error handling inside callback
binary.Write(c.chunkdigests, binary.LittleEndian, (c.pos + uint64(len(c.current_chunk))))
// TODO: error handling inside callback
c.chunkdigests.Write(h.Sum(nil))
c.assignments_offset = append(c.assignments_offset, c.pos)
c.assignments = append(c.assignments, shahash)
c.pos += uint64(len(c.current_chunk))
c.chunkcount += 1
c.current_chunk = make([]byte, 0)
b = b[chunkpos:] //Take remainder of data
chunkpos = c.C.Scan(b)
}
//No further break happened, append remaining data
c.current_chunk = append(c.current_chunk, b...)
}
}
func (c *ChunkState) Eof(client *PBSClient) {
//Here we write the remainder of data for which cyclic hash did not trigger
if len(c.current_chunk) > 0 {
h := sha256.New()
_, err := h.Write(c.current_chunk)
if err != nil {
panic(err)
}
shahash := hex.EncodeToString(h.Sum(nil))
binary.Write(c.chunkdigests, binary.LittleEndian, (c.pos + uint64(len(c.current_chunk))))
c.chunkdigests.Write(h.Sum(nil))
if _, ok := c.knownChunks.GetOrInsert(shahash, true); !ok {
fmt.Printf("New chunk[%s] %d bytes\n", shahash, len(c.current_chunk))
client.UploadCompressedChunk(c.wrid, shahash, c.current_chunk)
c.newchunk.Add(1)
} else {
fmt.Printf("Reuse chunk[%s] %d bytes\n", shahash, len(c.current_chunk))
c.reusechunk.Add(1)
}
c.assignments_offset = append(c.assignments_offset, c.pos)
c.assignments = append(c.assignments, shahash)
c.pos += uint64(len(c.current_chunk))
c.chunkcount += 1
}
//Avoid incurring in request entity too large by chunking assignment PUT requests in blocks of at most 128 chunks
for k := 0; k < len(c.assignments); k += 128 {
k2 := k + 128
if k2 > len(c.assignments) {
k2 = len(c.assignments)
}
client.AssignChunks(c.wrid, c.assignments[k:k2], c.assignments_offset[k:k2])
}
client.CloseDynamicIndex(c.wrid, hex.EncodeToString(c.chunkdigests.Sum(nil)), c.pos, c.chunkcount)
}
func main() {
var newchunk *atomic.Uint64 = new(atomic.Uint64)
var reusechunk *atomic.Uint64 = new(atomic.Uint64)
cfg := loadConfig()
if ok := cfg.valid(); !ok {
if runtime.GOOS == "windows" {
usage := "All options are mandatory:\n"
flag.VisitAll(func(f *flag.Flag) {
usage += "-" + f.Name + " " + f.Usage + "\n"
})
dialog.Error(usage)
} else {
fmt.Println("All options are mandatory")
flag.PrintDefaults()
}
os.Exit(1)
}
L := Locking{}
lock_ok := L.AcquireProcessLock()
if !lock_ok {
dialog.Error("Backup jobs need to run exclusively, please wait until the previous job has finished")
os.Exit(2)
}
defer L.ReleaseProcessLock()
if runtime.GOOS == "windows" {
go systray.Run(func() {
systray.SetIcon(ICON)
systray.SetTooltip("PBSGO Backup running")
beeep.Notify("Proxmox Backup Go", "Backup started", "")
},
func() {
})
}
insecure := cfg.CertFingerprint != ""
client := &PBSClient{
baseurl: cfg.BaseURL,
certfingerprint: cfg.CertFingerprint, //"ea:7d:06:f9:87:73:a4:72:d0:e8:05:a4:b3:3d:95:d7:0a:26:dd:6d:5c:ca:e6:99:83:e4:11:3b:5f:10:f4:4b",
authid: cfg.AuthID,
secret: cfg.Secret,
datastore: cfg.Datastore,
namespace: cfg.Namespace,
insecure: insecure,
manifest: BackupManifest{
BackupID: cfg.BackupID,
},
}
hostname, err := os.Hostname()
if err != nil {
fmt.Println("Failed to retrieve hostname:", err)
hostname = "unknown"
}
begin := time.Now()
if cfg.BackupSourceDir != "" {
err = backup(client, newchunk, reusechunk, cfg.PxarOut, cfg.BackupSourceDir)
} else if cfg.BackupStreamName != "" {
sn := cfg.BackupStreamName
if ! strings.HasSuffix(sn, ".didx" ) {
sn += ".didx"
}
fmt.Printf("Backing up from STDIN to %s", sn)
err = backup_stream(client, newchunk, reusechunk, sn, os.Stdin )
}else{
panic("No backup dir or stream name specified, exiting")
}
end := time.Now()
mailCtx := mailCtx{
NewChunks: newchunk.Load(),
ReusedChunks: reusechunk.Load(),
Error: err,
Hostname: hostname,
Datastore: cfg.Datastore,
StartTime: begin,
EndTime: end,
}
mailBodyTemplate := defaultMailBodyTemplate
if cfg.SMTP != nil && cfg.SMTP.Template != nil && cfg.SMTP.Template.Body != "" {
mailBodyTemplate = cfg.SMTP.Template.Body
}
fmt.Printf("New %d, Reused %d, backup took %s.\n", newchunk.Load(), reusechunk.Load(), end.Sub(begin))
var msg string
msg, err = mailCtx.buildStr(mailBodyTemplate)
if err != nil {
fmt.Println("Cannot use custom mail body: " + err.Error())
msg, err = mailCtx.buildStr(defaultMailBodyTemplate)
if err != nil {
// this should never happen
panic(err)
}
}
if runtime.GOOS == "windows" {
systray.Quit()
beeep.Notify("Proxmox Backup Go", msg, "")
}
if cfg.SMTP != nil {
var subject string
mailSubjectTemplate := defaultMailSubjectTemplate
if cfg.SMTP.Template != nil && cfg.SMTP.Template.Subject != "" {
mailSubjectTemplate = cfg.SMTP.Template.Subject
}
subject, err = mailCtx.buildStr(mailSubjectTemplate)
if err != nil {
fmt.Println("Cannot use custom mail subject: " + err.Error())
msg, err = mailCtx.buildStr(defaultMailSubjectTemplate)
if err != nil {
// this should never happen
panic(err)
}
}
client, err := setupClient(cfg.SMTP.Host, cfg.SMTP.Port, cfg.SMTP.Username, cfg.SMTP.Password, cfg.SMTP.Insecure)
if err != nil {
fmt.Println("Cannot connect to mail server: " + err.Error())
os.Exit(1)
}
defer client.Quit()
for _, ccc := range cfg.SMTP.Mails {
err = sendMail(ccc.From, ccc.To, subject, msg, client)
if err != nil {
fmt.Println("Cannot send email: " + err.Error())
os.Exit(1)
}
}
}
}
func backup_stream(client *PBSClient, newchunk, reusechunk *atomic.Uint64, filename string, stream io.Reader ) error {
knownChunks := hashmap.New[string, bool]()
client.Connect(false)
previousDidx, err := client.DownloadPreviousToBytes(filename)
if err != nil {
return err
}
fmt.Printf("Downloaded previous DIDX: %d bytes\n", len(previousDidx))
if !bytes.HasPrefix(previousDidx, didxMagic) {
fmt.Printf("Previous index has wrong magic (%s)!\n", previousDidx[:8])
} else {
//Header as per proxmox documentation is fixed size of 4096 bytes,
//then offset of type uint64 and sha256 digests follow , so 40 byte each record until EOF
previousDidx = previousDidx[4096:]
for i := 0; i*40 < len(previousDidx); i += 1 {
e := DidxEntry{}
e.offset = binary.LittleEndian.Uint64(previousDidx[i*40 : i*40+8])
e.digest = previousDidx[i*40+8 : i*40+40]
shahash := hex.EncodeToString(e.digest)
fmt.Printf("Previous: %s\n", shahash)
knownChunks.Set(shahash, true)
}
}
fmt.Printf("Known chunks: %d!\n", knownChunks.Len())
streamChunk := ChunkState{}
streamChunk.Init(newchunk, reusechunk, knownChunks)
streamChunk.wrid, err = client.CreateDynamicIndex(filename)
if err != nil {
return err
}
B := make([]byte, 65536)
for {
n, err := stream.Read(B)
b := B[:n]
streamChunk.HandleData(b, client)
if err == io.EOF {
break
}
}
streamChunk.Eof(client)
client.CloseDynamicIndex(streamChunk.wrid, hex.EncodeToString(streamChunk.chunkdigests.Sum(nil)), streamChunk.pos, streamChunk.chunkcount)
err = client.UploadManifest()
if err != nil {
return err
}
return client.Finish()
}
func backup(client *PBSClient, newchunk, reusechunk *atomic.Uint64, pxarOut string, backupdir string) error {
knownChunks := hashmap.New[string, bool]()
fmt.Printf("Starting backup of %s\n", backupdir)
backupdir = createVSSSnapshot(backupdir)
//Remove VSS snapshot on windows, on linux for now NOP
defer VSSCleanup()
client.Connect(false)
archive := &PXARArchive{}
archive.archivename = "backup.pxar.didx"
previousDidx, err := client.DownloadPreviousToBytes(archive.archivename)
if err != nil {
return err
}
fmt.Printf("Downloaded previous DIDX: %d bytes\n", len(previousDidx))
/*f2, _ := os.Create("test.didx")
defer f2.Close()
f2.Write(previous_didx)*/
/*
Here we download the previous dynamic index to figure out which chunks are the same of what
we are going to upload to avoid unnecessary traffic and compression cpu usage
*/
if !bytes.HasPrefix(previousDidx, didxMagic) {
fmt.Printf("Previous index has wrong magic (%s)!\n", previousDidx[:8])
} else {
//Header as per proxmox documentation is fixed size of 4096 bytes,
//then offset of type uint64 and sha256 digests follow , so 40 byte each record until EOF
previousDidx = previousDidx[4096:]
for i := 0; i*40 < len(previousDidx); i += 1 {
e := DidxEntry{}
e.offset = binary.LittleEndian.Uint64(previousDidx[i*40 : i*40+8])
e.digest = previousDidx[i*40+8 : i*40+40]
shahash := hex.EncodeToString(e.digest)
fmt.Printf("Previous: %s\n", shahash)
knownChunks.Set(shahash, true)
}
}
fmt.Printf("Known chunks: %d!\n", knownChunks.Len())
f := &os.File{}
if pxarOut != "" {
f, err = os.Create(pxarOut)
if err != nil {
return err
}
defer f.Close()
}
/**/
pxarChunk := ChunkState{}
pxarChunk.Init(newchunk, reusechunk, knownChunks)
pcat1Chunk := ChunkState{}
pcat1Chunk.Init(newchunk, reusechunk, knownChunks)
pxarChunk.wrid, err = client.CreateDynamicIndex(archive.archivename)
if err != nil {
return err
}
pcat1Chunk.wrid, err = client.CreateDynamicIndex("catalog.pcat1.didx")
if err != nil {
return err
}
archive.writeCB = func(b []byte) {
if pxarOut != "" {
// TODO: error handling inside callback
f.Write(b)
}
pxarChunk.HandleData(b, client)
//
}
archive.catalogWriteCB = func(b []byte) {
pcat1Chunk.HandleData(b, client)
}
//This is the entry point of backup job which will start streaming with the PCAT and PXAR write callback
//Data to be hashed and eventuall uploaded
archive.WriteDir(backupdir, "", true)
pxarChunk.Eof(client)
pcat1Chunk.Eof(client)
err = client.UploadManifest()
if err != nil {
return err
}
return client.Finish()
}