backup.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. 'require view';
  3. 'require ui';
  4. 'require fs';
  5. return view.extend({
  6. load: function() {
  7. return fs.trimmed('/proc/sys/kernel/hostname');
  8. },
  9. handleArchiveUpload: function(ev) {
  10. return ui.uploadFile('/tmp/nlbw-restore.tar.gz').then(function() {
  11. return fs.exec('/usr/libexec/nlbwmon-action', [ 'restore' ]).then(function(res) {
  12. if (res.code != 0)
  13. throw new Error(res.stderr || res.stdout);
  14. var json = JSON.parse(res.stdout || '{}'),
  15. list = (L.isObject(json) && Array.isArray(json.restored)) ? json.restored : [];
  16. ui.showModal(_('Restore complete'), [
  17. E('p', [ _('The following database files have been restored:') ]),
  18. E('ul', list.map(function(file) { return E('li', [ file ]) })),
  19. E('div', { 'class': 'right' }, [
  20. E('button', { 'click': ui.hideModal }, [ _('Dismiss') ])
  21. ])
  22. ]);
  23. }).catch(function(err) {
  24. ui.addNotification(null, E('p', [ _('Failed to restore backup archive: %s').format(err.message) ]));
  25. });
  26. });
  27. },
  28. handleArchiveDownload: function(hostname, ev) {
  29. return fs.exec_direct('/usr/libexec/nlbwmon-action', [ 'backup' ], 'blob').then(function(blob) {
  30. var url = window.URL.createObjectURL(blob),
  31. date = new Date(),
  32. name = 'nlbwmon-backup-%s-%04d-%02d-%02d.tar.gz'.format(hostname, date.getFullYear(), date.getMonth() + 1, date.getDate()),
  33. link = E('a', { 'style': 'display:none', 'href': url, 'download': name });
  34. document.body.appendChild(link);
  35. link.click();
  36. document.body.removeChild(link);
  37. window.URL.revokeObjectURL(url);
  38. }).catch(function(err) {
  39. ui.addNotification(null, E('p', [ _('Failed to download backup archive: %s').format(err.message) ]));
  40. });
  41. },
  42. render: function(hostname) {
  43. return E([], [
  44. E('h2', [ _('Netlink Bandwidth Monitor - Backup / Restore') ]),
  45. E('h5', [ _('Restore Database Backup') ]),
  46. E('p', [
  47. E('button', {
  48. 'class': 'cbi-button',
  49. 'click': ui.createHandlerFn(this, 'handleArchiveUpload')
  50. }, [ _('Restore') ])
  51. ]),
  52. E('h5', [ _('Download Database Backup') ]),
  53. E('p', [
  54. E('button', {
  55. 'class': 'cbi-button',
  56. 'click': ui.createHandlerFn(this, 'handleArchiveDownload', hostname)
  57. }, [ _('Generate Backup') ])
  58. ])
  59. ]);
  60. },
  61. handleSave: null,
  62. handleSaveApply: null,
  63. handleReset: null
  64. });