-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.go
190 lines (164 loc) · 5.31 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
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
// This file is part of the happyDomain (R) project.
// Copyright (c) 2020-2024 happyDomain
// Authors: Pierre-Olivier Mercier, et al.
//
// This program is offered under a commercial and under the AGPL license.
// For commercial licensing, contact us at <[email protected]>.
//
// For AGPL licensing:
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package admin
import (
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
"go.uber.org/multierr"
"git.happydns.org/happyDomain/config"
"git.happydns.org/happyDomain/model"
"git.happydns.org/happyDomain/storage"
)
func declareBackupRoutes(opts *config.Options, router *gin.RouterGroup) {
router.POST("/backup.json", backupJSON)
router.PUT("/backup.json", restoreJSON)
}
type happyBackup struct {
Domains happydns.Domains
DomainsLogs map[string][]*happydns.DomainLog
Errors []string
Providers []*happydns.ProviderMessage
Sessions []*happydns.Session
Users []*happydns.User
UsersAuth happydns.UserAuths
Zones []*happydns.Zone
}
func DoBackup() (ret happyBackup) {
ret.DomainsLogs = map[string][]*happydns.DomainLog{}
// UserAuth
uas, err := storage.MainStore.GetAuthUsers()
if err != nil {
ret.Errors = append(ret.Errors, fmt.Sprintf("unable to retrieve AuthUsers: %s", err.Error()))
} else {
ret.UsersAuth = uas
}
// Users
us, err := storage.MainStore.GetUsers()
if err != nil {
ret.Errors = append(ret.Errors, fmt.Sprintf("unable to retrieve Users: %s", err.Error()))
} else {
ret.Users = us
for _, u := range us {
// Domains
ds, err := storage.MainStore.GetDomains(u)
if err != nil {
ret.Errors = append(ret.Errors, fmt.Sprintf("unable to retrieve Domain names of %s (%s): %s", u.Id.String(), u.Email, err.Error()))
} else {
ret.Domains = append(ret.Domains, ds...)
for _, dn := range ds {
// Domain logs
ls, err := storage.MainStore.GetDomainLogs(dn)
if err != nil {
ret.Errors = append(ret.Errors, fmt.Sprintf("unable to retrieve domain's logs %s/%s (%s): %s", u.Id.String(), dn.Id.String(), dn.DomainName, err.Error()))
} else {
ret.DomainsLogs[dn.Id.String()] = ls
}
// Zones
for _, zid := range dn.ZoneHistory {
z, err := storage.MainStore.GetZone(zid)
if err != nil {
ret.Errors = append(ret.Errors, fmt.Sprintf("unable to retrieve domain's zone %s/%s (%s): zoneid=%s: %s", u.Id.String(), dn.Id.String(), dn.DomainName, zid.String(), err.Error()))
} else {
ret.Zones = append(ret.Zones, z)
}
}
}
}
// Providers
ps, err := storage.MainStore.GetProviders(u)
if err != nil {
ret.Errors = append(ret.Errors, fmt.Sprintf("unable to retrieve Providers: %s", err.Error()))
} else {
ret.Providers = append(ret.Providers, ps...)
}
// Sessions
ss, err := storage.MainStore.GetUserSessions(u)
if err != nil {
ret.Errors = append(ret.Errors, fmt.Sprintf("unable to retrieve Sessions: %s", err.Error()))
} else {
ret.Sessions = append(ret.Sessions, ss...)
}
}
}
return
}
func DoRestore(backup *happyBackup) (errs error) {
// UserAuth
for _, ua := range backup.UsersAuth {
errs = multierr.Combine(errs, storage.MainStore.UpdateAuthUser(ua))
}
// Users
for _, user := range backup.Users {
err := storage.MainStore.CreateOrUpdateUser(user)
if err != nil {
errs = multierr.Combine(errs, err)
}
}
// Providers
for _, provider := range backup.Providers {
p, err := provider.ParseProvider()
if err != nil {
errs = multierr.Combine(errs, err)
}
errs = multierr.Combine(errs, storage.MainStore.UpdateProvider(p))
}
// Domains
for _, domain := range backup.Domains {
err := storage.MainStore.UpdateDomain(domain)
if err != nil {
errs = multierr.Combine(errs, err)
} else {
// Domain logs
for _, log := range backup.DomainsLogs[domain.Id.String()] {
errs = multierr.Combine(errs, storage.MainStore.UpdateDomainLog(domain, log))
}
}
}
// Zones
for _, zone := range backup.Zones {
errs = multierr.Combine(errs, storage.MainStore.UpdateZone(zone))
}
// Sessions
for _, session := range backup.Sessions {
errs = multierr.Combine(errs, storage.MainStore.UpdateSession(session))
}
return
}
func backupJSON(c *gin.Context) {
c.JSON(http.StatusOK, DoBackup())
}
func restoreJSON(c *gin.Context) {
var backup happyBackup
err := c.ShouldBindJSON(&backup)
if err != nil {
log.Printf("%s sends invalid Backup JSON: %s", c.ClientIP(), err.Error())
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Something is wrong in received data: %s", err.Error())})
return
}
err = DoRestore(&backup)
if err == nil {
c.JSON(http.StatusOK, true)
} else {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errors": err})
}
}