🔨 Add setting to specify backup directory

This commit is contained in:
SebClem 2020-09-12 16:47:32 +02:00
parent 4cdb25c091
commit 25618ccd91
4 changed files with 59 additions and 17 deletions

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;