-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.go
executable file
·70 lines (61 loc) · 1.83 KB
/
backup.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
// Copyright 2015-2016 Zack Scholl. All rights reserved.
// Use of this source code is governed by a AGPL
// license that can be found in the LICENSE file.
// backup.go contains functions for dumping a backup database.
package main
import (
"log"
"os"
"path"
"github.com/boltdb/bolt"
)
func dumpFingerprints(group string) error {
// Debug.Println("Making dump-" + group + " directory")
err := os.MkdirAll(path.Join(RuntimeArgs.SourcePath, "dump-"+group), 0777)
if err != nil {
return err
}
// Debug.Println("Opening db")
db, err := bolt.Open(path.Join(RuntimeArgs.SourcePath, group+".db"), 0664, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Debug.Println("Opening file for learning fingerprints")
// Debug.Println(path.Join(RuntimeArgs.SourcePath, "dump-"+group, "learning"))
f, err := os.OpenFile(path.Join(RuntimeArgs.SourcePath, "dump-"+group, "learning"), os.O_WRONLY|os.O_CREATE, 0664)
if err != nil {
return err
}
// Debug.Println("Writing fingerprints to file")
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("fingerprints"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
if _, err = f.WriteString(string(decompressByte(v)) + "\n"); err != nil {
panic(err)
}
}
return nil
})
f.Close()
// Debug.Println("Opening file for tracking fingerprints")
f, err = os.OpenFile(path.Join(RuntimeArgs.SourcePath, "dump-"+group, "tracking"), os.O_WRONLY|os.O_CREATE, 0664)
if err != nil {
return err
}
// Debug.Println("Writing fingerprints to file")
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("fingerprints-track"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
if _, err = f.WriteString(string(decompressByte(v)) + "\n"); err != nil {
panic(err)
}
}
return nil
})
f.Close()
// Debug.Println("Returning")
return nil
}