From dfc63f09562d8c8800fed95dbb9225164b838b9f Mon Sep 17 00:00:00 2001 From: Sebastien Clement Date: Sat, 28 Dec 2019 17:33:27 +0100 Subject: [PATCH] Add create new-backup route --- .../rootfs/opt/nextcloud_backup/routes/api.js | 27 ++++++++++- .../nextcloud_backup/tools/hassioApiTools.js | 48 +++++++++++++++++-- .../opt/nextcloud_backup/views/index.ejs | 22 ++++++++- 3 files changed, 89 insertions(+), 8 deletions(-) diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/routes/api.js b/nextcloud_backup/rootfs/opt/nextcloud_backup/routes/api.js index 21c803c..67384fc 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/routes/api.js +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/routes/api.js @@ -24,6 +24,13 @@ router.get('/status', (req, res, next) => { router.get('/formated-local-snap', function(req, res, next) { hassioApiTools.getSnapshots().then( (snaps) => { + // TODO sort snaps by date + snaps.sort((a, b) =>{ + if(moment(a.date).isBefore(moment(b.date))) + return 1; + else + return -1; + }) res.render('localSnaps', { snaps: snaps, moment: moment }); }, (err) => { @@ -83,15 +90,31 @@ router.post('/manual-backup', function(req, res, next) { hassioApiTools.downloadSnapshot(id) .then(() => { webdav.uploadFile(id, '/Hassio Backup/Manual/' + name + '.tar'); - res.send(200); + res.status(201); + res.send(); }) .catch(() => { - res.send(500); + res.status(500) + res.send(); }) }); +router.post('/new-backup', function(req, res, next) { + + let name = 'Manual-' + moment().format('YYYY-MM-DD_HH:mm'); + hassioApiTools.createNewBackup(name).then((id) => { + hassioApiTools.downloadSnapshot(id) + .then(() => { + webdav.uploadFile(id, '/Hassio Backup/Manual/' + name + '.tar'); + }).catch(() => { + }) + }).catch(() => { + + }) + res.status(201); + res.send(); }); module.exports = router; \ No newline at end of file diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/hassioApiTools.js b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/hassioApiTools.js index a09f843..b051156 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/hassioApiTools.js +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/hassioApiTools.js @@ -49,7 +49,7 @@ function getSnapshots() { function downloadSnapshot(id) { return new Promise((resolve, reject) => { console.log('Downloading snapshot ' + id + '...') - if(!fs.existsSync('./temp/')) + if (!fs.existsSync('./temp/')) fs.mkdirSync('./temp/'); let stream = fs.createWriteStream('./temp/' + id + '.tar'); let token = process.env.HASSIO_TOKEN; @@ -74,7 +74,7 @@ function downloadSnapshot(id) { .on('error', (error) => { status.status = "error"; status.message = "Fail to download Hassio snapshot (" + error + ")"; - status.error_code = 4; + status.error_code = 1; statusTools.setStatus(status); console.error(status.message); reject(error); @@ -89,7 +89,7 @@ function downloadSnapshot(id) { }).catch(() => { status.status = "error"; status.message = "Fail to download Hassio snapshot. Not found ?"; - status.error_code = 4; + status.error_code = 1; statusTools.setStatus(status); console.error(status.message); reject(); @@ -111,7 +111,7 @@ function checkSnap(id) { json: true } request(option, (error, response, body) => { - if (error || response.statusCode != 200) + if (error || response.statusCode != 200) reject(); else resolve(); @@ -120,5 +120,43 @@ function checkSnap(id) { } + +function createNewBackup(name) { + return new Promise((resolve, reject) => { + let status = statusTools.getStatus(); + status.status = "creating"; + status.progress = -1; + statusTools.setStatus(status); + console.log("Creating new snapshot...") + let token = process.env.HASSIO_TOKEN; + if (token == null) { + token = fallbackToken + } + let option = { + url: 'http://hassio/snapshots/new/full', + headers: { 'X-HASSIO-KEY': token }, + json: true, + body: { name: name }, + timeout: 1200000 + } + request.post(option, (error, response, body) => { + if (response.statusCode != 200) { + status.status = "error"; + status.message = "Can't create new snapshot ("+ error + ")"; + status.error_code = 5; + statusTools.setStatus(status); + console.error(status.message); + reject(status.message); + } + else{ + body.data.slug + console.log('Snapshot created with id ' + body.data.slug); + resolve(body.data.slug); + } + }); + }); +} + exports.getSnapshots = getSnapshots; -exports.downloadSnapshot = downloadSnapshot; \ No newline at end of file +exports.downloadSnapshot = downloadSnapshot; +exports.createNewBackup = createNewBackup; \ No newline at end of file diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/views/index.ejs b/nextcloud_backup/rootfs/opt/nextcloud_backup/views/index.ejs index fda0c1d..89a7571 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/views/index.ejs +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/views/index.ejs @@ -128,7 +128,7 @@
@@ -330,8 +330,13 @@ break; case "download": printStatusWithBar('Downloading Snapshot', data.progress); + break; case "upload": printStatusWithBar('Uploading Snapshot', data.progress); + break; + case "creating": + printStatusWithBar('Creating Snapshot', data.progress); + break; } if (data.last_backup != null) { if ($('#last_back_status').html() != data.last_backup) @@ -380,6 +385,7 @@ function listeners() { $('#save-nextcloud-settings').click(sendNextcloudSettings); $('#trigger-nextcloud-settings').click(getNextcloudSettings) + $('#btn-backup-now').click(backupNow) } @@ -438,6 +444,20 @@ }); } + function backupNow() { + loadingModal.open(); + $.post('./api/new-backup') + .done(() => { + M.toast({ html: 'check_box Command send !', classes: "green" }); + }).fail((error)=>{ + console.log(error); + M.toast({ html: 'warning Can\'t send command !', classes: "red" }); + }) + .always(() => { + loadingModal.close(); + }) + } + \ No newline at end of file