diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/config/winston.js b/nextcloud_backup/rootfs/opt/nextcloud_backup/config/winston.js index 018c545..424cffc 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/config/winston.js +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/config/winston.js @@ -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}) ] }); diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/routes/api.js b/nextcloud_backup/rootfs/opt/nextcloud_backup/routes/api.js index f163947..2fdb47f 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/routes/api.js +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/routes/api.js @@ -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(() => { }) diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/cronTools.js b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/cronTools.js index 04b8854..1f98f5d 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/cronTools.js +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/cronTools.js @@ -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(() => { }) diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/pathTools.js b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/pathTools.js index e4777c6..0cc1c3d 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/pathTools.js +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/pathTools.js @@ -1,4 +1,5 @@ -let root = '/Hassio Backup/'; -exports.root = root; -exports.manual = root + 'Manual/'; -exports.auto = root + 'Auto/'; \ No newline at end of file + +let default_root = '/Hassio Backup/'; +exports.default_root = default_root; +exports.manual = 'Manual/'; +exports.auto = 'Auto/'; \ No newline at end of file diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/webdavTools.js b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/webdavTools.js index e277b87..b7de3d1 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/webdavTools.js +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/tools/webdavTools.js @@ -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; diff --git a/nextcloud_backup/rootfs/opt/nextcloud_backup/views/index.ejs b/nextcloud_backup/rootfs/opt/nextcloud_backup/views/index.ejs index 0baecd2..bb75d6c 100644 --- a/nextcloud_backup/rootfs/opt/nextcloud_backup/views/index.ejs +++ b/nextcloud_backup/rootfs/opt/nextcloud_backup/views/index.ejs @@ -177,198 +177,9 @@ -
- - + <%- include('modals/backup-settings-modal') %>