Merge branch 'dev'

This commit is contained in:
SebClem 2020-09-12 16:49:33 +02:00
commit 1d86f42bf7
8 changed files with 265 additions and 210 deletions

View File

@ -22,7 +22,7 @@ const logger = winston.createLogger({
// - Write all logs error (and below) to `quick-start-error.log`.
//
new winston.transports.Console({handleExceptions: true}),
new winston.transports.File({filename: '/data/NCB.log', handleExceptions: true})
// new winston.transports.File({filename: '/data/NCB.log', handleExceptions: true})
]
});

View File

@ -30,10 +30,14 @@ router.get('/formated-local-snap', function(req, res, next) {
hassioApiTools.getSnapshots().then(
(snaps) => {
snaps.sort((a, b) => {
if (moment(a.date).isBefore(moment(b.date)))
if (moment(a.date).isBefore(moment(b.date))){
return 1;
}
else
{
return -1;
}
})
res.render('localSnaps', { snaps: snaps, moment: moment });
},
@ -46,7 +50,7 @@ router.get('/formated-local-snap', function(req, res, next) {
});
router.get('/formated-backup-manual', function(req, res, next) {
webdav.getFolderContent(pathTools.manual)
webdav.getFolderContent( webdav.getConf().back_dir + pathTools.manual)
.then((contents) => {
contents.sort((a, b) => {
if (moment(a.lastmod).isBefore(moment(b.lastmod)))
@ -62,7 +66,8 @@ router.get('/formated-backup-manual', function(req, res, next) {
});
router.get('/formated-backup-auto', function(req, res, next) {
webdav.getFolderContent(pathTools.auto)
let url = webdav.getConf().back_dir + pathTools.auto
webdav.getFolderContent( url )
.then((contents) => {
contents.sort((a, b) => {
if (moment(a.lastmod).isBefore(moment(b.lastmod)))
@ -122,7 +127,7 @@ router.post('/manual-backup', function(req, res, next) {
hassioApiTools.downloadSnapshot(id)
.then(() => {
webdav.uploadFile(id, pathTools.manual + name + '.tar');
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + '.tar');
res.status(201);
res.send();
})
@ -145,7 +150,7 @@ router.post('/new-backup', function(req, res, next) {
hassioApiTools.createNewBackup(name).then((id) => {
hassioApiTools.downloadSnapshot(id)
.then(() => {
webdav.uploadFile(id, pathTools.manual + name + '.tar');
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + '.tar');
}).catch(() => {
})

View File

@ -128,7 +128,7 @@ class CronContainer {
hassioApiTools.createNewBackup(name).then((id) => {
hassioApiTools.downloadSnapshot(id)
.then(() => {
webdav.uploadFile(id, pathTools.auto + name + '.tar');
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.auto + name + '.tar');
}).catch(() => {
})

View File

@ -1,4 +1,5 @@
let root = '/Hassio Backup/';
exports.root = root;
exports.manual = root + 'Manual/';
exports.auto = root + 'Auto/';
let default_root = '/Hassio Backup/';
exports.default_root = default_root;
exports.manual = 'Manual/';
exports.auto = 'Auto/';

View File

@ -64,12 +64,31 @@ class WebdavTools {
});
}
async __createRoot(){
let root_splited = this.getConf().back_dir.split('/').splice(1)
let path = '/'
for(let elem of root_splited){
if(elem != ''){
path = path + elem + '/'
try {
await this.client.createDirectory(path)
logger.debug(`Path ${path} created.`)
} catch (error) {
if(error.response.status == 405)
logger.debug(`Path ${path} already exist.`)
else
logger.error(error)
}
}
}
}
initFolder() {
return new Promise((resolve, reject) => {
this.client.createDirectory(pathTools.root).catch(() => { }).then(() => {
this.client.createDirectory(pathTools.auto).catch(() => { }).then(() => {
this.client.createDirectory(pathTools.manual).catch(() => { }).then(() => {
return new Promise((resolve) => {
this.__createRoot().catch((err) => { logger.error(err)}).then(() => {
this.client.createDirectory(this.getConf().back_dir + pathTools.auto).catch(() => { }).then(() => {
this.client.createDirectory(this.getConf().back_dir + pathTools.manual).catch(() => { }).then(() => {
resolve();
})
})
@ -105,6 +124,24 @@ class WebdavTools {
logger.error(status.message);
reject("Nextcloud config invalid !");
}
if(conf.back_dir == null || conf.back_dir == ''){
logger.info('Backup dir is null, initializing it.');
conf.back_dir = pathTools.default_root;
this.setConf(conf);
}
else{
if(!conf.back_dir.startsWith('/')){
logger.warn('Backup dir not starting with \'/\', fixing this...');
conf.back_dir=`/${conf.back_dir}`;
this.setConf(conf);
}
if(!conf.back_dir.endsWith('/')){
logger.warn('Backup dir not ending with \'/\', fixing this...');
conf.back_dir=`${conf.back_dir}/`;
this.setConf(conf);
}
}
}
else {
status.status = "error";
@ -147,7 +184,7 @@ class WebdavTools {
}
_startUpload(id, path) {
return new Promise( async (resolve, reject) => {
return new Promise( (resolve, reject) => {
let status = statusTools.getStatus();
status.status = "upload";
status.progress = 0;
@ -156,7 +193,6 @@ class WebdavTools {
statusTools.setStatus(status);
logger.info('Uploading snap...');
let tmpFile = `./temp/${id}.tar`
let fileSize = fs.statSync(tmpFile).size;
let stream = fs.createReadStream(tmpFile);
let options = {
@ -237,7 +273,7 @@ class WebdavTools {
if (limit == null)
limit = 5;
return new Promise((resolve, reject) => {
this.getFolderContent(pathTools.auto).then(async (contents) => {
this.getFolderContent(this.getConf().back_dir + pathTools.auto).then(async (contents) => {
if (contents.length < limit) {
resolve();
return;

View File

@ -177,198 +177,9 @@
</div>
<div id="modal-settings-nextcloud" class="modal modal-fixed-footer blue-grey darken-4 white-text">
<div class="modal-content">
<div class="row">
<div class="col s12 center">
<h4>Nextcloud Settings</h4>
</div>
</div>
<div class="row">
<div class="col s12 center divider">
</div>
</div>
<div class="row hide">
<div class="col s12 center red-text" id="nextcloud_settings_message">
<%- include('modals/nextcloud-settings-modal') %>
</div>
</div>
<div class="row" style="margin-bottom: 10px;">
<div class="col s12" style="margin-bottom: 10px;">
<div style="color: #9e9e9e; display: inline;">SSL</div>
<div class="switch" style="display: inline;">
<label>
<input id="ssl" type="checkbox">
<span class="lever"></span>
</label>
</div>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="hostname" type="text" class="white-text">
<label for="hostname">Hostname</label>
<span class="helper-text">exemple.com:8080</span>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input id="username" type="text" class="white-text">
<label for="username">Username</label>
</div>
<div class="input-field col s6">
<input id="password" type="password" class="white-text">
<label for="password">Password</label>
<span class="helper-text">!!! Use App Password !!! See <a target="_blank"
href="https://github.com/Sebclem/hassio-nextcloud-backup#nextcloud-config">doc</a> for more
information.</span>
</div>
</div>
</div>
<div class="modal-footer blue-grey darken-4">
<a href="#" class="modal-close waves-effect btn red"><b>Cancel</b></a>
<a href="#" class="btn green waves-effect" style="margin-left: 5px;" id="save-nextcloud-settings"><b>Save</b></a>
</div>
</div>
<div id="modal-settings-backup" class="modal modal-fixed-footer blue-grey darken-4 white-text">
<div class="modal-content">
<div class="row">
<div class="col s12 center">
<h4>Backup Settings</h2>
</div>
</div>
<div class="row">
<div class="col s12 center divider">
</div>
</div>
<div class="row">
<div class="input-field col s12">
<select id="cron-drop-settings">
<option value="0">Disable</option>
<option value="1">Daily</option>
<option value="2">Weekly</option>
<option value="3">Monthly</option>
</select>
<label>Auto Backup</label>
</div>
</div>
<div class="row hide">
<div class="input-field col s12">
<input type="text" class="timepicker white-text" readonly="true" id="timepicker">
<label>Hour </label>
</div>
</div>
<div class="row hide">
<div class="input-field col s12">
<select id="cron-drop-day">
<option value="1">Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
<option value="5">Friday</option>
<option value="6">Saturday</option>
<option value="0">Sunday</option>
</select>
<label>Day</label>
</div>
</div>
<div class="row hide">
<div class="input-field col s2">
<input type="text" class="white-text" disabled readonly="true" id="cron-drop-day-month-read">
<label class="white-text active">Day of month</label>
</div>
<div class="input-field col s10">
<p class="range-field">
<input type="range" id="cron-drop-day-month" min="1" max="28" style="border: none;" />
</p>
</div>
</div>
<div class="row">
<div class="col s12">
</div>
</div>
<div class="row">
<div class="col s12 center">
<h4>Auto Clean Settings</h2>
</div>
</div>
<div class="row">
<div class="col s12 divider">
</div>
</div>
<div class="row" style="margin-bottom: 10px;">
<div class="col s12" style="margin-bottom: 10px;">
<div style="color: #9e9e9e; display: inline;">Auto Clean Local Snapshots</div>
<div class="switch" style="display: inline;">
<label>
<input id="auto_clean_local" type="checkbox">
<span class="lever"></span>
</label>
</div>
</div>
</div>
<div class="row">
<div class="input-field col s3">
<input type="text" class="white-text" disabled readonly="true" id="local-snap-keep-read">
<label class="white-text active">Local snapshot to keep</label>
</div>
<div class="input-field col s9">
<p class="range-field">
<input type="range" id="local-snap-keep" min="1" max="20" style="border: none;" />
</p>
</div>
</div>
<div class="row" style="margin-bottom: 10px;">
<div class="col s12" style="margin-bottom: 10px;">
<div style="color: #9e9e9e; display: inline;">Auto Clean Nextcloud Snapshots</div>
<div class="switch" style="display: inline;">
<label>
<input id="auto_clean_backup" type="checkbox">
<span class="lever"></span>
</label>
</div>
</div>
</div>
<div class="row">
<div class="input-field col s3">
<input type="text" class="white-text" disabled readonly="true" id="backup-snap-keep-read">
<label class="white-text active">Nextcloud snapshot to keep</label>
</div>
<div class="input-field col s9">
<p class="range-field">
<input type="range" id="backup-snap-keep" min="1" max="20" style="border: none;" />
</p>
</div>
</div>
</div>
<div class="modal-footer blue-grey darken-4">
<a href="#" class="modal-close waves-effect btn red"><b>Cancel</b></a>
<a href="#" class="btn green waves-effect" style="margin-left: 5px;" id="save-backup-settings"><b>Save</b></a>
</div>
</div>
<%- include('modals/backup-settings-modal') %>
<div id="modal-loading" class="modal blue-grey darken-4 white-text">
@ -631,7 +442,8 @@
let hostname = $('#hostname').val();
let username = $('#username').val();
let password = $('#password').val();
$.post('./api/nextcloud-settings', { ssl: ssl, host: hostname, username: username, password: password })
let back_dir = $('#back-dir').val();
$.post('./api/nextcloud-settings', { ssl: ssl, host: hostname, username: username, password: password, back_dir: back_dir })
.done((data) => {
console.log('Saved');
$('#nextcloud_settings_message').parent().addClass("hide");
@ -676,6 +488,9 @@
$('#password').val(data.password);
$('#password + label').removeClass("active");
$('#password + label').addClass("active");
$('#back-dir').val(data.back_dir);
$('#back-dir + label').removeClass("active");
$('#back-dir + label').addClass("active");
loadingModal.close();
});
}

View File

@ -0,0 +1,133 @@
<div id="modal-settings-backup" class="modal modal-fixed-footer blue-grey darken-4 white-text">
<div class="modal-content">
<div class="row">
<div class="col s12 center">
<h4>Backup Settings</h2>
</div>
</div>
<div class="row">
<div class="col s12 center divider">
</div>
</div>
<div class="row">
<div class="input-field col s12">
<select id="cron-drop-settings">
<option value="0">Disable</option>
<option value="1">Daily</option>
<option value="2">Weekly</option>
<option value="3">Monthly</option>
</select>
<label>Auto Backup</label>
</div>
</div>
<div class="row hide">
<div class="input-field col s12">
<input type="text" class="timepicker white-text" readonly="true" id="timepicker">
<label>Hour </label>
</div>
</div>
<div class="row hide">
<div class="input-field col s12">
<select id="cron-drop-day">
<option value="1">Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
<option value="5">Friday</option>
<option value="6">Saturday</option>
<option value="0">Sunday</option>
</select>
<label>Day</label>
</div>
</div>
<div class="row hide">
<div class="input-field col s2">
<input type="text" class="white-text" disabled readonly="true" id="cron-drop-day-month-read">
<label class="white-text active">Day of month</label>
</div>
<div class="input-field col s10">
<p class="range-field">
<input type="range" id="cron-drop-day-month" min="1" max="28" style="border: none;" />
</p>
</div>
</div>
<div class="row">
<div class="col s12">
</div>
</div>
<div class="row">
<div class="col s12 center">
<h4>Auto Clean Settings</h2>
</div>
</div>
<div class="row">
<div class="col s12 divider">
</div>
</div>
<div class="row" style="margin-bottom: 10px;">
<div class="col s12" style="margin-bottom: 10px;">
<div style="color: #9e9e9e; display: inline;">Auto Clean Local Snapshots</div>
<div class="switch" style="display: inline;">
<label>
<input id="auto_clean_local" type="checkbox">
<span class="lever"></span>
</label>
</div>
</div>
</div>
<div class="row">
<div class="input-field col s3">
<input type="text" class="white-text" disabled readonly="true" id="local-snap-keep-read">
<label class="white-text active">Local snapshot to keep</label>
</div>
<div class="input-field col s9">
<p class="range-field">
<input type="range" id="local-snap-keep" min="1" max="20" style="border: none;" />
</p>
</div>
</div>
<div class="row" style="margin-bottom: 10px;">
<div class="col s12" style="margin-bottom: 10px;">
<div style="color: #9e9e9e; display: inline;">Auto Clean Nextcloud Snapshots</div>
<div class="switch" style="display: inline;">
<label>
<input id="auto_clean_backup" type="checkbox">
<span class="lever"></span>
</label>
</div>
</div>
</div>
<div class="row">
<div class="input-field col s3">
<input type="text" class="white-text" disabled readonly="true" id="backup-snap-keep-read">
<label class="white-text active">Nextcloud snapshot to keep</label>
</div>
<div class="input-field col s9">
<p class="range-field">
<input type="range" id="backup-snap-keep" min="1" max="20" style="border: none;" />
</p>
</div>
</div>
</div>
<div class="modal-footer blue-grey darken-4">
<a href="#" class="modal-close waves-effect btn red"><b>Cancel</b></a>
<a href="#" class="btn green waves-effect" style="margin-left: 5px;" id="save-backup-settings"><b>Save</b></a>
</div>
</div>

View File

@ -0,0 +1,65 @@
<div id="modal-settings-nextcloud" class="modal modal-fixed-footer blue-grey darken-4 white-text">
<div class="modal-content">
<div class="row">
<div class="col s12 center">
<h4>Nextcloud Settings</h4>
</div>
</div>
<div class="row">
<div class="col s12 center divider">
</div>
</div>
<div class="row hide">
<div class="col s12 center red-text" id="nextcloud_settings_message">
</div>
</div>
<div class="row" style="margin-bottom: 10px;">
<div class="col s12" style="margin-bottom: 10px;">
<div style="color: #9e9e9e; display: inline;">SSL</div>
<div class="switch" style="display: inline;">
<label>
<input id="ssl" type="checkbox">
<span class="lever"></span>
</label>
</div>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="hostname" type="text" class="white-text">
<label for="hostname">Hostname</label>
<span class="helper-text">exemple.com:8080</span>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<input id="username" type="text" class="white-text">
<label for="username">Username</label>
</div>
<div class="input-field col s6">
<input id="password" type="password" class="white-text">
<label for="password">Password</label>
<span class="helper-text">!!! Use App Password !!! See <a target="_blank"
href="https://github.com/Sebclem/hassio-nextcloud-backup#nextcloud-config">doc</a> for more
information.</span>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="back-dir" type="text" class="white-text">
<label for="back-dir">Backup Directory</label>
<span class="helper-text">Default: /Hassio Backup/</span>
</div>
</div>
</div>
<div class="modal-footer blue-grey darken-4">
<a href="#" class="modal-close waves-effect btn red"><b>Cancel</b></a>
<a href="#" class="btn green waves-effect" style="margin-left: 5px;" id="save-nextcloud-settings"><b>Save</b></a>
</div>
</div>