-
Notifications
You must be signed in to change notification settings - Fork 9
/
backup.jps
402 lines (369 loc) · 13.9 KB
/
backup.jps
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
type: update
jpsVersion: 6.1.1
name: Database Backup/Restore for the filesystem and the databases
id: db-backup
targetEditions: any
logo: /images/backup-logo.png
description: Backup Add-On for the database. It can be used to create scheduled backups according to any required timezone and restore corrupted databases, even if the content has been completely deleted.
baseUrl: https://raw.githubusercontent.com/jelastic-jps/database-backup-addon/master
targetNodes:
nodeType:
- redis
- redis7
- redis6
- postgresql
- postgres15
- postgres14
- postgres13
- postgres12
- postgres11
- mysql
- mysql5
- mysql8
- mariadb-dockerized
- mariadb10
- mariadb11
- mongodb-dockerized
- mongodb
- mongo
- perconadb
- percona8
- percona5
settings:
main:
fields:
- type: radio-fieldset
name: scheduleType
hidden: false
default: '1'
values:
- value: 1
caption: Pre-defined
- value: 2
caption: Custom
- value: 3
caption: Manual (crontab)
tooltip: "<span>A simple <a href=\"https://en.wikipedia.org/wiki/Cron#Overview\" target=\"_blank\"> cron-based</a>
scheduler to automatically start the backup process based on prescribed timing
instructions.</span><div><b>Note</b> that the required timestamps should be specified
respectively to the UTC time zone.</div>"
showIf:
1:
- name: cronTime
caption: Backup schedule
type: list
editable: false
values:
- value: 0 * * * *
caption: "Hourly (at minute 0)"
- value: 0 0 * * *
caption: "Daily (at 00:00)"
- value: 0 0 * * 0
caption: "Weekly (at 00:00 on Sunday)"
- value: 0 0 1 * *
caption: "Monthly (at 00:00 on day 1)"
default: 0 0 * * *
2:
- type: string
name: backupTime
caption: Time
inputType: time
default: "09:00"
cls: x-form-text
width: 120
required: true
- caption: Days
type: compositefield
name: days
defaultMargins: 0 12 0 0
items:
- name: sun
value: true
type: checkbox
caption: Su
- name: mon
value: true
type: checkbox
caption: Mo
- name: tue
value: true
type: checkbox
caption: Tu
- name: wed
value: true
type: checkbox
caption: We
- name: thu
value: true
type: checkbox
caption: Th
- name: fri
value: true
type: checkbox
caption: Fr
- name: sat
value: true
type: checkbox
caption: Sa
- name: "tz"
caption: "Time Zone"
type: "list"
required: true
editable: true
forceSelection: true
values: values
3:
- name: cronTime
caption: Crontab
type: string
default: 0 0 * * *
regexText: Cron syntax is incorrect!
regex: "^(((([\\\\*]{1}){1,})|((\\\\*\\\\\\/){0,1}(([0-9\\/\\*\\-\\,]{1}){1,}|(([1-5]{1}){1}([0-9\\/\\*\\-\\,]{1}){1,}){1})))
((([\\\\*]{1}){1,})|((\\\\*\\\\\\/){0,1}(([0-9\\/\\*\\-\\,]{1}){1,}|(([1]{1}){1}([0-9\\/\\*\\-\\,-]{1}){1,}){1}|([2]{1}){1}([0-3]{1}){1})))
((([\\\\*]{1}){1})|((\\\\*\\\\\\/){0,1}(([1-9]{1}){1}|(([1-2]{1}){1}([0-9\\/\\*\\-\\,]{1}){1,5}){1}|([3]{1}){1}([0-1]{1}){1})))
((([\\\\*]{1}){1})|((\\\\*\\\\\\/){0,1}(([1-9]{1}){1}|(([1-2]{1}){1}([0-9\\/\\*\\-\\,]{1}){1,}){1}|([3]{1}){1}([0-1]{1}){1}))|(jan|JAN|feb|FEB|mar|MAR|apr|APR|may|MAY|jun|JUN|jul|JUL|aug|AUG|sep|SEP|okt|OKT|nov|NOV|dec|DEC)(-?\\w+?)?)
((([\\\\*]{1}){1})|((\\\\*\\\\\\/){0,1}(([0-7]{1,}(-?[0-7]?(,[0-7]){0,6})){1}))|((sun|SUN|mon|MON|tue|TUE|wed|WED|thu|THU|fri|FRI|sat|SAT)?(,(sun|SUN|mon|MON|tue|TUE|wed|WED|thu|THU|fri|FRI|sat|SAT)){0,6})(-?\\w+?)?))$|^(@(reboot|yearly|annualy|monthly|weekly|daily|hourly))$"
- caption: Backup storage
type: list
tooltip: "The environment with backup storage to be used for backups creation. Presence of this environment is obligatory."
name: storageName
dependsOn: region
required: true
- type: spinner
name: backupCount
caption: Number of backups
tooltip: "The number of newest backups to be kept during rotation."
min: 1
max: 30
default: 5
- type: toggle
name: isAlwaysUmount
caption: Always umount
tooltip: "Always unmount backup storage when backup/restore is finished."
value: false
hidden: false
- type: displayfield
name: displayfield
markup: Please specify the database user that has enough privileges to access and modify all the databases stored on server. Username and password are required for all the DB servers except Redis.
hidden: false
hideLabel: true
cls: warning
- hideLabel: false
hidden: false
type: string
caption: Database User
name: dbuser
tooltip: In case you restore non-native database backup do not forget to provide its credentials instead of initial ones with help of add-on Configure action. It is relevant to <b>sqldb</b> databases and <b>MongoDB</b> only.
- hideLabel: false
hidden: false
type: string
inputType: password
caption: Database Password
name: dbpass
onBeforeInit: scripts/configOnBeforeInit.js
restore:
fields: []
onBeforeInit: scripts/restoreOnBeforeInit.js
onBeforeInit: scripts/backupOnBeforeInit.js
buttons:
- caption: Backup Now
action: backup
loadingText: Backing up...
confirmText: Do you want to initiate the backup process?
successText: The backup process has been finished successfully.
- caption: Configure
action: configure
settings: main
loadingText: Configuring...
successText: The backup configs have been updated successfully.
- caption: Restore
action: restore
loadingText: Restoring...
settings: restore
successText: The backup have been successfully restored.
title: Restore Backup
submitButtonText: Restore
confirmText: You are going to restore from a backup, which will override all your existing data. This action cannot be canceled or reverted. Do you want to proceed?
globals:
scriptSufix: db-backup
onInstall:
- checkAddons
- installRestic
- setSchedule
onUninstall:
- callScript: uninstall
- removeScript
onBeforeDelete:
- callScript: uninstall
- removeScript
onAfterRedeployContainer[${targetNodes.nodeGroup}]:
- installRestic
onAfterClone:
- script: return {result:0, jps:MANIFEST};
- install: ${response.jps}
nodeGroup: ${targetNodes.nodeGroup}
envName: ${event.response.env.envName}
settings:
scheduleType: ${settings.scheduleType}
storageName: ${settings.storageName}
cronTime: ${settings.cronTime}
backupTime: ${settings.backupTime}
sun: ${settings.sun}
mon: ${settings.mon}
tue: ${settings.tue}
wed: ${settings.wed}
thu: ${settings.thu}
fri: ${settings.fri}
sat: ${settings.sat}
tz: ${settings.tz}
backupCount: ${settings.backupCount}
isAlwaysUmount: ${settings.isAlwaysUmount}
onAfterConfirmTransfer: setSchedule
actions:
checkAddons:
- script: |-
var onAfterReturn = { setGlobals: {} },
glbs = onAfterReturn.setGlobals,
resp = api.marketplace.app.GetAddonList({
search: {},
envName: "${env.name}",
session: session
});
if (resp.result != 0) return resp;
glbs["alreadyInstalled"] = false;
for (let i = 0, n = resp.apps.length; i < n; i++) {
if (resp.apps[i].isInstalled) {
if (resp.apps[i].app_id == 'wp-backup') {
glbs["alreadyInstalled"] = true;
break;
}
}
}
return { result: 0, onAfterReturn: onAfterReturn };
- if ('${globals.alreadyInstalled}' == 'true' ):
- stopEvent:
type: warning
message: Database backup add-on is already installed on ${env.name}. Database backup addon installation is not possible.
installRestic:
cmd [${targetNodes.nodeGroup}]: |-
wget --tries=10 -O /tmp/installUpdateRestic ${baseUrl}/scripts/installUpdateRestic && \
mv -f /tmp/installUpdateRestic /usr/sbin/installUpdateRestic && \
chmod +x /usr/sbin/installUpdateRestic && /usr/sbin/installUpdateRestic
user: root
installScript:
- removeScript
- getStorageCtid
- script: ${baseUrl}/scripts/create-backup-main-script.js?_r=${fn.random}
params:
scriptName: ${env.envName}-${globals.scriptSufix}
baseUrl: ${baseUrl}
cronTime: ${this.cronTime}
backupCount: ${this.backupCount}
userId: ${env.uid}
storageNodeId: ${response.storageCtid}
backupExecNode: ${targetNodes.master.id}
storageEnv: ${response.storageEnvShortName}
isAlwaysUmount: ${this.isAlwaysUmount}
nodeGroup: ${this.nodeGroup}
dbuser: ${this.dbuser}
dbpass: ${this.dbpass}
callScript:
script: |-
var resp = jelastic.dev.scripting.Eval(appid, session, '${env.envName}-${globals.scriptSufix}', {action:"${this}"});
if (resp.result === 1702 && "${this}" == "uninstall") {
return { result: 0, out: "script not found" };
} else {
return resp.response || resp;
}
removeScript:
script: |-
var resp = jelastic.dev.scripting.GetScript(appid, session, '${env.envName}-${globals.scriptSufix}');
if (resp.result === 0) {
var resp = jelastic.dev.scripting.DeleteScript(appid, session, '${env.envName}-${globals.scriptSufix}');
return resp.response || resp;
}
return { result: 0 };
backup:
- callScript: backup
- deleteDBdump
restore:
- cmd[${targetNodes.nodeGroup}]: |-
echo "${settings.backupedEnvName}" > /root/.backupedenv
echo "${settings.backupDir}" > /root/.backupid
user: root
- callScript: restore
- deleteDBdump
deleteDBdump:
- cmd[${targetNodes.nodeGroup}]: |-
[ -f /root/db_backup.sql ] && rm -f /root/db_backup.sql || exit 0;
user: root
configure:
- setSchedule
getStorageCtid:
- script: scripts/getStorageCtid.js
convert:
- script: |
var resp = {result:0, onAfterReturn: {setGlobals:{cron: ""}}}, offset = java.util.TimeZone.getTimeZone("${settings.tz}").getRawOffset(),
setGlobals = resp.onAfterReturn.setGlobals;
var time = "${settings.backupTime}".split(":"),
d1 = new Date(2020, 1, 10, parseInt(time[0],10), parseInt(time[1],10)),
d2 = new Date(d1.getTime() - offset),
dd = d2.getDate() - d1.getDate(),
days = getDays([${settings.sun:0}, ${settings.mon:0}, ${settings.tue:0}, ${settings.wed:0}, ${settings.thu:0}, ${settings.fri:0}, ${settings.sat:0}], dd);
setGlobals.cron = d2.getMinutes() + " " + d2.getHours() + " * * " + days.join(",");
function getDays(settings, dd) {
var days = [];
for (var i = 0, n = settings.length; i < n; i++) {
if (settings[i]) {
var day = i + dd;
if (day < 0) day +=7; else if (day > 6) day -=7;
days.push(day);
}
}
days.sort();
return days;
}
return resp;
setSchedule:
- setGlobals:
storageEnv: ${settings.storageName}
isAlwaysUmount: ${settings.isAlwaysUmount}
- if ("${settings.scheduleType}" == 2):
- convert
- else:
- setGlobals:
cron: ${settings.cronTime}
- if ("${settings.isAlwaysUmount}" == "true"):
- removePermanentMount
- else:
- removePermanentMount
- addPermanentMount
- installScript:
cronTime: ${globals.cron}
backupCount: ${settings.backupCount}
isAlwaysUmount: ${globals.isAlwaysUmount}
nodeGroup: ${targetNodes.nodeGroup}
dbuser: ${settings.dbuser}
dbpass: ${settings.dbpass}
addPermanentMount:
- getStorageCtid
- script: |
return jelastic.env.file.AddMountPointById("${env.envName}", session, "${targetNodes.master.id}", "/opt/backup", "nfs4", null, "/data/", "${response.storageCtid}", "DBBackupRestore", false);
removePermanentMount:
- getStorageCtid
- script: |
var allMounts = jelastic.env.file.GetMountPoints("${env.envName}", session, "${targetNodes.master.id}").array;
for (var i = 0, n = allMounts.length; i < n; i++) {
if (allMounts[i].path == "/opt/backup" && allMounts[i].type == "INTERNAL") {
resp = jelastic.env.file.RemoveMountPointById("${env.envName}", session, "${targetNodes.master.id}", "/opt/backup");
if (resp.result != 0) { return resp; }
}
}
allMounts = jelastic.env.file.GetMountPoints("${env.envName}", session).array;
for (var i = 0, n = allMounts.length; i < n; i++) {
if (allMounts[i].path == "/opt/backup" && allMounts[i].type == "INTERNAL") {
resp = jelastic.env.file.RemoveMountPointByGroup("${env.envName}", session, "sqldb", "/opt/backup");
if (resp.result != 0) { return resp; }
}
}
return { "result": 0 };