Add manual backup interface, only download

This commit is contained in:
Sebastien Clement 2019-12-21 22:55:40 +01:00
parent 45f1de996e
commit 7ce157efec
4 changed files with 118 additions and 38 deletions

View File

@ -78,8 +78,15 @@ router.get('/nextcloud-settings', function(req, res, next){
router.post('/manual-backup', function(req, res, next) {
hassioApiTools.downloadSnapshot('e058caf6');
let id = req.query.id;
hassioApiTools.downloadSnapshot(id)
.then(() => {
res.send(200);
})
.catch(() => {
res.send(500);
})
});
module.exports = router;

View File

@ -54,6 +54,7 @@ function downloadSnapshot(id) {
token = fallbackToken
}
let status = statusTools.getStatus();
checkSnap(id).then(() => {
status.status = "download";
status.progress = 0;
statusTools.setStatus(status);
@ -67,7 +68,11 @@ function downloadSnapshot(id) {
statusTools.setStatus(status);
})
.on('error', (error) => {
console.log("error")
status.status = "error";
status.message = "Fail to downloadw Hassio snapshot (" + error + ")";
status.error_code = 4;
statusTools.setStatus(status);
reject(error);
})
.on('end', () => {
console.log('end')
@ -75,7 +80,37 @@ function downloadSnapshot(id) {
statusTools.setStatus(status);
})
.pipe(stream);
}).catch(() => {
status.status = "error";
status.message = "Fail to downloadw Hassio snapshot";
status.error_code = 4;
statusTools.setStatus(status);
reject();
});
});
}
function checkSnap(id) {
return new Promise((resolve, reject) => {
let token = process.env.HASSIO_TOKEN;
if (token == null) {
token = fallbackToken
}
let option = {
url: 'http://hassio/snapshots/' + id + '/info',
headers: { 'X-HASSIO-KEY': token },
json: true
}
request(option, (error, response, body) => {
if (error || response.statusCode != 200)
reject();
else
resolve();
})
});
}
exports.getSnapshots = getSnapshots;

View File

@ -98,8 +98,11 @@
<div class="card-content">
<span class="card-title white-text" style="font-weight: bold;">Status </span>
<div class="divider"></div>
<h5 id="status" class="white-text"></h5>
<h6 id="status" class="white-text"></h6>
<div id="status-second-line" class="truncate tooltipped" data-position="bottom" data-tooltip=""></div>
<div class="progress hide" id="progress">
<div class="determinate light-green darken-2" style="width: 0%"></div>
</div>
</div>
</div>
</div>
@ -305,6 +308,11 @@
console.log(id);
});
$('.manual-back-list').click(function(){
let id = this.getAttribute('data-id');
manualBackup(id);
})
});
}
@ -320,7 +328,7 @@
printStatus('Idle', "Waiting for next backup.");
break;
case "download":
printStatus('DL Snap...', data.progress);
printStatusWithBar('Downloading Snapshot', data.progress);
}
}
@ -333,8 +341,33 @@
$('#status').empty();
$('#status').html(status);
$('#status-second-line').empty();
$('#status-second-line').removeClass('center');
$('#status-second-line').html(secondLine);
$('#status-second-line').attr('data-tooltip', secondLine)
$('#progress').addClass("hide");
}
function printStatusWithBar(status, progress){
$('#status').empty();
$('#status').html(status);
let secondLine = $('#status-second-line')
secondLine.empty();
secondLine.html(progress == -1 ? "Waiting..." : (Math.round(progress*100) + "%"));
secondLine.addClass("center");
secondLine.attr('data-tooltip', Math.round(progress*100) + "%");
let progressDiv = $('#progress');
progressDiv.removeClass("hide");
if(progress == -1){
progressDiv.children().removeClass('determinate');
progressDiv.children().addClass('indeterminate');
}
else{
progressDiv.children().removeClass('indeterminate');
progressDiv.children().addClass('determinate');
progressDiv.children().css('width', (progress*100) + "%");
}
}
function listeners() {
@ -375,6 +408,11 @@
})
}
function manualBackup(id){
$.post('./api/manual-backup?id=' + id);
}
function getNextcloudSettings() {
loadingModal.open();

View File

@ -46,7 +46,7 @@
</div>
</div>
<div class="modal-footer blue-grey darken-4">
<a href="#!" class="waves-effect waves-green btn light-blue accent-4">Backup now</a>
<a href="#!" class="waves-effect waves-green btn light-blue accent-4 manual-back-list modal-close" data-id="<%=snaps[index].slug%>">Backup now</a>
<a href="#!" class="modal-close waves-effect waves-green btn red">Close</a>
</div>