🔨 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( 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;
}
}) })
res.render('localSnaps', { snaps: snaps, moment: moment }); 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) { router.get('/formated-backup-manual', function(req, res, next) {
webdav.getFolderContent(pathTools.manual) webdav.getFolderContent( webdav.getConf().back_dir + pathTools.manual)
.then((contents) => { .then((contents) => {
contents.sort((a, b) => { contents.sort((a, b) => {
if (moment(a.lastmod).isBefore(moment(b.lastmod))) 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) { 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) => { .then((contents) => {
contents.sort((a, b) => { contents.sort((a, b) => {
if (moment(a.lastmod).isBefore(moment(b.lastmod))) if (moment(a.lastmod).isBefore(moment(b.lastmod)))
@ -122,7 +127,7 @@ router.post('/manual-backup', function(req, res, next) {
hassioApiTools.downloadSnapshot(id) hassioApiTools.downloadSnapshot(id)
.then(() => { .then(() => {
webdav.uploadFile(id, pathTools.manual + name + '.tar'); webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + '.tar');
res.status(201); res.status(201);
res.send(); res.send();
}) })
@ -145,7 +150,7 @@ router.post('/new-backup', function(req, res, next) {
hassioApiTools.createNewBackup(name).then((id) => { hassioApiTools.createNewBackup(name).then((id) => {
hassioApiTools.downloadSnapshot(id) hassioApiTools.downloadSnapshot(id)
.then(() => { .then(() => {
webdav.uploadFile(id, pathTools.manual + name + '.tar'); webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + '.tar');
}).catch(() => { }).catch(() => {
}) })

View File

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

View File

@ -1,4 +1,5 @@
let root = '/Hassio Backup/';
exports.root = root; let default_root = '/Hassio Backup/';
exports.manual = root + 'Manual/'; exports.default_root = default_root;
exports.auto = root + 'Auto/'; 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() { initFolder() {
return new Promise((resolve, reject) => { return new Promise((resolve) => {
this.client.createDirectory(pathTools.root).catch(() => { }).then(() => { this.__createRoot().catch((err) => { logger.error(err)}).then(() => {
this.client.createDirectory(pathTools.auto).catch(() => { }).then(() => { this.client.createDirectory(this.getConf().back_dir + pathTools.auto).catch(() => { }).then(() => {
this.client.createDirectory(pathTools.manual).catch(() => { }).then(() => { this.client.createDirectory(this.getConf().back_dir + pathTools.manual).catch(() => { }).then(() => {
resolve(); resolve();
}) })
}) })
@ -105,6 +124,24 @@ class WebdavTools {
logger.error(status.message); logger.error(status.message);
reject("Nextcloud config invalid !"); 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 { else {
status.status = "error"; status.status = "error";
@ -147,7 +184,7 @@ class WebdavTools {
} }
_startUpload(id, path) { _startUpload(id, path) {
return new Promise( async (resolve, reject) => { return new Promise( (resolve, reject) => {
let status = statusTools.getStatus(); let status = statusTools.getStatus();
status.status = "upload"; status.status = "upload";
status.progress = 0; status.progress = 0;
@ -156,7 +193,6 @@ class WebdavTools {
statusTools.setStatus(status); statusTools.setStatus(status);
logger.info('Uploading snap...'); logger.info('Uploading snap...');
let tmpFile = `./temp/${id}.tar` let tmpFile = `./temp/${id}.tar`
let fileSize = fs.statSync(tmpFile).size;
let stream = fs.createReadStream(tmpFile); let stream = fs.createReadStream(tmpFile);
let options = { let options = {
@ -237,7 +273,7 @@ class WebdavTools {
if (limit == null) if (limit == null)
limit = 5; limit = 5;
return new Promise((resolve, reject) => { 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) { if (contents.length < limit) {
resolve(); resolve();
return; return;