Adding auto backup integration (Using Cron)

This commit is contained in:
Sebastien Clement 2020-01-04 19:59:13 +01:00
parent c2e8c08cb8
commit ea8aeb3f47
6 changed files with 168 additions and 22 deletions

View File

@ -64,6 +64,8 @@ webdav.confIsValid().then(
} }
) )
const cronTools = require('./tools/cronTools');
cronTools.startCron();
module.exports = app; module.exports = app;

View File

@ -299,6 +299,14 @@
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
}, },
"cron": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/cron/-/cron-1.7.2.tgz",
"integrity": "sha512-+SaJ2OfeRvfQqwXQ2kgr0Y5pzBR/lijf5OpnnaruwWnmI799JfWr2jN2ItOV9s3A/+TFOt6mxvKzQq5F0Jp6VQ==",
"requires": {
"moment-timezone": "^0.5.x"
}
},
"cross-spawn": { "cross-spawn": {
"version": "6.0.5", "version": "6.0.5",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz",
@ -1167,6 +1175,14 @@
"resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz", "resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz",
"integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==" "integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg=="
}, },
"moment-timezone": {
"version": "0.5.27",
"resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.27.tgz",
"integrity": "sha512-EIKQs7h5sAsjhPCqN6ggx6cEbs94GK050254TIJySD1bzoM5JTYDwAU1IoVOeTOL6Gm27kYJ51/uuvq1kIlrbw==",
"requires": {
"moment": ">= 2.9.0"
}
},
"morgan": { "morgan": {
"version": "1.9.1", "version": "1.9.1",
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz",

View File

@ -7,6 +7,7 @@
}, },
"dependencies": { "dependencies": {
"cookie-parser": "~1.4.4", "cookie-parser": "~1.4.4",
"cron": "^1.7.2",
"debug": "~2.6.9", "debug": "~2.6.9",
"ejs": "~2.6.1", "ejs": "~2.6.1",
"express": "~4.16.1", "express": "~4.16.1",

View File

@ -15,6 +15,7 @@ const cronTools = require('../tools/cronTools');
router.get('/status', (req, res, next) => { router.get('/status', (req, res, next) => {
cronTools.updatetNextDate();
let status = statusTools.getStatus(); let status = statusTools.getStatus();
res.json(status); res.json(status);
}); });
@ -26,8 +27,8 @@ router.get('/status', (req, res, next) => {
router.get('/formated-local-snap', function(req, res, next) { router.get('/formated-local-snap', function(req, res, next) {
hassioApiTools.getSnapshots().then( hassioApiTools.getSnapshots().then(
(snaps) => { (snaps) => {
snaps.sort((a, b) =>{ snaps.sort((a, b) => {
if(moment(a.date).isBefore(moment(b.date))) if (moment(a.date).isBefore(moment(b.date)))
return 1; return 1;
else else
return -1; return -1;
@ -88,6 +89,13 @@ router.get('/nextcloud-settings', function(req, res, next) {
router.post('/manual-backup', function(req, res, next) { router.post('/manual-backup', function(req, res, next) {
let id = req.query.id; let id = req.query.id;
let name = req.query.name; let name = req.query.name;
let status = statusTools.getStatus();
if (status.status == "creating" && status.status == "upload" && status.status == "download"){
res.status(503);
res.send();
return;
}
hassioApiTools.downloadSnapshot(id) hassioApiTools.downloadSnapshot(id)
.then(() => { .then(() => {
webdav.uploadFile(id, '/Hassio Backup/Manual/' + name + '.tar'); webdav.uploadFile(id, '/Hassio Backup/Manual/' + name + '.tar');
@ -103,6 +111,12 @@ router.post('/manual-backup', function(req, res, next) {
router.post('/new-backup', function(req, res, next) { router.post('/new-backup', function(req, res, next) {
let status = statusTools.getStatus();
if (status.status == "creating" && status.status == "upload" && status.status == "download"){
res.status(503);
res.send();
return;
}
let name = 'Manual-' + moment().format('YYYY-MM-DD_HH:mm'); let name = 'Manual-' + moment().format('YYYY-MM-DD_HH:mm');
hassioApiTools.createNewBackup(name).then((id) => { hassioApiTools.createNewBackup(name).then((id) => {
hassioApiTools.downloadSnapshot(id) hassioApiTools.downloadSnapshot(id)
@ -119,17 +133,17 @@ router.post('/new-backup', function(req, res, next) {
}); });
router.get('/backup-settings', function(req, res, next){ router.get('/backup-settings', function(req, res, next) {
res.send(settingsTools.getSettings()); res.send(settingsTools.getSettings());
}); });
router.post('/backup-settings', function(req, res, next){ router.post('/backup-settings', function(req, res, next) {
//TODO check if config is valid if (cronTools.checkConfig(req.body)) {
if(cronTools.checkConfig(req.body)){
settingsTools.setSettings(req.body); settingsTools.setSettings(req.body);
cronTools.startCron();
res.send(); res.send();
} }
else{ else {
res.status(400); res.status(400);
res.send(); res.send();
} }

View File

@ -1,4 +1,14 @@
const settingsTools = require('./settingsTools');
const WebdavTools = require('./webdavTools')
const webdav = new WebdavTools().getInstance();
const hassioApiTools = require('./hassioApiTools');
const statusTools = require('./status');
var CronJob = require('cron').CronJob;
const moment = require('moment');
function checkConfig(conf) { function checkConfig(conf) {
if (conf.cron_base != null) { if (conf.cron_base != null) {
@ -18,14 +28,14 @@ function checkConfig(conf) {
return false; return false;
} }
if ( conf.cron_base == '3'){ if (conf.cron_base == '3') {
if (conf.cron_month_day != null && conf.cron_month_day >= 1 && conf.cron_month_day <= 28) if (conf.cron_month_day != null && conf.cron_month_day >= 1 && conf.cron_month_day <= 28)
return true; return true;
else else
return false; return false;
} }
if(conf.cron_base == '0') if (conf.cron_base == '0')
return true return true
} }
else else
@ -34,4 +44,103 @@ function checkConfig(conf) {
return false; return false;
} }
function startCron() {
let cronContainer = new Singleton().getInstance();
cronContainer.init();
}
function updatetNextDate(){
let cronContainer = new Singleton().getInstance();
cronContainer.updatetNextDate();
}
class CronContainer {
constructor() {
this.cronJob = null;
}
init() {
let settings = settingsTools.getSettings();
let cronStr = "";
if (this.cronJob != null) {
console.log("Stoping Cron...")
this.cronJob.stop();
this.cronJob = null;
}
switch (settings.cron_base) {
case '0':
console.log("No Cron settings available.")
return;
case '1': {
let splited = settings.cron_hour.split(':');
cronStr = "" + splited[1] + " " + splited[0] + " * * *";
break;
}
case '2': {
let splited = settings.cron_hour.split(':');
cronStr = "" + splited[1] + " " + splited[0] + " * * " + settings.cron_weekday;
break;
}
case '3': {
let splited = settings.cron_hour.split(':');
cronStr = "" + splited[1] + " " + splited[0] + " " + settings.cron_month_day + " * *";
break;
}
}
console.log("Starting Cron...")
this.cronJob = new CronJob(cronStr, this._createBackup, null, false, Intl.DateTimeFormat().resolvedOptions().timeZone);
this.cronJob.start();
this.updatetNextDate();
}
updatetNextDate() {
let date;
if( this.cronJob == null)
date = "Not configured";
else
date = this.cronJob.nextDate().format('MMM D, YYYY HH:mm');
let status = statusTools.getStatus();
status.next_backup = date;
statusTools.setStatus(status);
}
_createBackup() {
let status = statusTools.getStatus();
if(status.status == "creating" && status.status == "upload" && status.status == "download")
return;
let name = 'Auto-' + moment().format('YYYY-MM-DD_HH:mm');
hassioApiTools.createNewBackup(name).then((id) => {
hassioApiTools.downloadSnapshot(id)
.then(() => {
webdav.uploadFile(id, '/Hassio Backup/Auto/' + name + '.tar');
}).catch(() => {
})
}).catch(() => {
})
}
}
class Singleton {
constructor() {
if (!Singleton.instance) {
Singleton.instance = new CronContainer();
}
}
getInstance() {
return Singleton.instance;
}
}
exports.checkConfig = checkConfig; exports.checkConfig = checkConfig;
exports.startCron = startCron;
exports.updatetNextDate = updatetNextDate;

View File

@ -125,7 +125,7 @@
<div class="card-content"> <div class="card-content">
<span class="card-title white-text" style="font-weight: bold;">Next Backup </span> <span class="card-title white-text" style="font-weight: bold;">Next Backup </span>
<div class="divider"></div> <div class="divider"></div>
<h5 id="next_back_status"></h5> <h6 class="white-text" id="next_back_status"></h6>
</div> </div>
</div> </div>
</div> </div>
@ -241,13 +241,13 @@
<div class="row hide"> <div class="row hide">
<div class="input-field col s12"> <div class="input-field col s12">
<select id="cron-drop-day"> <select id="cron-drop-day">
<option value="0">Monday</option> <option value="1">Monday</option>
<option value="1">Tuesday</option> <option value="2">Tuesday</option>
<option value="2">Wednesday</option> <option value="3">Wednesday</option>
<option value="3">Thursday</option> <option value="4">Thursday</option>
<option value="4">Friday</option> <option value="5">Friday</option>
<option value="5">Saturday</option> <option value="6">Saturday</option>
<option value="6">Sunday</option> <option value="0">Sunday</option>
</select> </select>
<label>Day</label> <label>Day</label>
</div> </div>
@ -414,6 +414,10 @@
if ($('#last_back_status').html() != data.last_backup) if ($('#last_back_status').html() != data.last_backup)
$('#last_back_status').html(data.last_backup); $('#last_back_status').html(data.last_backup);
} }
if (data.next_backup != null) {
if ($('#next_back_status').html() != data.next_backup)
$('#next_back_status').html(data.next_backup);
}
} }