Add create new-backup route

This commit is contained in:
Sebastien Clement 2019-12-28 17:33:27 +01:00
parent b32d51847e
commit dfc63f0956
3 changed files with 89 additions and 8 deletions

View File

@ -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;

View File

@ -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;
exports.downloadSnapshot = downloadSnapshot;
exports.createNewBackup = createNewBackup;

View File

@ -128,7 +128,7 @@
<div class="col s12 m3">
<div class="card cyan darken-3">
<div class="card-content center">
<a class="btn green">Backup Now</a>
<a class="btn green" id="btn-backup-now">Backup Now</a>
</div>
</div>
</div>
@ -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: '<i class="material-icons" style="margin-right:10px">check_box</i> Command send !', classes: "green" });
}).fail((error)=>{
console.log(error);
M.toast({ html: '<i class="material-icons" style="margin-right:10px">warning</i> Can\'t send command !', classes: "red" });
})
.always(() => {
loadingModal.close();
})
}
</script>
</html>