1
|
|
|
class Xcloner_Backup { |
2
|
|
|
|
3
|
|
|
constructor() { |
4
|
|
|
this.cancel = 0; |
5
|
|
|
this.params; |
|
|
|
|
6
|
|
|
this.generate_hash = false; |
7
|
|
|
this.last_dumpfile = ""; |
8
|
|
|
this.last_backup_file = "" |
9
|
|
|
this.backup_part = 0 |
10
|
|
|
this.backup_size_total = 0; |
11
|
|
|
this.resume = new Object(); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
get_form_params() { |
15
|
|
|
var table_params = [] |
16
|
|
|
var files_params = [] |
17
|
|
|
var extra = [] |
|
|
|
|
18
|
|
|
|
19
|
|
|
jQuery.each(jQuery("#jstree_database_container").jstree("get_checked", true), function () { |
20
|
|
|
|
21
|
|
|
var object = new Object(); |
22
|
|
|
object.id = this.id |
23
|
|
|
object.parent = this.parent |
24
|
|
|
|
25
|
|
|
var index = table_params.length; |
26
|
|
|
table_params[index] = object |
27
|
|
|
}); |
28
|
|
|
|
29
|
|
|
jQuery.each(jQuery("#jstree_files_container").jstree("get_checked", true), function () { |
30
|
|
|
//console.log(this.id+"-"+this.parent); |
31
|
|
|
|
32
|
|
|
var object = new Object(); |
33
|
|
|
object.id = this.id |
34
|
|
|
object.parent = this.parent |
35
|
|
|
|
36
|
|
|
var index = files_params.length; |
37
|
|
|
files_params[index] = object |
38
|
|
|
}); |
39
|
|
|
|
40
|
|
|
var $return = new Object(); |
41
|
|
|
$return.table_params = table_params; |
42
|
|
|
$return.files_params = files_params; |
43
|
|
|
$return.backup_params = jQuery('#generate_backup_form').serializeArray(); |
44
|
|
|
|
45
|
|
|
return $return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
do_backup_database_callback(elem, action, json) { |
49
|
|
|
if (json.extra) |
50
|
|
|
this.params.extra = json.extra; |
|
|
|
|
51
|
|
|
|
52
|
|
|
if (json.extra.stats) { |
53
|
|
|
if (json.extra.stats.tables_count !== undefined) { |
54
|
|
|
jQuery(elem).find(".table-counter").text(parseInt(json.extra.stats.tables_count)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (json.extra.stats.database_count !== undefined) { |
58
|
|
|
jQuery(elem).find(".database-counter").text(parseInt(json.extra.stats.database_count)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (json.extra.stats.total_records !== undefined) { |
62
|
|
|
jQuery(elem).find(".total-records").text(parseInt(json.extra.stats.total_records)); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (json.extra.tableName) { |
67
|
|
|
jQuery(elem).find(".last-logged-table").text(json.extra.databaseName + "." + json.extra.tableName + " (" + json.extra.processedRecords + " records)"); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (json.extra.processedRecords !== undefined && !json.extra.startAtRecord && !json.extra.endDump) { |
71
|
|
|
var records = parseInt(jQuery(elem).find(".total-records").attr('data-processed')) + parseInt(json.extra.processedRecords); |
72
|
|
|
|
73
|
|
|
var percent = 100 * parseInt(records) / parseInt(jQuery(elem).find(".total-records").text()); |
74
|
|
|
jQuery(elem).find('.progress .determinate').css('width', percent + '%'); |
75
|
|
|
|
76
|
|
|
jQuery(elem).find(".total-records").attr('data-processed', records); |
77
|
|
|
jQuery(elem).find(".status-body ul.logged-tables").prepend(jQuery("<li>").text(json.extra.databaseName + "." + json.extra.tableName + " (" + json.extra.processedRecords + " records)")); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (json.extra.dumpfile !== undefined) { |
81
|
|
|
var db_text = (json.extra.dumpfile + " (" + this.getSize(json.extra.dumpsize, 1024) + " KB)"); |
82
|
|
|
|
83
|
|
|
if (!jQuery(this.last_dumpfile).hasClass(json.extra.dumpfile)) { |
84
|
|
|
this.last_dumpfile = (jQuery("<li>").addClass(json.extra.dumpfile).html(db_text)).prependTo("ul.logged-databases"); |
85
|
|
|
} |
86
|
|
|
else { |
87
|
|
|
jQuery(this.last_dumpfile).html(db_text) |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (!json.finished /*&& !this.cancel*/) { |
93
|
|
|
|
94
|
|
|
this.do_ajax(elem, action); |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
jQuery(elem).find(".last-logged-table").text('done'); |
100
|
|
|
jQuery(elem).find('.progress .determinate').css('width', '100%'); |
101
|
|
|
|
102
|
|
|
this.do_backup_files(); |
|
|
|
|
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
do_backup_database() { |
107
|
|
|
if (!jQuery('#jstree_database_container').length) { |
108
|
|
|
this.do_backup_files(); |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/*if(this.cancel) |
113
|
|
|
return false;*/ |
114
|
|
|
|
115
|
|
|
var elem = "#generate_backup ul.backup-status li.database-backup"; |
116
|
|
|
jQuery(elem).show(); |
117
|
|
|
jQuery(elem + ' .status-body').show(); |
118
|
|
|
|
119
|
|
|
jQuery(elem).find(".total-records").text(0); |
120
|
|
|
jQuery(elem).find(".total-records").attr('data-processed', 0); |
121
|
|
|
jQuery(elem).find(".table-counter").text(0); |
122
|
|
|
jQuery(elem).find(".database-counter").text(0); |
123
|
|
|
jQuery(elem).find(".logged-databases").html(""); |
124
|
|
|
jQuery(elem).find(".logged-tables").html(""); |
125
|
|
|
|
126
|
|
|
this.last_dumpfile = 0; |
127
|
|
|
|
128
|
|
|
jQuery(elem).find('.progress .determinate').css('width', '0%'); |
129
|
|
|
|
130
|
|
|
this.do_ajax(elem, 'backup_database', 1); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
do_scan_filesystem_callback(elem, action, json) { |
134
|
|
|
|
135
|
|
|
if (json.total_files_num) { |
136
|
|
|
jQuery(".file-system .file-counter").text(parseInt(json.total_files_num) + parseInt(jQuery(".file-system .file-counter").text())); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (json.total_files_size) { |
140
|
|
|
var size = parseFloat(json.total_files_size) + parseFloat(jQuery(".file-system .file-size-total").text()) |
141
|
|
|
jQuery(".file-system .file-size-total").text(size.toFixed(2)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if (json.last_logged_file) { |
145
|
|
|
jQuery(".file-system .last-logged-file").text(json.last_logged_file); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (!json.finished /*&& !this.cancel*/) { |
149
|
|
|
|
150
|
|
|
this.do_ajax(elem, action); |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
//finished |
155
|
|
|
jQuery(elem).find('.progress .indeterminate').removeClass('indeterminate').addClass('determinate').css('width', '100%'); |
156
|
|
|
jQuery(".file-system .last-logged-file").text('done'); |
157
|
|
|
|
158
|
|
|
//this.do_backup_database(); |
159
|
|
|
this.do_backup_database(); |
|
|
|
|
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
do_scan_filesystem() { |
164
|
|
|
/*if(this.cancel) |
165
|
|
|
return false;*/ |
166
|
|
|
|
167
|
|
|
var elem = "#generate_backup ul.backup-status li.file-system"; |
168
|
|
|
jQuery(elem).show(); |
169
|
|
|
jQuery(elem + ' .status-body').show(); |
170
|
|
|
jQuery(elem).find('.collapsible-header').trigger('click'); |
171
|
|
|
|
172
|
|
|
jQuery(".file-system .file-counter").text(0); |
173
|
|
|
jQuery(".file-system .last-logged-file").text(""); |
174
|
|
|
jQuery(".file-system .file-size-total").text(0); |
175
|
|
|
jQuery('.file-system .progress div').removeClass('determinate').addClass('indeterminate').css('width', '0%'); |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
this.do_ajax(elem, 'scan_filesystem', 1); |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
do_backup_files_callback(elem, action, json) { |
183
|
|
|
/*if(this.cancel) |
184
|
|
|
return false;*/ |
185
|
|
|
|
186
|
|
|
if (json.extra) { |
187
|
|
|
this.params.extra = json.extra; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if (json.extra) { |
191
|
|
|
if (json.extra.start_at_line !== undefined) { |
192
|
|
|
jQuery(elem).find(".file-counter").text(parseInt(json.extra.start_at_line)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (json.extra.start_at_line !== undefined) { |
196
|
|
|
//var prev_backup_size = parseInt(jQuery(elem).find(".file-size-total").attr('data-processed')); |
197
|
|
|
jQuery(elem).find(".file-size-total").text(this.getSize(this.backup_size_total + parseInt(json.extra.backup_size))); |
198
|
|
|
//var backup_size = parseInt(json.extra.backup_size); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (json.extra.processed_file) { |
204
|
|
|
if (json.extra.start_at_byte !== undefined && json.extra.start_at_byte) { |
205
|
|
|
var processed_size = json.extra.start_at_byte; |
206
|
|
|
} |
207
|
|
|
else { |
208
|
|
|
var processed_size = json.extra.processed_file_size; |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
jQuery(elem).find(".last-logged-file").text(json.extra.processed_file + " (" + this.getSize(processed_size, 1024) + " KB)"); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if (json.extra.processed_file !== undefined) { |
215
|
|
|
|
216
|
|
|
var backup_text = json.extra.backup_archive_name_full + " (" + this.getSize(json.extra.backup_size) + ") MB"; |
217
|
|
|
|
218
|
|
|
if (this.backup_part != json.extra.backup_part) { |
219
|
|
|
this.backup_part = json.extra.backup_part; |
220
|
|
|
this.backup_size_total = this.backup_size_total + json.extra.backup_size; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if (!jQuery(this.last_backup_file).hasClass(json.extra.backup_archive_name)) { |
224
|
|
|
this.last_backup_file = (jQuery("<li>").addClass(json.extra.backup_archive_name).html(backup_text)).prependTo(jQuery(elem).find(".status-body .backup-name")); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
jQuery(this.last_backup_file).html(backup_text) |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
if (json.extra.lines_total) { |
233
|
|
|
var percent = 100 * parseInt(json.extra.start_at_line) / parseInt(json.extra.lines_total); |
234
|
|
|
jQuery(elem).find('.progress .determinate').css('width', percent + '%'); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if (!json.finished /*&& !this.cancel*/) { |
238
|
|
|
|
239
|
|
|
this.do_ajax(elem, action); |
240
|
|
|
return false; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
jQuery(elem).find(".last-logged-file").text('done'); |
244
|
|
|
jQuery(".backup-done .cloud-upload").attr("href", "#" + json.extra.backup_parent); |
245
|
|
|
jQuery(".backup-done .download").attr("href", "#" + json.extra.backup_parent); |
246
|
|
|
jQuery(".backup-done .list-backup-content").attr("href", "#" + json.extra.backup_parent); |
247
|
|
|
|
248
|
|
|
if( jQuery('#backup_options #backup_encrypt').is(':checked')) { |
249
|
|
|
this.do_backup_encryption(); |
250
|
|
|
return; |
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
//this.restart_backup(); |
254
|
|
|
this.do_backup_done() |
255
|
|
|
} |
|
|
|
|
256
|
|
|
|
257
|
|
|
do_backup_files() { |
258
|
|
|
if (this.cancel) |
259
|
|
|
return false; |
|
|
|
|
260
|
|
|
|
261
|
|
|
var elem = "#generate_backup ul.backup-status li.files-backup"; |
262
|
|
|
jQuery(elem).show(); |
263
|
|
|
jQuery(elem + ' .status-body').show(); |
264
|
|
|
jQuery(elem).find('.collapsible-header').trigger('click'); |
265
|
|
|
|
266
|
|
|
jQuery(elem).find(".file-size-total").text(0); |
267
|
|
|
jQuery(elem).find(".file-size-total").attr('data-processed', 0); |
268
|
|
|
jQuery(elem).find(".file-counter").text(0); |
269
|
|
|
jQuery(elem).find(".last-logged-file").text(""); |
270
|
|
|
|
271
|
|
|
jQuery(elem).find('.progress .determinate').css('width', '0%'); |
272
|
|
|
|
273
|
|
|
this.do_ajax(elem, 'backup_files', 1); |
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
do_backup_encryption_callback(elem, action, response) { |
277
|
|
|
|
278
|
|
|
if (response.extra) { |
279
|
|
|
this.params.extra = response.extra; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
jQuery(".backup-encryption .last-logged-file").text('encrypting ' + response.processing_file+' ...'); |
283
|
|
|
|
284
|
|
|
if (response.total_size !== undefined) { |
285
|
|
|
jQuery(".backup-encryption .progress > div").removeClass('indeterminate').addClass("determinate"); |
286
|
|
|
var percent = parseInt(response.start * 100) / parseInt(response.total_size) |
287
|
|
|
jQuery(".backup-encryption .progress .determinate").css('width', parseInt(percent) + "%") |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
if (response.error) { |
291
|
|
|
jQuery(".backup-encryption .status-body").show(); |
292
|
|
|
jQuery(".backup-encryption .status-body").addClass("error").prepend(response.message+" ") |
293
|
|
|
jQuery(".backup-encryption .progress > div").addClass("determinate").removeClass("indeterminate").css('width', "100%") |
294
|
|
|
return; |
|
|
|
|
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
if (!response.finished /*&& !this.cancel*/) { |
298
|
|
|
|
299
|
|
|
this.do_ajax(elem, action); |
300
|
|
|
return false; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
//finished |
304
|
|
|
jQuery(elem).find('.progress > div').css('width', '100%'); |
305
|
|
|
jQuery(".backup-encryption .last-logged-file").text('done'); |
306
|
|
|
|
307
|
|
|
//this.do_backup_database(); |
308
|
|
|
this.do_backup_done() |
309
|
|
|
|
310
|
|
|
} |
|
|
|
|
311
|
|
|
|
312
|
|
|
do_backup_encryption() { |
313
|
|
|
if (this.cancel) |
314
|
|
|
return false; |
|
|
|
|
315
|
|
|
|
316
|
|
|
var elem = "#generate_backup ul.backup-status li.backup-encryption"; |
317
|
|
|
jQuery(elem).show(); |
318
|
|
|
jQuery(elem + ' .status-body').show(); |
319
|
|
|
jQuery(elem).find('.collapsible-header').trigger('click'); |
320
|
|
|
|
321
|
|
|
jQuery(elem).find('.progress .determinate').css('width', '0%'); |
322
|
|
|
|
323
|
|
|
this.do_ajax(elem, 'backup_encryption', 1); |
|
|
|
|
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
do_backup_done() { |
327
|
|
|
var elem = "#generate_backup ul.backup-status li.backup-done"; |
328
|
|
|
jQuery(elem).show(); |
329
|
|
|
jQuery(elem + ' .status-body').show(); |
330
|
|
|
jQuery(elem).find('.collapsible-header').trigger('click'); |
331
|
|
|
|
332
|
|
|
this.set_cancel(false) |
333
|
|
|
jQuery('#generate_backup .action-buttons a').hide(); |
334
|
|
|
jQuery('#generate_backup .action-buttons .start').css('display', 'inline-block'); |
335
|
|
|
|
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
do_save_schedule_callback(elem, action, json) { |
|
|
|
|
339
|
|
|
jQuery("#schedule_backup_success").show(); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
cancel_backup() { |
343
|
|
|
this.set_cancel(true) |
344
|
|
|
jQuery('#generate_backup .action-buttons a').hide(); |
345
|
|
|
jQuery('#generate_backup .action-buttons .start').css('display', 'inline-block'); |
346
|
|
|
jQuery('#generate_backup .action-buttons .restart').css('display', 'inline-block'); |
347
|
|
|
|
348
|
|
|
//this.restart_backup(); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
restart_backup() { |
352
|
|
|
this.set_cancel(false); |
353
|
|
|
|
354
|
|
|
jQuery('#generate_backup .action-buttons a').hide(); |
355
|
|
|
jQuery('#generate_backup .action-buttons .cancel').css('display', 'inline-block'); |
356
|
|
|
|
357
|
|
|
if (this.resume.action) { |
358
|
|
|
//console.log(this.resume.action) |
359
|
|
|
this.do_ajax(this.resume.elem, this.resume.action, this.resume.params); |
360
|
|
|
this.resume = new Object(); |
361
|
|
|
return; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
this.start_backup() |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
start_backup() { |
368
|
|
|
|
369
|
|
|
this.resume = new Object() |
370
|
|
|
this.set_cancel(false); |
371
|
|
|
jQuery('#generate_backup .action-buttons a').hide(); |
372
|
|
|
jQuery('#generate_backup .action-buttons .cancel').css('display', 'inline-block'); |
373
|
|
|
|
374
|
|
|
|
375
|
|
|
this.generate_hash = true; |
376
|
|
|
//this.cancel = false; |
377
|
|
|
this.backup_size_total = 0; |
378
|
|
|
this.last_backup_file = ""; |
379
|
|
|
this.backup_part = 0; |
380
|
|
|
jQuery('#generate_backup ul.backup-name li').remove(); |
381
|
|
|
|
382
|
|
|
jQuery('#generate_backup ul.backup-status > li').hide(); |
383
|
|
|
jQuery('#generate_backup .backup-status').show(); |
384
|
|
|
|
385
|
|
|
this.params = this.get_form_params(); |
386
|
|
|
|
387
|
|
|
|
388
|
|
|
this.do_scan_filesystem(); |
389
|
|
|
|
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
set_cancel(status) { |
393
|
|
|
if (status) { |
|
|
|
|
394
|
|
|
//document.dispatchEvent(new CustomEvent("xcloner_restore_update_progress", {detail: {percent: 0, class: 'determinate' }})); |
395
|
|
|
//jQuery("#generate_backup .collapsible-header.active .progress > div").add |
396
|
|
|
} |
397
|
|
|
this.cancel = status |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
get_cancel(status) { |
|
|
|
|
401
|
|
|
return this.cancel |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
init_resume(elem, action, init) { |
405
|
|
|
this.resume.elem = elem |
406
|
|
|
this.resume.action = action |
407
|
|
|
this.resume.init = init |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** global: ajaxurl */ |
411
|
|
|
do_ajax(elem, action, init = 0) { |
412
|
|
|
var hash = ''; |
413
|
|
|
if (this.params.hash !== undefined) |
414
|
|
|
hash = this.params.hash; |
|
|
|
|
415
|
|
|
else if (this.generate_hash) |
416
|
|
|
hash = 'generate_hash'; |
|
|
|
|
417
|
|
|
|
418
|
|
|
|
419
|
|
|
if (this.cancel == true) { |
|
|
|
|
420
|
|
|
this.init_resume(elem, action, init); |
421
|
|
|
return; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
var callback = 'do_' + action + '_callback'; |
425
|
|
|
var data = JSON.stringify(this.params); |
426
|
|
|
var $this = this; |
427
|
|
|
|
428
|
|
|
jQuery.ajax({ |
429
|
|
|
url: ajaxurl, |
|
|
|
|
430
|
|
|
dataType: 'json', |
431
|
|
|
type: 'POST', |
432
|
|
|
data: {'action': action, 'data': data, 'init': init, 'hash': hash, 'API_ID': ID()}, |
433
|
|
|
error: function (err) { |
434
|
|
|
show_ajax_error("Communication Error", "", err) |
435
|
|
|
$this.init_resume(elem, action, init); |
436
|
|
|
//console.log(err); |
437
|
|
|
} |
438
|
|
|
}).done(function (json) { |
439
|
|
|
if (json.hash) { |
440
|
|
|
$this.params.hash = json.hash; |
441
|
|
|
//console.log(json.hash); |
442
|
|
|
} |
443
|
|
|
if (json.error !== undefined) { |
444
|
|
|
show_ajax_error("Communication Error", "", json.error_message); |
445
|
|
|
$this.init_resume(elem, action, init); |
446
|
|
|
return; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
$this.resume = new Object(); |
450
|
|
|
|
451
|
|
|
$this[callback](elem, action, json) |
452
|
|
|
|
453
|
|
|
}); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
getSize(bytes, conv = 1024 * 1024) { |
457
|
|
|
return (bytes / conv).toFixed(2); |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|