mirror of
https://github.com/Sebclem/hassio-nextcloud-backup.git
synced 2024-12-23 22:46:44 +01:00
✏️ Clean code with prettier
This commit is contained in:
parent
1237f9c6ff
commit
8632a3d079
@ -2,19 +2,14 @@ module.exports = {
|
||||
"env": {
|
||||
"browser": true,
|
||||
"commonjs": true,
|
||||
"es6": true,
|
||||
"es2021": true,
|
||||
"node": true
|
||||
},
|
||||
"extends": "eslint:recommended",
|
||||
"globals": {
|
||||
"Atomics": "readonly",
|
||||
"SharedArrayBuffer": "readonly"
|
||||
},
|
||||
"extends": ["eslint:recommended","prettier"],
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2018
|
||||
"ecmaVersion": 12
|
||||
},
|
||||
"rules": {
|
||||
"no-unused-vars": "warn"
|
||||
|
||||
}
|
||||
};
|
||||
};
|
||||
|
@ -1,30 +1,31 @@
|
||||
const createError = require('http-errors');
|
||||
const express = require('express');
|
||||
const path = require('path');
|
||||
const cookieParser = require('cookie-parser');
|
||||
const logger = require('morgan');
|
||||
const createError = require("http-errors");
|
||||
const express = require("express");
|
||||
const path = require("path");
|
||||
const cookieParser = require("cookie-parser");
|
||||
const logger = require("morgan");
|
||||
|
||||
|
||||
const indexRouter = require('./routes/index');
|
||||
const apiRouter = require('./routes/api');
|
||||
const indexRouter = require("./routes/index");
|
||||
const apiRouter = require("./routes/api");
|
||||
|
||||
const app = express();
|
||||
// view engine setup
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
app.set('view engine', 'ejs');
|
||||
app.set("views", path.join(__dirname, "views"));
|
||||
app.set("view engine", "ejs");
|
||||
|
||||
app.use(logger('dev', {
|
||||
skip: function (req, res) {
|
||||
return res.statusCode = 304
|
||||
}
|
||||
}));
|
||||
app.use(
|
||||
logger("dev", {
|
||||
skip: function (req, res) {
|
||||
return (res.statusCode = 304);
|
||||
},
|
||||
})
|
||||
);
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({extended: false}));
|
||||
app.use(express.urlencoded({ extended: false }));
|
||||
app.use(cookieParser());
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.use(express.static(path.join(__dirname, "public")));
|
||||
|
||||
app.use('/', indexRouter);
|
||||
app.use('/api', apiRouter);
|
||||
app.use("/", indexRouter);
|
||||
app.use("/api", apiRouter);
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use(function (req, res, next) {
|
||||
@ -35,43 +36,43 @@ app.use(function (req, res, next) {
|
||||
app.use(function (err, req, res, next) {
|
||||
// set locals, only providing error in development
|
||||
res.locals.message = err.message;
|
||||
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
||||
res.locals.error = req.app.get("env") === "development" ? err : {};
|
||||
|
||||
// render the error page
|
||||
res.status(err.status || 500);
|
||||
res.render('error');
|
||||
res.render("error");
|
||||
});
|
||||
|
||||
const fs = require('fs');
|
||||
const newlog = require('./config/winston');
|
||||
if (!fs.existsSync('/data'))
|
||||
fs.mkdirSync('/data');
|
||||
const statusTools = require('./tools/status');
|
||||
const fs = require("fs");
|
||||
const newlog = require("./config/winston");
|
||||
if (!fs.existsSync("/data")) fs.mkdirSync("/data");
|
||||
const statusTools = require("./tools/status");
|
||||
statusTools.init();
|
||||
newlog.info("Satus : \x1b[32mGo !\x1b[0m");
|
||||
const hassioApiTools = require('./tools/hassioApiTools');
|
||||
const hassioApiTools = require("./tools/hassioApiTools");
|
||||
hassioApiTools.getSnapshots().then(
|
||||
() => {
|
||||
newlog.info("Hassio API : \x1b[32mGo !\x1b[0m")
|
||||
|
||||
}, (err) => {
|
||||
newlog.error("Hassio API : \x1b[31;1mFAIL !\x1b[0m")
|
||||
newlog.info("Hassio API : \x1b[32mGo !\x1b[0m");
|
||||
},
|
||||
(err) => {
|
||||
newlog.error("Hassio API : \x1b[31;1mFAIL !\x1b[0m");
|
||||
newlog.error("... " + err);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
const WebdavTools = require('./tools/webdavTools');
|
||||
const WebdavTools = require("./tools/webdavTools");
|
||||
const webdav = new WebdavTools().getInstance();
|
||||
webdav.confIsValid().then(
|
||||
() => {
|
||||
newlog.info("Nextcloud connection : \x1b[32mGo !\x1b[0m")
|
||||
}, (err) => {
|
||||
newlog.error("Nextcloud connection : \x1b[31;1mFAIL !\x1b[0m")
|
||||
newlog.info("Nextcloud connection : \x1b[32mGo !\x1b[0m");
|
||||
},
|
||||
(err) => {
|
||||
newlog.error("Nextcloud connection : \x1b[31;1mFAIL !\x1b[0m");
|
||||
newlog.error("... " + err);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
const cronTools = require('./tools/cronTools');
|
||||
const cronTools = require("./tools/cronTools");
|
||||
cronTools.startCron();
|
||||
|
||||
|
||||
module.exports = app;
|
||||
|
@ -1,18 +1,17 @@
|
||||
const appRoot = require('app-root-path');
|
||||
const winston = require('winston');
|
||||
|
||||
const appRoot = require("app-root-path");
|
||||
const winston = require("winston");
|
||||
|
||||
const logger = winston.createLogger({
|
||||
level: 'debug',
|
||||
level: "debug",
|
||||
format: winston.format.combine(
|
||||
winston.format.timestamp({
|
||||
format: 'YYYY-MM-DD HH:mm:ss'
|
||||
format: "YYYY-MM-DD HH:mm:ss",
|
||||
}),
|
||||
// winston.format.errors({ stack: true }),
|
||||
// winston.format.splat(),
|
||||
|
||||
winston.format.colorize(),
|
||||
winston.format.printf(({level, message, timestamp}) => {
|
||||
winston.format.printf(({ level, message, timestamp }) => {
|
||||
return `[${timestamp}] [${level}]: ${message}`;
|
||||
})
|
||||
),
|
||||
@ -21,10 +20,9 @@ const logger = winston.createLogger({
|
||||
// - Write to all logs with level `info` and below to `quick-start-combined.log`.
|
||||
// - Write all logs error (and below) to `quick-start-error.log`.
|
||||
//
|
||||
new winston.transports.Console({handleExceptions: true}),
|
||||
new winston.transports.Console({ handleExceptions: true }),
|
||||
// new winston.transports.File({filename: '/data/NCB.log', handleExceptions: true})
|
||||
]
|
||||
],
|
||||
});
|
||||
|
||||
|
||||
module.exports = logger;
|
||||
module.exports = logger;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -22,7 +22,10 @@
|
||||
"winston": "^3.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^6.7.2",
|
||||
"eslint-config-google": "^0.14.0"
|
||||
"eslint": "^7.13.0",
|
||||
"eslint-config-eslint": "^6.0.0",
|
||||
"eslint-config-prettier": "^6.15.0",
|
||||
"eslint-plugin-import": "^2.22.1",
|
||||
"eslint-plugin-node": "^11.1.0"
|
||||
}
|
||||
}
|
||||
|
@ -1,122 +1,106 @@
|
||||
const express = require('express');
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const moment = require('moment');
|
||||
const statusTools = require('../tools/status');
|
||||
const WebdavTools = require('../tools/webdavTools')
|
||||
const moment = require("moment");
|
||||
const statusTools = require("../tools/status");
|
||||
const WebdavTools = require("../tools/webdavTools");
|
||||
const webdav = new WebdavTools().getInstance();
|
||||
const settingsTools = require('../tools/settingsTools');
|
||||
const pathTools = require('../tools/pathTools');
|
||||
const hassioApiTools = require('../tools/hassioApiTools');
|
||||
const humanFileSize = require('../tools/toolbox').humanFileSize;
|
||||
const settingsTools = require("../tools/settingsTools");
|
||||
const pathTools = require("../tools/pathTools");
|
||||
const hassioApiTools = require("../tools/hassioApiTools");
|
||||
const humanFileSize = require("../tools/toolbox").humanFileSize;
|
||||
|
||||
const cronTools = require('../tools/cronTools');
|
||||
const cronTools = require("../tools/cronTools");
|
||||
|
||||
const logger = require('../config/winston');
|
||||
const logger = require("../config/winston");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
router.get('/status', (req, res, next) => {
|
||||
router.get("/status", (req, res, next) => {
|
||||
cronTools.updatetNextDate();
|
||||
let status = statusTools.getStatus();
|
||||
res.json(status);
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
router.get('/formated-local-snap', function(req, res, next) {
|
||||
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
|
||||
{
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
})
|
||||
res.render('localSnaps', { snaps: snaps, moment: moment });
|
||||
});
|
||||
res.render("localSnaps", { snaps: snaps, moment: moment });
|
||||
},
|
||||
(err) => {
|
||||
logger.error(err);
|
||||
res.status(500);
|
||||
res.send('');
|
||||
res.send("");
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
router.get("/formated-backup-manual", function (req, res, next) {
|
||||
webdav
|
||||
.getFolderContent(webdav.getConf().back_dir + pathTools.manual)
|
||||
.then((contents) => {
|
||||
contents.sort((a, b) => {
|
||||
if (moment(a.lastmod).isBefore(moment(b.lastmod))) return 1;
|
||||
else return -1;
|
||||
});
|
||||
res.render("backupSnaps", { backups: contents, moment: moment, humanFileSize: humanFileSize });
|
||||
})
|
||||
|
||||
});
|
||||
|
||||
router.get('/formated-backup-manual', function(req, res, next) {
|
||||
webdav.getFolderContent( webdav.getConf().back_dir + pathTools.manual)
|
||||
.then((contents) => {
|
||||
contents.sort((a, b) => {
|
||||
if (moment(a.lastmod).isBefore(moment(b.lastmod)))
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
})
|
||||
res.render('backupSnaps',{backups: contents, moment: moment, humanFileSize: humanFileSize});
|
||||
}).catch(()=>{
|
||||
res.send();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
router.get('/formated-backup-auto', function(req, res, next) {
|
||||
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)))
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
})
|
||||
res.render('backupSnaps',{backups: contents, moment: moment, humanFileSize: humanFileSize});
|
||||
}).catch(()=>{
|
||||
.catch(() => {
|
||||
res.send();
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/formated-backup-auto", function (req, res, next) {
|
||||
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))) return 1;
|
||||
else return -1;
|
||||
});
|
||||
res.render("backupSnaps", { backups: contents, moment: moment, humanFileSize: humanFileSize });
|
||||
})
|
||||
.catch(() => {
|
||||
res.send();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
router.post('/nextcloud-settings', function(req, res, next) {
|
||||
router.post("/nextcloud-settings", function (req, res, next) {
|
||||
let settings = req.body;
|
||||
if (settings.ssl != null && settings.host != null && settings.host != "" && settings.username != null && settings.password != null) {
|
||||
webdav.setConf(settings);
|
||||
webdav.confIsValid().then(() => {
|
||||
res.status(201);
|
||||
res.send();
|
||||
}).catch((err) => {
|
||||
res.status(406);
|
||||
res.json({ message: err });
|
||||
});
|
||||
|
||||
}
|
||||
else {
|
||||
webdav
|
||||
.confIsValid()
|
||||
.then(() => {
|
||||
res.status(201);
|
||||
res.send();
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(406);
|
||||
res.json({ message: err });
|
||||
});
|
||||
} else {
|
||||
res.status(400);
|
||||
res.send();
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/nextcloud-settings', function(req, res, next) {
|
||||
router.get("/nextcloud-settings", function (req, res, next) {
|
||||
let conf = webdav.getConf();
|
||||
if (conf == null) {
|
||||
res.status(404);
|
||||
res.send();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
res.json(conf);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
router.post('/manual-backup', function(req, res, next) {
|
||||
router.post("/manual-backup", function (req, res, next) {
|
||||
let id = req.query.id;
|
||||
let name = req.query.name;
|
||||
let status = statusTools.getStatus();
|
||||
@ -126,84 +110,81 @@ router.post('/manual-backup', function(req, res, next) {
|
||||
return;
|
||||
}
|
||||
|
||||
hassioApiTools.downloadSnapshot(id)
|
||||
hassioApiTools
|
||||
.downloadSnapshot(id)
|
||||
.then(() => {
|
||||
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + '.tar');
|
||||
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + ".tar");
|
||||
res.status(201);
|
||||
res.send();
|
||||
})
|
||||
.catch(() => {
|
||||
res.status(500)
|
||||
res.status(500);
|
||||
res.send();
|
||||
})
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
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');
|
||||
hassioApiTools.createNewBackup(name).then((id) => {
|
||||
hassioApiTools.downloadSnapshot(id)
|
||||
.then(() => {
|
||||
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + '.tar');
|
||||
}).catch(() => {
|
||||
|
||||
})
|
||||
}).catch(() => {
|
||||
|
||||
})
|
||||
let name = "Manual-" + moment().format("YYYY-MM-DD_HH-mm");
|
||||
hassioApiTools
|
||||
.createNewBackup(name)
|
||||
.then((id) => {
|
||||
hassioApiTools
|
||||
.downloadSnapshot(id)
|
||||
.then(() => {
|
||||
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + ".tar");
|
||||
})
|
||||
.catch(() => {});
|
||||
})
|
||||
.catch(() => {});
|
||||
res.status(201);
|
||||
res.send();
|
||||
});
|
||||
|
||||
|
||||
router.get('/backup-settings', function(req, res, next) {
|
||||
router.get("/backup-settings", function (req, res, next) {
|
||||
res.send(settingsTools.getSettings());
|
||||
});
|
||||
|
||||
router.post('/backup-settings', function(req, res, next) {
|
||||
router.post("/backup-settings", function (req, res, next) {
|
||||
if (cronTools.checkConfig(req.body)) {
|
||||
settingsTools.setSettings(req.body);
|
||||
cronTools.startCron();
|
||||
res.send();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
res.status(400);
|
||||
res.send();
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/clean-now', function(req, res, next){
|
||||
webdav.clean().then(()=>{
|
||||
hassioApiTools.clean().catch();
|
||||
}).catch(()=>{
|
||||
hassioApiTools.clean().catch();
|
||||
});
|
||||
router.post("/clean-now", function (req, res, next) {
|
||||
webdav
|
||||
.clean()
|
||||
.then(() => {
|
||||
hassioApiTools.clean().catch();
|
||||
})
|
||||
.catch(() => {
|
||||
hassioApiTools.clean().catch();
|
||||
});
|
||||
res.status(201);
|
||||
res.send()
|
||||
res.send();
|
||||
});
|
||||
|
||||
|
||||
router.post('/restore', function(req, res, next){
|
||||
if(req.body['path'] != null){
|
||||
webdav.downloadFile(req.body['path'] ).then((path)=>{
|
||||
router.post("/restore", function (req, res, next) {
|
||||
if (req.body["path"] != null) {
|
||||
webdav.downloadFile(req.body["path"]).then((path) => {
|
||||
hassioApiTools.uploadSnapshot(path);
|
||||
});
|
||||
res.status(200);
|
||||
res.send()
|
||||
}
|
||||
else{
|
||||
res.send();
|
||||
} else {
|
||||
res.status(400);
|
||||
res.send()
|
||||
res.send();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
module.exports = router;
|
||||
|
@ -1,11 +1,9 @@
|
||||
const express = require('express');
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
|
||||
/* GET home page. */
|
||||
router.get('/', function(req, res, next) {
|
||||
res.render('index');
|
||||
router.get("/", function (req, res, next) {
|
||||
res.render("index");
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
|
@ -1,43 +1,36 @@
|
||||
const settingsTools = require('./settingsTools');
|
||||
const settingsTools = require("./settingsTools");
|
||||
|
||||
const WebdavTools = require('./webdavTools');
|
||||
const WebdavTools = require("./webdavTools");
|
||||
const webdav = new WebdavTools().getInstance();
|
||||
|
||||
const hassioApiTools = require('./hassioApiTools');
|
||||
const hassioApiTools = require("./hassioApiTools");
|
||||
|
||||
const statusTools = require('./status');
|
||||
const statusTools = require("./status");
|
||||
|
||||
const pathTools = require('./pathTools');
|
||||
|
||||
const CronJob = require('cron').CronJob;
|
||||
const moment = require('moment');
|
||||
const logger = require('../config/winston');
|
||||
const pathTools = require("./pathTools");
|
||||
|
||||
const CronJob = require("cron").CronJob;
|
||||
const moment = require("moment");
|
||||
const logger = require("../config/winston");
|
||||
|
||||
function checkConfig(conf) {
|
||||
if (conf.cron_base != null) {
|
||||
if (conf.cron_base === '1' || conf.cron_base === '2' || conf.cron_base === '3') {
|
||||
if (conf.cron_base === "1" || conf.cron_base === "2" || conf.cron_base === "3") {
|
||||
if (conf.cron_hour != null && conf.cron_hour.match(/\d\d:\d\d/)) {
|
||||
if (conf.cron_base === '1')
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
if (conf.cron_base === "1") return true;
|
||||
} else return false;
|
||||
}
|
||||
|
||||
if (conf.cron_base === '2') {
|
||||
if (conf.cron_base === "2") {
|
||||
return conf.cron_weekday != null && conf.cron_weekday >= 0 && conf.cron_weekday <= 6;
|
||||
}
|
||||
|
||||
if (conf.cron_base === '3') {
|
||||
if (conf.cron_base === "3") {
|
||||
return conf.cron_month_day != null && conf.cron_month_day >= 1 && conf.cron_month_day <= 28;
|
||||
}
|
||||
|
||||
if (conf.cron_base === '0')
|
||||
return true
|
||||
}
|
||||
else
|
||||
return false;
|
||||
if (conf.cron_base === "0") return true;
|
||||
} else return false;
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -52,19 +45,18 @@ function updatetNextDate() {
|
||||
cronContainer.updatetNextDate();
|
||||
}
|
||||
|
||||
|
||||
class CronContainer {
|
||||
constructor() {
|
||||
this.cronJob = null;
|
||||
this.cronClean = null
|
||||
this.cronClean = null;
|
||||
}
|
||||
|
||||
init() {
|
||||
let settings = settingsTools.getSettings();
|
||||
let cronStr = "";
|
||||
if (this.cronClean == null) {
|
||||
logger.info("Starting auto clean cron...");
|
||||
this.cronClean = new CronJob('0 1 * * *', this._clean, null, false, Intl.DateTimeFormat().resolvedOptions().timeZone);
|
||||
logger.info("Starting auto clean cron...");
|
||||
this.cronClean = new CronJob("0 1 * * *", this._clean, null, false, Intl.DateTimeFormat().resolvedOptions().timeZone);
|
||||
this.cronClean.start();
|
||||
}
|
||||
if (this.cronJob != null) {
|
||||
@ -78,28 +70,26 @@ class CronContainer {
|
||||
}
|
||||
|
||||
switch (settings.cron_base) {
|
||||
case '0':
|
||||
case "0":
|
||||
logger.warn("No Cron settings available.");
|
||||
return;
|
||||
case '1': {
|
||||
let splited = settings.cron_hour.split(':');
|
||||
case "1": {
|
||||
let splited = settings.cron_hour.split(":");
|
||||
cronStr = "" + splited[1] + " " + splited[0] + " * * *";
|
||||
break;
|
||||
}
|
||||
|
||||
case '2': {
|
||||
let splited = settings.cron_hour.split(':');
|
||||
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(':');
|
||||
case "3": {
|
||||
let splited = settings.cron_hour.split(":");
|
||||
cronStr = "" + splited[1] + " " + splited[0] + " " + settings.cron_month_day + " * *";
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
logger.info("Starting Cron...");
|
||||
this.cronJob = new CronJob(cronStr, this._createBackup, null, false, Intl.DateTimeFormat().resolvedOptions().timeZone);
|
||||
@ -109,32 +99,30 @@ class CronContainer {
|
||||
|
||||
updatetNextDate() {
|
||||
let date;
|
||||
if (this.cronJob == null)
|
||||
date = "Not configured";
|
||||
else
|
||||
date = this.cronJob.nextDate().format('MMM D, YYYY HH:mm');
|
||||
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() {
|
||||
logger.debug('Cron triggered !');
|
||||
logger.debug("Cron triggered !");
|
||||
let status = statusTools.getStatus();
|
||||
if (status.status === "creating" || status.status === "upload" || status.status === "download")
|
||||
return;
|
||||
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, webdav.getConf().back_dir + pathTools.auto + name + '.tar');
|
||||
}).catch(() => {
|
||||
|
||||
})
|
||||
}).catch(() => {
|
||||
|
||||
})
|
||||
let name = "Auto-" + moment().format("YYYY-MM-DD_HH-mm");
|
||||
hassioApiTools
|
||||
.createNewBackup(name)
|
||||
.then((id) => {
|
||||
hassioApiTools
|
||||
.downloadSnapshot(id)
|
||||
.then(() => {
|
||||
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.auto + name + ".tar");
|
||||
})
|
||||
.catch(() => {});
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
_clean() {
|
||||
@ -146,11 +134,9 @@ class CronContainer {
|
||||
if (autoCleanCloud != null && autoCleanCloud === "true") {
|
||||
webdav.clean().catch(() => {});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Singleton {
|
||||
constructor() {
|
||||
if (!Singleton.instance) {
|
||||
@ -165,4 +151,4 @@ class Singleton {
|
||||
|
||||
exports.checkConfig = checkConfig;
|
||||
exports.startCron = startCron;
|
||||
exports.updatetNextDate = updatetNextDate;
|
||||
exports.updatetNextDate = updatetNextDate;
|
||||
|
@ -1,25 +1,25 @@
|
||||
const statusTools = require('./status');
|
||||
const fs = require('fs');
|
||||
const settingsTools = require('./settingsTools');
|
||||
const moment = require('moment');
|
||||
const logger = require('../config/winston');
|
||||
const stream = require('stream');
|
||||
const {promisify} = require('util');
|
||||
const fs = require("fs");
|
||||
|
||||
const moment = require("moment");
|
||||
const stream = require("stream");
|
||||
const { promisify } = require("util");
|
||||
|
||||
const pipeline = promisify(stream.pipeline);
|
||||
const got = require ('got');
|
||||
const FormData = require('form-data');
|
||||
|
||||
const create_snap_timeout = 90 * 60 * 1000
|
||||
|
||||
const got = require("got");
|
||||
const FormData = require("form-data");
|
||||
const statusTools = require("./status");
|
||||
const settingsTools = require("./settingsTools");
|
||||
const logger = require("../config/winston");
|
||||
|
||||
const create_snap_timeout = 90 * 60 * 1000;
|
||||
|
||||
function getSnapshots() {
|
||||
return new Promise((resolve, reject) => {
|
||||
let token = process.env.HASSIO_TOKEN;
|
||||
let status = statusTools.getStatus();
|
||||
let option = {
|
||||
headers: { 'X-HASSIO-KEY': token },
|
||||
responseType: 'json'
|
||||
headers: { "X-HASSIO-KEY": token },
|
||||
responseType: "json",
|
||||
};
|
||||
|
||||
got("http://hassio/snapshots", option)
|
||||
@ -42,41 +42,43 @@ function getSnapshots() {
|
||||
reject(error.message);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function downloadSnapshot(id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
logger.info(`Downloading snapshot ${id}...`);
|
||||
if (!fs.existsSync('./temp/'))
|
||||
fs.mkdirSync('./temp/');
|
||||
let tmp_file = `./temp/${id}.tar`
|
||||
if (!fs.existsSync("./temp/")) fs.mkdirSync("./temp/");
|
||||
let tmp_file = `./temp/${id}.tar`;
|
||||
let stream = fs.createWriteStream(tmp_file);
|
||||
let token = process.env.HASSIO_TOKEN;
|
||||
let status = statusTools.getStatus();
|
||||
checkSnap(id).then(() => {
|
||||
checkSnap(id)
|
||||
.then(() => {
|
||||
status.status = "download";
|
||||
status.progress = 0;
|
||||
statusTools.setStatus(status);
|
||||
let option = {
|
||||
headers: { 'X-HASSIO-KEY': token },
|
||||
headers: { "X-HASSIO-KEY": token },
|
||||
};
|
||||
|
||||
pipeline(got.stream.get(`http://hassio/snapshots/${id}/download`, option)
|
||||
.on('downloadProgress', e => {
|
||||
pipeline(
|
||||
got.stream.get(`http://hassio/snapshots/${id}/download`, option).on("downloadProgress", (e) => {
|
||||
let percent = Math.round(e.percent * 100) / 100;
|
||||
if (status.progress != percent) {
|
||||
status.progress = percent;
|
||||
statusTools.setStatus(status);
|
||||
}
|
||||
}), stream)
|
||||
.then((res)=>{
|
||||
logger.info('Download success !')
|
||||
}),
|
||||
stream
|
||||
)
|
||||
.then((res) => {
|
||||
logger.info("Download success !");
|
||||
status.progress = 1;
|
||||
statusTools.setStatus(status);
|
||||
logger.debug("Snapshot dl size : " + (fs.statSync(tmp_file).size / 1024 / 1024));
|
||||
logger.debug("Snapshot dl size : " + fs.statSync(tmp_file).size / 1024 / 1024);
|
||||
resolve();
|
||||
}).catch((err)=>{
|
||||
})
|
||||
.catch((err) => {
|
||||
fs.unlinkSync(tmp_file);
|
||||
status.status = "error";
|
||||
status.message = "Fail to download Hassio snapshot (" + err.message + ")";
|
||||
@ -84,8 +86,9 @@ function downloadSnapshot(id) {
|
||||
statusTools.setStatus(status);
|
||||
logger.error(status.message);
|
||||
reject(err.message);
|
||||
})
|
||||
}).catch((err) => {
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
status.status = "error";
|
||||
status.message = "Fail to download Hassio snapshot. Not found ?";
|
||||
status.error_code = 7;
|
||||
@ -93,18 +96,18 @@ function downloadSnapshot(id) {
|
||||
logger.error(status.message);
|
||||
reject();
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
function dellSnap(id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
checkSnap(id).then(() => {
|
||||
checkSnap(id)
|
||||
.then(() => {
|
||||
let token = process.env.HASSIO_TOKEN;
|
||||
|
||||
let option = {
|
||||
headers: { 'X-HASSIO-KEY': token },
|
||||
responseType: 'json'
|
||||
headers: { "X-HASSIO-KEY": token },
|
||||
responseType: "json",
|
||||
};
|
||||
|
||||
got.post(`http://hassio/snapshots/${id}/remove`, option)
|
||||
@ -114,48 +117,45 @@ function dellSnap(id) {
|
||||
.catch((error) => {
|
||||
reject();
|
||||
});
|
||||
}).catch(() => {
|
||||
reject();
|
||||
})
|
||||
})
|
||||
|
||||
.catch(() => {
|
||||
reject();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function checkSnap(id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let token = process.env.HASSIO_TOKEN;
|
||||
let option = {
|
||||
headers: { 'X-HASSIO-KEY': token },
|
||||
responseType: 'json'
|
||||
headers: { "X-HASSIO-KEY": token },
|
||||
responseType: "json",
|
||||
};
|
||||
|
||||
got(`http://hassio/snapshots/${id}/info`, option)
|
||||
.then((result) => {
|
||||
logger.debug(`Snapshot size: ${result.body.data.size}`)
|
||||
logger.debug(`Snapshot size: ${result.body.data.size}`);
|
||||
resolve();
|
||||
})
|
||||
.catch((error) => {
|
||||
reject();
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
function createNewBackup(name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let status = statusTools.getStatus();
|
||||
status.status = "creating";
|
||||
status.progress = -1;
|
||||
statusTools.setStatus(status);
|
||||
logger.info("Creating new snapshot...")
|
||||
logger.info("Creating new snapshot...");
|
||||
let token = process.env.HASSIO_TOKEN;
|
||||
let option = {
|
||||
headers: { 'X-HASSIO-KEY': token },
|
||||
responseType: 'json',
|
||||
headers: { "X-HASSIO-KEY": token },
|
||||
responseType: "json",
|
||||
timeout: create_snap_timeout,
|
||||
json: { name: name }
|
||||
|
||||
json: { name: name },
|
||||
};
|
||||
|
||||
got.post(`http://hassio/snapshots/new/full`, option)
|
||||
@ -171,75 +171,71 @@ function createNewBackup(name) {
|
||||
logger.error(status.message);
|
||||
reject(status.message);
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
function clean() {
|
||||
let limit = settingsTools.getSettings().auto_clean_local_keep;
|
||||
if (limit == null)
|
||||
limit = 5;
|
||||
if (limit == null) limit = 5;
|
||||
return new Promise((resolve, reject) => {
|
||||
getSnapshots().then(async (snaps) => {
|
||||
getSnapshots()
|
||||
.then(async (snaps) => {
|
||||
if (snaps.length < limit) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
snaps.sort((a, b) => {
|
||||
if (moment(a.date).isBefore(moment(b.date)))
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
if (moment(a.date).isBefore(moment(b.date))) return 1;
|
||||
else return -1;
|
||||
});
|
||||
let toDel = snaps.slice(limit);
|
||||
for (let i in toDel) {
|
||||
await dellSnap(toDel[i].slug)
|
||||
await dellSnap(toDel[i].slug);
|
||||
}
|
||||
logger.info('Local clean done.')
|
||||
logger.info("Local clean done.");
|
||||
resolve();
|
||||
}).catch(() => {
|
||||
})
|
||||
.catch(() => {
|
||||
reject();
|
||||
});
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
function uploadSnapshot(path) {
|
||||
return new Promise( (resolve, reject) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let status = statusTools.getStatus();
|
||||
status.status = "upload-b";
|
||||
status.progress = 0;
|
||||
status.message = null;
|
||||
status.error_code = null;
|
||||
statusTools.setStatus(status);
|
||||
logger.info('Uploading backup...');
|
||||
let stream = fs.createReadStream(path);
|
||||
logger.info("Uploading backup...");
|
||||
let stream = fs.createReadStream(path);
|
||||
let token = process.env.HASSIO_TOKEN;
|
||||
|
||||
|
||||
let form = new FormData();
|
||||
form.append('file', stream)
|
||||
|
||||
form.append("file", stream);
|
||||
|
||||
let options = {
|
||||
body: form,
|
||||
username: this.username,
|
||||
password: this.password,
|
||||
headers: { 'X-HASSIO-KEY': token },
|
||||
}
|
||||
headers: { "X-HASSIO-KEY": token },
|
||||
};
|
||||
|
||||
got.stream.post(`http://hassio/snapshots/new/upload`, options).on('uploadProgress', e => {
|
||||
got.stream
|
||||
.post(`http://hassio/snapshots/new/upload`, options)
|
||||
.on("uploadProgress", (e) => {
|
||||
let percent = e.percent;
|
||||
if (status.progress != percent) {
|
||||
status.progress = percent;
|
||||
statusTools.setStatus(status);
|
||||
}
|
||||
if (percent >= 1) {
|
||||
logger.info('Upload done...');
|
||||
logger.info("Upload done...");
|
||||
}
|
||||
}).on('response', res => {
|
||||
|
||||
})
|
||||
.on("response", (res) => {
|
||||
if (res.statusCode != 200) {
|
||||
status.status = "error";
|
||||
status.error_code = 4;
|
||||
@ -258,8 +254,8 @@ function uploadSnapshot(path) {
|
||||
fs.unlinkSync(path);
|
||||
resolve();
|
||||
}
|
||||
}).on('error', err => {
|
||||
|
||||
})
|
||||
.on("error", (err) => {
|
||||
fs.unlinkSync(path);
|
||||
status.status = "error";
|
||||
status.error_code = 4;
|
||||
@ -268,14 +264,11 @@ function uploadSnapshot(path) {
|
||||
logger.error(status.message);
|
||||
reject(status.message);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
exports.getSnapshots = getSnapshots;
|
||||
exports.downloadSnapshot = downloadSnapshot;
|
||||
exports.createNewBackup = createNewBackup;
|
||||
exports.uploadSnapshot = uploadSnapshot;
|
||||
exports.clean = clean;
|
||||
exports.clean = clean;
|
||||
|
@ -1,5 +1,4 @@
|
||||
|
||||
let default_root = '/Hassio Backup/';
|
||||
let default_root = "/Hassio Backup/";
|
||||
exports.default_root = default_root;
|
||||
exports.manual = 'Manual/';
|
||||
exports.auto = 'Auto/';
|
||||
exports.manual = "Manual/";
|
||||
exports.auto = "Auto/";
|
||||
|
@ -1,26 +1,19 @@
|
||||
const fs = require('fs');
|
||||
const fs = require("fs");
|
||||
|
||||
const settingsPath = "/data/backup_conf.json";
|
||||
|
||||
|
||||
function getSettings(){
|
||||
|
||||
function getSettings() {
|
||||
if (!fs.existsSync(settingsPath)) {
|
||||
return {};
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
let rawSettings = fs.readFileSync(settingsPath);
|
||||
return JSON.parse(rawSettings);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function setSettings(settings){
|
||||
function setSettings(settings) {
|
||||
fs.writeFileSync(settingsPath, JSON.stringify(settings));
|
||||
}
|
||||
|
||||
|
||||
exports.getSettings = getSettings;
|
||||
exports.setSettings = setSettings;
|
||||
|
||||
|
||||
|
@ -1,31 +1,27 @@
|
||||
const fs = require('fs');
|
||||
const fs = require("fs");
|
||||
|
||||
const statusPath = '/data/status.json';
|
||||
const statusPath = "/data/status.json";
|
||||
|
||||
let baseStatus = {
|
||||
status: "idle",
|
||||
last_backup: null,
|
||||
next_backup: null
|
||||
next_backup: null,
|
||||
};
|
||||
|
||||
|
||||
|
||||
function init() {
|
||||
if (!fs.existsSync(statusPath)) {
|
||||
fs.writeFileSync(statusPath, JSON.stringify(baseStatus));
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
let content = getStatus();
|
||||
if(content.status !== "idle"){
|
||||
if (content.status !== "idle") {
|
||||
content.status = "idle";
|
||||
content.message = null;
|
||||
setStatus(content)
|
||||
setStatus(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function getStatus(){
|
||||
function getStatus() {
|
||||
if (!fs.existsSync(statusPath)) {
|
||||
fs.writeFileSync(statusPath, JSON.stringify(baseStatus));
|
||||
}
|
||||
@ -34,12 +30,10 @@ function getStatus(){
|
||||
return JSON.parse(content);
|
||||
}
|
||||
|
||||
function setStatus(state){
|
||||
function setStatus(state) {
|
||||
fs.writeFileSync(statusPath, JSON.stringify(state));
|
||||
}
|
||||
|
||||
|
||||
|
||||
exports.init = init;
|
||||
exports.getStatus = getStatus;
|
||||
exports.setStatus = setStatus;
|
||||
exports.setStatus = setStatus;
|
||||
|
@ -1,23 +1,21 @@
|
||||
|
||||
// Found on Stackoverflow, perfect code :D https://stackoverflow.com/a/14919494/8654475
|
||||
function humanFileSize(bytes, si=false, dp=1) {
|
||||
function humanFileSize(bytes, si = false, dp = 1) {
|
||||
const thresh = si ? 1000 : 1024;
|
||||
|
||||
|
||||
if (Math.abs(bytes) < thresh) {
|
||||
return bytes + ' B';
|
||||
return bytes + " B";
|
||||
}
|
||||
|
||||
const units = si ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
||||
|
||||
const units = si ? ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] : ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
|
||||
let u = -1;
|
||||
const r = 10**dp;
|
||||
|
||||
const r = 10 ** dp;
|
||||
|
||||
do {
|
||||
bytes /= thresh;
|
||||
++u;
|
||||
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
|
||||
|
||||
|
||||
return bytes.toFixed(dp) + ' ' + units[u];
|
||||
|
||||
return bytes.toFixed(dp) + " " + units[u];
|
||||
}
|
||||
|
||||
exports.humanFileSize = humanFileSize
|
||||
exports.humanFileSize = humanFileSize;
|
||||
|
@ -1,20 +1,20 @@
|
||||
const { createClient } = require("webdav");
|
||||
const fs = require("fs");
|
||||
const moment = require('moment');
|
||||
const https = require('https')
|
||||
const moment = require("moment");
|
||||
const https = require("https");
|
||||
|
||||
const statusTools = require('./status');
|
||||
const statusTools = require("./status");
|
||||
const endpoint = "/remote.php/webdav";
|
||||
const configPath = "/data/webdav_conf.json";
|
||||
const path = require('path');
|
||||
const settingsTools = require('./settingsTools');
|
||||
const pathTools = require('./pathTools');
|
||||
const hassioApiTools = require('./hassioApiTools');
|
||||
const logger = require('../config/winston');
|
||||
const path = require("path");
|
||||
const settingsTools = require("./settingsTools");
|
||||
const pathTools = require("./pathTools");
|
||||
const hassioApiTools = require("./hassioApiTools");
|
||||
const logger = require("../config/winston");
|
||||
|
||||
const got = require ('got');
|
||||
const stream = require('stream');
|
||||
const {promisify} = require('util');
|
||||
const got = require("got");
|
||||
const stream = require("stream");
|
||||
const { promisify } = require("util");
|
||||
const pipeline = promisify(stream.pipeline);
|
||||
|
||||
class WebdavTools {
|
||||
@ -24,7 +24,7 @@ class WebdavTools {
|
||||
this.username = null;
|
||||
this.password = null;
|
||||
}
|
||||
|
||||
|
||||
init(ssl, host, username, password, accept_selfsigned_cert) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let status = statusTools.getStatus();
|
||||
@ -32,78 +32,85 @@ class WebdavTools {
|
||||
this.baseUrl = (ssl === "true" ? "https" : "http") + "://" + host + endpoint;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
let agent_option = ssl === "true" ? { rejectUnauthorized: accept_selfsigned_cert === "false" } : {}
|
||||
let agent_option = ssl === "true" ? { rejectUnauthorized: accept_selfsigned_cert === "false" } : {};
|
||||
try {
|
||||
this.client = createClient(this.baseUrl, { username: username, password: password, httpsAgent: new https.Agent(agent_option) }) ;
|
||||
|
||||
this.client.getDirectoryContents("/").then(() => {
|
||||
if (status.error_code == 3) {
|
||||
status.status = "idle";
|
||||
status.message = null;
|
||||
status.error_code = null;
|
||||
this.client = createClient(this.baseUrl, { username: username, password: password, httpsAgent: new https.Agent(agent_option) });
|
||||
|
||||
this.client
|
||||
.getDirectoryContents("/")
|
||||
.then(() => {
|
||||
if (status.error_code == 3) {
|
||||
status.status = "idle";
|
||||
status.message = null;
|
||||
status.error_code = null;
|
||||
statusTools.setStatus(status);
|
||||
}
|
||||
logger.debug("Nextcloud connection: \x1b[32mSuccess !\x1b[0m");
|
||||
this.initFolder().then(() => {
|
||||
resolve();
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
status.status = "error";
|
||||
status.error_code = 3;
|
||||
status.message = "Can't connect to Nextcloud (" + error + ") !";
|
||||
statusTools.setStatus(status);
|
||||
}
|
||||
logger.debug("Nextcloud connection: \x1b[32mSuccess !\x1b[0m");
|
||||
this.initFolder().then(() => {
|
||||
resolve();
|
||||
this.client = null;
|
||||
logger.error("Can't connect to Nextcloud (" + error + ") !");
|
||||
reject("Can't connect to Nextcloud (" + error + ") !");
|
||||
});
|
||||
|
||||
}).catch((error) => {
|
||||
status.status = "error";
|
||||
status.error_code = 3;
|
||||
status.message = "Can't connect to Nextcloud (" + error + ") !"
|
||||
statusTools.setStatus(status);
|
||||
this.client = null;
|
||||
logger.error("Can't connect to Nextcloud (" + error + ") !");
|
||||
reject("Can't connect to Nextcloud (" + error + ") !");
|
||||
});
|
||||
} catch (err) {
|
||||
status.status = "error";
|
||||
status.error_code = 3;
|
||||
status.message = "Can't connect to Nextcloud (" + err + ") !"
|
||||
status.message = "Can't connect to Nextcloud (" + err + ") !";
|
||||
statusTools.setStatus(status);
|
||||
this.client = null;
|
||||
logger.error("Can't connect to Nextcloud (" + err + ") !");
|
||||
reject("Can't connect to Nextcloud (" + err + ") !");
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
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 + '/'
|
||||
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.`)
|
||||
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)
|
||||
if (error.response.status == 405) logger.debug(`Path ${path} already exist.`);
|
||||
else logger.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
initFolder() {
|
||||
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();
|
||||
})
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
/**
|
||||
* Check if theh webdav config is valid, if yes, start init of webdav client
|
||||
*/
|
||||
* Check if theh webdav config is valid, if yes, start init of webdav client
|
||||
*/
|
||||
confIsValid() {
|
||||
return new Promise((resolve, reject) => {
|
||||
let status = statusTools.getStatus();
|
||||
@ -117,309 +124,294 @@ class WebdavTools {
|
||||
statusTools.setStatus(status);
|
||||
}
|
||||
// Check if self_signed option exist
|
||||
if( conf.self_signed == null || conf.self_signed == ''){
|
||||
if (conf.self_signed == null || conf.self_signed == "") {
|
||||
conf.self_signed = "false";
|
||||
this.setConf(conf);
|
||||
}
|
||||
this.init(conf.ssl, conf.host, conf.username, conf.password, conf.self_signed).then(() => {
|
||||
resolve();
|
||||
}).catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
|
||||
}
|
||||
else {
|
||||
this.init(conf.ssl, conf.host, conf.username, conf.password, conf.self_signed)
|
||||
.then(() => {
|
||||
resolve();
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
} else {
|
||||
status.status = "error";
|
||||
status.error_code = 2;
|
||||
status.message = "Nextcloud config invalid !"
|
||||
status.message = "Nextcloud config invalid !";
|
||||
statusTools.setStatus(status);
|
||||
logger.error(status.message);
|
||||
reject("Nextcloud config invalid !");
|
||||
}
|
||||
|
||||
if(conf.back_dir == null || conf.back_dir == ''){
|
||||
logger.info('Backup dir is null, initializing it.');
|
||||
|
||||
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}`;
|
||||
} 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}/`;
|
||||
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.error_code = 2;
|
||||
status.message = "Nextcloud config not found !"
|
||||
status.message = "Nextcloud config not found !";
|
||||
statusTools.setStatus(status);
|
||||
logger.error(status.message);
|
||||
reject("Nextcloud config not found !");
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
getConf() {
|
||||
if (fs.existsSync(configPath)) {
|
||||
let content = JSON.parse(fs.readFileSync(configPath));
|
||||
return content;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
} else return null;
|
||||
}
|
||||
|
||||
|
||||
setConf(conf) {
|
||||
fs.writeFileSync(configPath, JSON.stringify(conf));
|
||||
}
|
||||
|
||||
|
||||
|
||||
uploadFile(id, path) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.client == null) {
|
||||
this.confIsValid().then(() => {
|
||||
this._startUpload(id, path);
|
||||
}).catch((err) => {
|
||||
reject(err);
|
||||
})
|
||||
}
|
||||
else
|
||||
this._startUpload(id, path);
|
||||
this.confIsValid()
|
||||
.then(() => {
|
||||
this._startUpload(id, path);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
} else this._startUpload(id, path);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
_startUpload(id, path) {
|
||||
return new Promise( (resolve, reject) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let status = statusTools.getStatus();
|
||||
status.status = "upload";
|
||||
status.progress = 0;
|
||||
status.message = null;
|
||||
status.error_code = null;
|
||||
statusTools.setStatus(status);
|
||||
logger.info('Uploading snap...');
|
||||
let tmpFile = `./temp/${id}.tar`
|
||||
let stream = fs.createReadStream(tmpFile);
|
||||
let conf = this.getConf()
|
||||
logger.info("Uploading snap...");
|
||||
let tmpFile = `./temp/${id}.tar`;
|
||||
let stream = fs.createReadStream(tmpFile);
|
||||
let conf = this.getConf();
|
||||
let options = {
|
||||
body: stream,
|
||||
username: this.username,
|
||||
password: this.password,
|
||||
};
|
||||
if (conf.ssl === "true") {
|
||||
options["https"] = { rejectUnauthorized: conf.self_signed === "false" };
|
||||
}
|
||||
if(conf.ssl === 'true'){
|
||||
options["https"] = { rejectUnauthorized: conf.self_signed === "false" }
|
||||
}
|
||||
|
||||
got.stream.put(this.baseUrl + encodeURI(path), options).on('uploadProgress', e => {
|
||||
let percent = e.percent;
|
||||
if (status.progress != percent) {
|
||||
status.progress = percent;
|
||||
statusTools.setStatus(status);
|
||||
}
|
||||
if (percent >= 1) {
|
||||
logger.info('Upload done...');
|
||||
}
|
||||
}).on('response', res => {
|
||||
|
||||
if (res.statusCode != 201 && res.statusCode != 204) {
|
||||
|
||||
got.stream
|
||||
.put(this.baseUrl + encodeURI(path), options)
|
||||
.on("uploadProgress", (e) => {
|
||||
let percent = e.percent;
|
||||
if (status.progress != percent) {
|
||||
status.progress = percent;
|
||||
statusTools.setStatus(status);
|
||||
}
|
||||
if (percent >= 1) {
|
||||
logger.info("Upload done...");
|
||||
}
|
||||
})
|
||||
.on("response", (res) => {
|
||||
if (res.statusCode != 201 && res.statusCode != 204) {
|
||||
status.status = "error";
|
||||
status.error_code = 4;
|
||||
status.message = `Fail to upload snapshot to nextcloud (Status code: ${res.statusCode})!`;
|
||||
statusTools.setStatus(status);
|
||||
logger.error(status.message);
|
||||
fs.unlinkSync(tmpFile);
|
||||
reject(status.message);
|
||||
} else {
|
||||
logger.info(`...Upload finish ! (status: ${res.statusCode})`);
|
||||
status.status = "idle";
|
||||
status.progress = -1;
|
||||
status.message = null;
|
||||
status.error_code = null;
|
||||
status.last_backup = moment().format("MMM D, YYYY HH:mm");
|
||||
statusTools.setStatus(status);
|
||||
cleanTempFolder();
|
||||
let autoCleanCloud = settingsTools.getSettings().auto_clean_backup;
|
||||
if (autoCleanCloud != null && autoCleanCloud == "true") {
|
||||
this.clean().catch();
|
||||
}
|
||||
let autoCleanlocal = settingsTools.getSettings().auto_clean_local;
|
||||
if (autoCleanlocal != null && autoCleanlocal == "true") {
|
||||
hassioApiTools.clean();
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
})
|
||||
.on("error", (err) => {
|
||||
fs.unlinkSync(tmpFile);
|
||||
status.status = "error";
|
||||
status.error_code = 4;
|
||||
status.message = `Fail to upload snapshot to nextcloud (Status code: ${res.statusCode})!`;
|
||||
status.message = `Fail to upload snapshot to nextcloud (${err}) !`;
|
||||
statusTools.setStatus(status);
|
||||
logger.error(status.message);
|
||||
fs.unlinkSync(tmpFile);
|
||||
reject(status.message);
|
||||
} else {
|
||||
logger.info(`...Upload finish ! (status: ${res.statusCode})`);
|
||||
status.status = "idle";
|
||||
status.progress = -1;
|
||||
status.message = null;
|
||||
status.error_code = null;
|
||||
status.last_backup = moment().format('MMM D, YYYY HH:mm');
|
||||
statusTools.setStatus(status);
|
||||
cleanTempFolder();
|
||||
let autoCleanCloud = settingsTools.getSettings().auto_clean_backup;
|
||||
if (autoCleanCloud != null && autoCleanCloud == "true") {
|
||||
this.clean().catch();
|
||||
}
|
||||
let autoCleanlocal = settingsTools.getSettings().auto_clean_local;
|
||||
if (autoCleanlocal != null && autoCleanlocal == "true") {
|
||||
hassioApiTools.clean();
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
}).on('error', err => {
|
||||
|
||||
fs.unlinkSync(tmpFile);
|
||||
status.status = "error";
|
||||
status.error_code = 4;
|
||||
status.message = `Fail to upload snapshot to nextcloud (${err}) !`;
|
||||
statusTools.setStatus(status);
|
||||
logger.error(status.message);
|
||||
reject(status.message);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
downloadFile(path) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.client == null) {
|
||||
this.confIsValid().then(() => {
|
||||
this._startDownload(path)
|
||||
.then((path)=> resolve(path))
|
||||
.catch(()=> reject());
|
||||
}).catch((err) => {
|
||||
reject(err);
|
||||
})
|
||||
}
|
||||
else
|
||||
this._startDownload(path)
|
||||
.then((path)=> resolve(path))
|
||||
.catch(()=> reject());
|
||||
this.confIsValid()
|
||||
.then(() => {
|
||||
this._startDownload(path)
|
||||
.then((path) => resolve(path))
|
||||
.catch(() => reject());
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
} else
|
||||
this._startDownload(path)
|
||||
.then((path) => resolve(path))
|
||||
.catch(() => reject());
|
||||
});
|
||||
}
|
||||
_startDownload(path) {
|
||||
return new Promise( (resolve, reject) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let status = statusTools.getStatus();
|
||||
status.status = "download-b";
|
||||
status.progress = 0;
|
||||
status.message = null;
|
||||
status.error_code = null;
|
||||
statusTools.setStatus(status);
|
||||
|
||||
logger.info('Downloading backup...');
|
||||
|
||||
let tmpFile = `./temp/restore_${moment().format('MMM-DD-YYYY_HH_mm')}.tar`
|
||||
let stream = fs.createWriteStream(tmpFile);
|
||||
let conf = this.getConf()
|
||||
|
||||
logger.info("Downloading backup...");
|
||||
|
||||
let tmpFile = `./temp/restore_${moment().format("MMM-DD-YYYY_HH_mm")}.tar`;
|
||||
let stream = fs.createWriteStream(tmpFile);
|
||||
let conf = this.getConf();
|
||||
let options = {
|
||||
username: this.username,
|
||||
password: this.password,
|
||||
};
|
||||
if (conf.ssl === "true") {
|
||||
options["https"] = { rejectUnauthorized: conf.self_signed === "false" };
|
||||
}
|
||||
if(conf.ssl === 'true'){
|
||||
options["https"] = { rejectUnauthorized: conf.self_signed === "false" }
|
||||
}
|
||||
|
||||
|
||||
pipeline(
|
||||
got.stream.get(this.baseUrl + encodeURI(path), options)
|
||||
.on('downloadProgress', e => {
|
||||
got.stream.get(this.baseUrl + encodeURI(path), options).on("downloadProgress", (e) => {
|
||||
let percent = Math.round(e.percent * 100) / 100;
|
||||
if (status.progress != percent) {
|
||||
status.progress = percent;
|
||||
statusTools.setStatus(status);
|
||||
}
|
||||
})
|
||||
,
|
||||
}),
|
||||
stream
|
||||
)
|
||||
.then((res)=>{
|
||||
logger.info('Download success !')
|
||||
)
|
||||
.then((res) => {
|
||||
logger.info("Download success !");
|
||||
status.progress = 1;
|
||||
statusTools.setStatus(status);
|
||||
logger.debug("Backup dl size : " + (fs.statSync(tmpFile).size / 1024 / 1024));
|
||||
logger.debug("Backup dl size : " + fs.statSync(tmpFile).size / 1024 / 1024);
|
||||
resolve(tmpFile);
|
||||
}).catch((err)=>{
|
||||
if(fs.existsSync(tmpFile))
|
||||
fs.unlinkSync(tmpFile);
|
||||
})
|
||||
.catch((err) => {
|
||||
if (fs.existsSync(tmpFile)) fs.unlinkSync(tmpFile);
|
||||
status.status = "error";
|
||||
status.message = "Fail to download Hassio snapshot (" + err.message + ")";
|
||||
status.error_code = 7;
|
||||
statusTools.setStatus(status);
|
||||
logger.error(status.message);
|
||||
reject(err.message);
|
||||
})
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
getFolderContent(path) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(this.client == null){
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
this.client.getDirectoryContents(path)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getFolderContent(path) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.client == null) {
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
this.client
|
||||
.getDirectoryContents(path)
|
||||
.then((contents) => {
|
||||
resolve(contents);
|
||||
}).catch((error) => {
|
||||
reject(error);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
clean() {
|
||||
let limit = settingsTools.getSettings().auto_clean_backup_keep;
|
||||
if (limit == null)
|
||||
limit = 5;
|
||||
return new Promise((resolve, reject) => {
|
||||
this.getFolderContent(this.getConf().back_dir + pathTools.auto).then(async (contents) => {
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
clean() {
|
||||
let limit = settingsTools.getSettings().auto_clean_backup_keep;
|
||||
if (limit == null) limit = 5;
|
||||
return new Promise((resolve, reject) => {
|
||||
this.getFolderContent(this.getConf().back_dir + pathTools.auto)
|
||||
.then(async (contents) => {
|
||||
if (contents.length < limit) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
contents.sort((a, b) => {
|
||||
if (moment(a.lastmod).isBefore(moment(b.lastmod)))
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
if (moment(a.lastmod).isBefore(moment(b.lastmod))) return 1;
|
||||
else return -1;
|
||||
});
|
||||
|
||||
|
||||
let toDel = contents.slice(limit);
|
||||
for (let i in toDel) {
|
||||
await this.client.deleteFile(toDel[i].filename);
|
||||
}
|
||||
logger.info('Cloud clean done.')
|
||||
logger.info("Cloud clean done.");
|
||||
resolve();
|
||||
|
||||
}).catch((error) => {
|
||||
})
|
||||
.catch((error) => {
|
||||
status.status = "error";
|
||||
status.error_code = 6;
|
||||
status.message = "Fail to clean Nexcloud (" + error + ") !"
|
||||
status.message = "Fail to clean Nexcloud (" + error + ") !";
|
||||
statusTools.setStatus(status);
|
||||
logger.error(status.message);
|
||||
reject(status.message);
|
||||
});
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
function cleanTempFolder() {
|
||||
fs.readdir("./temp/", (err, files) => {
|
||||
if (err) throw err;
|
||||
|
||||
for (const file of files) {
|
||||
fs.unlink(path.join("./temp/", file), err => {
|
||||
if (err) throw err;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
class Singleton {
|
||||
constructor() {
|
||||
if (!Singleton.instance) {
|
||||
Singleton.instance = new WebdavTools();
|
||||
}
|
||||
}
|
||||
|
||||
function cleanTempFolder() {
|
||||
fs.readdir("./temp/", (err, files) => {
|
||||
if (err) throw err;
|
||||
|
||||
for (const file of files) {
|
||||
fs.unlink(path.join("./temp/", file), (err) => {
|
||||
if (err) throw err;
|
||||
});
|
||||
}
|
||||
|
||||
getInstance() {
|
||||
return Singleton.instance;
|
||||
});
|
||||
}
|
||||
|
||||
class Singleton {
|
||||
constructor() {
|
||||
if (!Singleton.instance) {
|
||||
Singleton.instance = new WebdavTools();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Singleton;
|
||||
|
||||
|
||||
|
||||
getInstance() {
|
||||
return Singleton.instance;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Singleton;
|
||||
|
Loading…
Reference in New Issue
Block a user