summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-nlbwmon/htdocs/luci-static/resources/view/nlbw/backup.js
blob: 0b13e6fdf2022cf129b129331c933a5229ed7e13 (plain)
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
'use strict';
'require view';
'require ui';
'require fs';

return view.extend({
	load: function() {
		return fs.trimmed('/proc/sys/kernel/hostname');
	},

	handleArchiveUpload: function(ev) {
		return ui.uploadFile('/tmp/nlbw-restore.tar.gz').then(function() {
			return fs.exec('/usr/libexec/nlbwmon-action', [ 'restore' ]).then(function(res) {
				if (res.code != 0)
					throw new Error(res.stderr || res.stdout);

				var json = JSON.parse(res.stdout || '{}'),
				    list = (L.isObject(json) && Array.isArray(json.restored)) ? json.restored : [];

				ui.showModal(_('Restore complete'), [
					E('p', [ _('The following database files have been restored:') ]),
					E('ul', list.map(function(file) { return E('li', [ file ]) })),
					E('div', { 'class': 'right' }, [
						E('button', { 'click': ui.hideModal }, [ _('Dismiss') ])
					])
				]);
			}).catch(function(err) {
				ui.addNotification(null, E('p', [ _('Failed to restore backup archive: %s').format(err.message) ]));
			});
		});
	},

	handleArchiveDownload: function(hostname, ev) {
		return fs.exec_direct('/usr/libexec/nlbwmon-action', [ 'backup' ], 'blob').then(function(blob) {
			var url = window.URL.createObjectURL(blob),
			    date = new Date(),
			    name = 'nlbwmon-backup-%s-%04d-%02d-%02d.tar.gz'.format(hostname, date.getFullYear(), date.getMonth() + 1, date.getDate()),
			    link = E('a', { 'style': 'display:none', 'href': url, 'download': name });

			document.body.appendChild(link);
			link.click();
			document.body.removeChild(link);
			window.URL.revokeObjectURL(url);
		}).catch(function(err) {
			ui.addNotification(null, E('p', [ _('Failed to download backup archive: %s').format(err.message) ]));
		});
	},

	render: function(hostname) {
		return E([], [
			E('h2', [ _('Netlink Bandwidth Monitor - Backup / Restore') ]),
			E('h5', [ _('Restore Database Backup') ]),
			E('p', [
				E('button', {
					'class': 'cbi-button',
					'click': ui.createHandlerFn(this, 'handleArchiveUpload')
				}, [ _('Restore') ])
			]),
			E('h5', [ _('Download Database Backup') ]),
			E('p', [
				E('button', {
					'class': 'cbi-button',
					'click': ui.createHandlerFn(this, 'handleArchiveDownload', hostname)
				}, [ _('Generate Backup') ])
			])
		]);
	},

	handleSave: null,
	handleSaveApply: null,
	handleReset: null
});