-
Notifications
You must be signed in to change notification settings - Fork 0
/
borg.go
125 lines (100 loc) · 2.73 KB
/
borg.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
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"github.com/labstack/gommon/log"
"os"
"os/exec"
"strings"
"time"
)
const BorgRepoDetectionString = "This is a Borg Backup repository."
type BorgRepo struct {
borgBin string
path string
key string
lastBackupTime time.Time
lastBackupId string
logger *log.Logger
}
type BorgBackup struct {
Archive string `json:"archive"`
BArchive string `json:"barchive"`
Id string `json:"id"`
Name string `json:"name"`
Start string `json:"start"`
Time string `json:"time"`
}
type BorgEncryption struct {
Mode string `json:"mode"`
}
type BorgRepository struct {
Id string `json:"id"`
LastModified string `json:"last_modified"`
Location string `json:"location"`
}
type BorgBackupList struct {
Archives []BorgBackup `json:"archives"`
Encryption BorgEncryption `json:"encryption"`
Repository BorgRepository `json:"repository"`
}
func (b *BorgRepo) GetLastBorgBackupTime() (time.Time, error) {
err := os.Setenv(EnvVariableBorgPassphrase, b.key)
defer os.Unsetenv(EnvVariableBorgPassphrase)
if err != nil {
b.logger.Errorf("%v", err)
return time.Time{}, err
}
borgOut, err := exec.Command(b.borgBin, "list", "--json", b.path).Output()
b.logger.Debugf("%v list --json %v", b.borgBin, b.path)
if err != nil {
b.logger.Errorf("%v", err)
b.logger.Debugf("%v", borgOut)
return time.Time{}, err
}
backupOut := BorgBackupList{}
err = json.Unmarshal(borgOut, &backupOut)
if err != nil {
b.logger.Errorf("%v", err)
return time.Time{}, err
}
b.lastBackupTime, err = ParseTimeInCurrentLocation(backupOut.Repository.LastModified)
if err != nil {
b.logger.Errorf("%v", err)
return time.Time{}, err
}
return b.lastBackupTime, nil
}
func (b *BorgRepo) IsLastBackupEarlierThen(seconds int) (bool, error) {
lastBackupTime, err := b.GetLastBorgBackupTime()
if err != nil {
return false, err
}
now := time.Now()
passedDuration := time.Duration(now.Unix() - lastBackupTime.Unix()) * time.Second
if passedDuration > time.Duration(seconds) * time.Second {
return false, nil
}
return true, nil
}
func NewBorgRepo(path string, borgBin string, key string, logger *log.Logger) (*BorgRepo, error) {
if ok, err := IsValidBorgRepo(path); !ok {
return &BorgRepo{}, err
}
return &BorgRepo{path: path, borgBin: borgBin, key: key, logger: logger}, nil
}
func IsValidBorgRepo(path string) (bool, error) {
readmefile, err := os.Open(fmt.Sprintf("%v/README", path))
if err != nil {
return false, err
}
scanner := bufio.NewScanner(readmefile)
scanner.Scan()
line := scanner.Text()
if !strings.Contains(line, BorgRepoDetectionString) {
return false, errors.New(fmt.Sprintf("%v not contains %v", line, BorgRepoDetectionString))
}
return true, nil
}