-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbackup_restore.go
233 lines (199 loc) · 6.99 KB
/
backup_restore.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
package controllers
import (
"net/http"
// "sort"
// "strconv"
"strings"
// "time"
"fmt"
// . "github.com/ahmetalpbalkan/go-linq"
// log "github.com/sirupsen/logrus"
// ast "github.com/aerospike/aerospike-client-go/v5/types"
"github.com/aerospike-community/amc/common"
"github.com/labstack/echo/v4"
)
func postInitiateBackup(c echo.Context) error {
clusterUUID := c.Param("clusterUUID")
cluster := _observer.FindClusterByID(clusterUUID)
if cluster == nil {
return c.JSON(http.StatusOK, errorMap("Cluster not found"))
}
form := struct {
Namespace string `form:"namespace"`
DestinationNodeAddress string `form:"destination_node_address"`
DestinationLocation string `form:"destination_location"`
Username string `form:"username"`
Password string `form:"password"`
Sets string `form:"sets"`
OnlyMetadata bool `form:"only_metadata"`
TerminateOnChange bool `form:"terminate_on_change"`
ScanPriority int `form:"scan_priority"`
ModifiedBefore string `form:"modified_before"`
ModifiedAfter string `form:"modified_after"`
}{}
c.Bind(&form)
if len(form.Namespace) == 0 {
return c.JSON(http.StatusOK, errorMap("Invalid Namespace"))
}
if len(form.DestinationNodeAddress) == 0 {
return c.JSON(http.StatusOK, errorMap("Invalid DestinationNodeAddress"))
}
if len(form.DestinationLocation) == 0 {
return c.JSON(http.StatusOK, errorMap("Invalid DestinationLocation"))
}
if len(form.ModifiedBefore) > 0 {
if _, err := common.ParseTimeStrict("2006-01-02_15:04:05", form.ModifiedBefore); err != nil {
return c.JSON(http.StatusOK, errorMap("Invalid Modified Before Date: "+err.Error()))
}
}
if len(form.ModifiedAfter) > 0 {
if _, err := common.ParseTimeStrict("2006-01-02_15:04:05", form.ModifiedAfter); err != nil {
return c.JSON(http.StatusOK, errorMap("Invalid Modified After Date: "+err.Error()))
}
}
backup, err := cluster.Backup(
form.Namespace,
form.DestinationNodeAddress,
form.DestinationLocation,
form.Username,
form.Password,
form.Sets,
form.OnlyMetadata,
form.TerminateOnChange,
form.ModifiedBefore,
form.ModifiedAfter,
form.ScanPriority)
if err != nil {
return c.JSON(http.StatusOK, errorMap(err.Error()))
}
return c.JSON(http.StatusOK, map[string]interface{}{
"backup_id": backup.ID,
"status": strings.ToLower(string(backup.Status)),
})
}
func getBackupProgress(c echo.Context) error {
clusterUUID := c.Param("clusterUUID")
cluster := _observer.FindClusterByID(clusterUUID)
if cluster == nil {
return c.JSON(http.StatusOK, errorMap("Cluster not found"))
}
backup := cluster.CurrentBackup()
if backup == nil {
return c.JSON(http.StatusOK, map[string]interface{}{})
}
return c.JSON(http.StatusOK, map[string]interface{}{
backup.ID: map[string]interface{}{
"destination_location": backup.DestinationPath,
"destination_node_address": backup.DestinationAddress,
"namespace": backup.Namespace,
"progress": map[string]interface{}{
"percentage": fmt.Sprintf("%d%%", backup.Progress),
"status": strings.ToLower(string(backup.Status)),
},
},
})
}
func getSuccessfulBackups(c echo.Context) error {
clusterUUID := c.Param("clusterUUID")
cluster := _observer.FindClusterByID(clusterUUID)
if cluster == nil {
return c.JSON(http.StatusOK, errorMap("Cluster not found"))
}
backupList, err := common.SuccessfulBackups()
if err != nil {
return c.JSON(http.StatusOK, errorMap("Error reading backup list from the database: "+err.Error()))
}
res := make([]interface{}, 0, len(backupList))
for _, backup := range backupList {
res = append(res, map[string]interface{}{
"destination_location": backup.DestinationPath + fmt.Sprintf("/backup_%s_%s", backup.Namespace, backup.Created.Format("2006-01-02_15:04:05")),
"destination_node_address": backup.DestinationAddress,
"namespace": backup.Namespace,
"only_metadata": backup.MetadataOnly,
"sets": backup.Sets,
})
}
return c.JSON(http.StatusOK, map[string]interface{}{
"successful_backups": res,
})
}
func getAvailableBackups(c echo.Context) error {
clusterUUID := c.Param("clusterUUID")
cluster := _observer.FindClusterByID(clusterUUID)
if cluster == nil {
return c.JSON(http.StatusOK, errorMap("Cluster not found"))
}
form := struct {
DestinationNodeAddress string `form:"source_node_address"`
DestinationLocation string `form:"source_location"`
}{}
c.Bind(&form)
backupList, err := common.SuccessfulBackups()
if err != nil {
return c.JSON(http.StatusOK, errorMap("Error reading backup list from the database: "+err.Error()))
}
res := make([]interface{}, 0, len(backupList))
for _, backup := range backupList {
name := backup.DestinationPath + fmt.Sprintf("/backup_%s_%s", backup.Namespace, backup.Created.Format("2006-01-02_15:04:05"))
if backup.DestinationAddress == form.DestinationNodeAddress && strings.HasPrefix(name, form.DestinationLocation) {
res = append(res, fmt.Sprintf("backup_%s_%s", backup.Namespace, backup.Created.Format("2006-01-02_15:04:05")))
}
}
return c.JSON(http.StatusOK, map[string]interface{}{
"available_backups": res,
"status": "Success",
})
}
func postInitiateRestore(c echo.Context) error {
clusterUUID := c.Param("clusterUUID")
cluster := _observer.FindClusterByID(clusterUUID)
if cluster == nil {
return c.JSON(http.StatusOK, errorMap("Cluster not found"))
}
form := struct {
Namespace string `form:"namespace"`
DestinationNodeAddress string `form:"source_node_address"`
DestinationLocation string `form:"source_location"`
Username string `form:"username"`
Password string `form:"password"`
Threads int `form:"threads"`
MissingRecordOnly bool `form:"missing_records_only"`
IgnoreGenerationNumber bool `form:"ignore_generation_num"`
}{}
c.Bind(&form)
if len(form.DestinationNodeAddress) == 0 {
return c.JSON(http.StatusOK, errorMap("Invalid DestinationNodeAddress"))
}
if len(form.DestinationLocation) == 0 {
return c.JSON(http.StatusOK, errorMap("Invalid DestinationLocation"))
}
restore, err := cluster.Restore(
form.Namespace,
form.DestinationNodeAddress,
form.DestinationLocation,
form.Username,
form.Password,
form.Threads,
form.MissingRecordOnly,
form.IgnoreGenerationNumber)
if err != nil {
return c.JSON(http.StatusOK, errorMap(err.Error()))
}
return c.JSON(http.StatusOK, map[string]interface{}{
"status": strings.ToLower(string(restore.Status)),
})
}
func getRestoreProgress(c echo.Context) error {
clusterUUID := c.Param("clusterUUID")
cluster := _observer.FindClusterByID(clusterUUID)
if cluster == nil {
return c.JSON(http.StatusOK, errorMap("Cluster not found"))
}
restore := cluster.CurrentRestore()
if restore == nil {
return c.JSON(http.StatusOK, errorMap("No Restore in progress"))
}
return c.JSON(http.StatusOK, map[string]interface{}{
"status": strings.ToLower(string(restore.Status)),
})
}