-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrvUnifiBackup.go
185 lines (152 loc) · 4.53 KB
/
srvUnifiBackup.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
package opnborg
import (
"bytes"
"crypto/sha256"
"crypto/tls"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"time"
)
// perform unifi backup
func srvUnifiBackup(config *OPNCall) {
// setup
server, notice := config.Unifi.WebUI.Hostname(), ""
displayChan <- []byte("[UNIFI][BACKUP][START][CONTROLLER] " + server)
// setup session
jar, err := cookiejar.New(nil)
if err != nil {
displayChan <- []byte("[UNIFI][BACKUP][ERROR][UNABLE-TO-SETUP-COOKIE-JAR]" + err.Error())
return // unrecoverable
}
// setup tls secure transport
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := http.Client{
Jar: jar,
Transport: transport,
}
// prep login
login := map[string]string{"username": config.Unifi.Backup.User, "password": config.Unifi.Backup.Secret}
postLogin, err := json.Marshal(login)
if err != nil {
displayChan <- []byte("[UNIFI][BACKUP][ERROR][CREDENTIALS-JSON-ENCODING-FAIL]" + err.Error())
return // unrecoverable
}
// prep system test
system := map[string]interface{}{"cmd": "async-backup", "days": 0}
postSystem, err := json.Marshal(system)
if err != nil {
displayChan <- []byte("[UNIFI][BACKUP][ERROR][CONFIG-SYSTEM-TEST-JSON-ENCODING-FAIL]" + err.Error())
return // unrecoverable
}
// init
ts := time.Now()
isReachable, backupOK, notice := true, false, ""
// enfore init backup
unifiBackupNow.Store(true)
// loop forever
for {
// reset default state
isReachable, backupOK, notice = true, false, "status:ok"
// perform authentication
res, err := client.Post(config.Unifi.WebUI.String()+"/api/login", "application/json", bytes.NewBuffer(postLogin))
if err != nil {
isReachable = false
notice = "[UNIFI][BACKUP][ERROR][UNABLE-TO-AUTENTHICATE]" + err.Error()
displayChan <- []byte(notice)
}
// was authentication ok?
if isReachable {
// check http status code
if res.StatusCode != 200 {
isReachable = false
body, _ := ioutil.ReadAll(res.Body)
notice = "[UNIFI][BACKUP][ERROR][UNABLE-TO-AUTENTHICATE][BODY] "
displayChan <- []byte(notice)
displayChan <- body
}
// was authentication and status code ok?
if isReachable {
// perform actual fetch test
res, err = client.Post(config.Unifi.WebUI.String()+"/api/s/default/cmd/system", "application/json", bytes.NewBuffer(postSystem))
if err != nil {
isReachable = false
notice = "[UNIFI][BACKUP][ERROR][CONFIG-DOWNLOAD-FAIL] " + err.Error()
displayChan <- []byte(notice)
}
if isReachable {
// was fetch sucessfull, check http code
if res.StatusCode != 200 {
isReachable = false
notice = "[UNIFI][BACKUP][ERROR][CONFIG-DOWNLOAD-FAIL][BODY] "
body, _ := ioutil.ReadAll(res.Body)
displayChan <- []byte(notice)
displayChan <- body
}
}
}
}
// if reachable, proceed with backup
if isReachable {
// if last backup > 6 hours
if time.Now().Sub(ts) < time.Duration(6*time.Hour) {
unifiBackupNow.Store(true)
}
// perform backup
if unifiBackupNow.Load() {
// reset unifiBackupNow
unifiBackupNow.Store(false)
// update timestamp
ts = time.Now()
// setup
backupOK = true
// download backup file
res, err = client.Get(config.Unifi.WebUI.String() + "/dl/backup/" + config.Unifi.Version + ".unf")
if err != nil {
backupOK = false
notice = "[UNIFI][BACKUP][ERROR][BACKUP-DOWNLOAD-FILE-HEAD-FAIL] " + err.Error()
displayChan <- []byte(notice)
}
defer res.Body.Close()
// proceed
if backupOK {
// read body
unf, err := io.ReadAll(res.Body)
if err != nil {
backupOK = false
notice = "[UNIFI][BACKUP][ERROR][BACKUP-DOWNLOAD-FILE-BODY-FAIL] " + err.Error()
displayChan <- []byte(notice)
}
// check file
if backupOK {
if len(unf) < 1024 {
backupOK = false
notice = "[UNIFI][BACKUP][ERROR][BACKUP-DOWNLOAD-FILE-TO-SMALL] " + err.Error()
displayChan <- []byte(notice)
}
// check into store
if backupOK {
// check into store
checkIntoStore(config, config.Unifi.WebUI.Hostname(), "unf", unf, ts, sha256.Sum256(unf))
// flag git store as dirty
config.dirty.Store(true)
// notify
displayChan <- []byte("[UNIFI][BACKUP][SUCCESSFUL]")
}
}
}
displayChan <- []byte("[UNIFI][BACKUP][END]")
}
}
// set unifi status
setUnifiStatus(config, server, config.Unifi.Tag, notice, time.Now(), isReachable, backupOK)
// wait for next round trigger
<-updateUnifiBackup
}
}