From cdcbac4bc65329685daa9f20165c28678a3e229d Mon Sep 17 00:00:00 2001 From: Sebastien Clement Date: Fri, 10 Jan 2020 19:38:27 +0100 Subject: [PATCH] :hammer: Add clean for local snapshots --- .../rootfs/opt/nextcloud_backup/routes/api.js | 6 +- .../nextcloud_backup/tools/hassioApiTools.js | 61 ++++++++++++++++++- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/routes/api.js b/nextcloud_backup/rootfs/opt/nextcloud_backup/routes/api.js index fa67183..62031fd 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/routes/api.js +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/routes/api.js @@ -169,7 +169,11 @@ router.post('/backup-settings', function(req, res, next) { }); router.post('/clean-now', function(req, res, next){ - webdav.clean(); + webdav.clean().then(()=>{ + hassioApiTools.clean().catch(); + }).catch(()=>{ + hassioApiTools.clean().catch(); + }); res.status(201); res.send() }); diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/hassioApiTools.js b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/hassioApiTools.js index b051156..3828410 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/hassioApiTools.js +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/hassioApiTools.js @@ -2,6 +2,8 @@ const request = require('request'); const progress = require('request-progress'); const statusTools = require('./status'); const fs = require('fs'); +const settingsTools = require('./settingsTools'); +const moment = require('moment'); // !!! FOR DEV PURPOSE ONLY !!! @@ -98,6 +100,30 @@ function downloadSnapshot(id) { }); } +function dellSnap(id) { + return new Promise((resolve, reject) => { + checkSnap(id).then(() => { + let token = process.env.HASSIO_TOKEN; + if (token == null) { + token = fallbackToken + } + let option = { + url: 'http://hassio/snapshots/' + id + '/remove', + headers: { 'X-HASSIO-KEY': token }, + json: true + } + request.post(option, (error, response, body) => { + if (error || (response.statusCode != 200 && response.statusCode != 204)) + reject(); + else + resolve(); + }) + }).catch(() => { + reject(); + }) + }) + +} function checkSnap(id) { return new Promise((resolve, reject) => { @@ -142,13 +168,13 @@ function createNewBackup(name) { request.post(option, (error, response, body) => { if (response.statusCode != 200) { status.status = "error"; - status.message = "Can't create new snapshot ("+ error + ")"; + status.message = "Can't create new snapshot (" + error + ")"; status.error_code = 5; statusTools.setStatus(status); console.error(status.message); reject(status.message); } - else{ + else { body.data.slug console.log('Snapshot created with id ' + body.data.slug); resolve(body.data.slug); @@ -157,6 +183,35 @@ function createNewBackup(name) { }); } +function clean() { + let limit = settingsTools.getSettings().auto_clean_backup_keep; + if (limit == null) + limit = 5; + return new Promise((resolve, reject) => { + getSnapshots().then(async (snaps) => { + if (snaps.length < limit) { + resolve(); + return; + } + snaps.sort((a, b) => { + if (moment(a.date).isBefore(moment(b.date))) + return 1; + else + return -1; + }); + let toDel = snaps.slice(limit); + for (let i in toDel) { + await dellSnap(toDel[i].slug) + } + console.log('Local clean done.') + resolve(); + }).catch(() => { + reject(); + }); + }) +} + exports.getSnapshots = getSnapshots; exports.downloadSnapshot = downloadSnapshot; -exports.createNewBackup = createNewBackup; \ No newline at end of file +exports.createNewBackup = createNewBackup; +exports.clean = clean; \ No newline at end of file