Compare commits

...

9 Commits

Author SHA1 Message Date
dependabot[bot]
f7d4992bf1
⬆️ Bump url-parse (#116)
Bumps [url-parse](https://github.com/unshiftio/url-parse) from 1.5.3 to 1.5.10.
- [Release notes](https://github.com/unshiftio/url-parse/releases)
- [Commits](https://github.com/unshiftio/url-parse/compare/1.5.3...1.5.10)

---
updated-dependencies:
- dependency-name: url-parse
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-04-30 16:51:45 +02:00
dependabot[bot]
f93c75fb25
⬆️ Bump ejs in /nextcloud_backup/rootfs/opt/nextcloud_backup (#131)
Bumps [ejs](https://github.com/mde/ejs) from 3.1.6 to 3.1.7.
- [Release notes](https://github.com/mde/ejs/releases)
- [Changelog](https://github.com/mde/ejs/blob/main/CHANGELOG.md)
- [Commits](https://github.com/mde/ejs/compare/v3.1.6...v3.1.7)

---
updated-dependencies:
- dependency-name: ejs
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-04-30 16:48:30 +02:00
dependabot[bot]
3d3e52666f
⬆️ Bump debug in /nextcloud_backup/rootfs/opt/nextcloud_backup (#119)
Bumps [debug](https://github.com/debug-js/debug) from 4.3.3 to 4.3.4.
- [Release notes](https://github.com/debug-js/debug/releases)
- [Commits](https://github.com/debug-js/debug/compare/4.3.3...4.3.4)

---
updated-dependencies:
- dependency-name: debug
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-04-30 16:46:55 +02:00
dependabot[bot]
f4d5244ae9
⬆️ Bump @fortawesome/fontawesome-free (#120)
Bumps [@fortawesome/fontawesome-free](https://github.com/FortAwesome/Font-Awesome) from 6.0.0 to 6.1.1.
- [Release notes](https://github.com/FortAwesome/Font-Awesome/releases)
- [Changelog](https://github.com/FortAwesome/Font-Awesome/blob/6.x/CHANGELOG.md)
- [Commits](https://github.com/FortAwesome/Font-Awesome/compare/6.0.0...6.1.1)

---
updated-dependencies:
- dependency-name: "@fortawesome/fontawesome-free"
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-04-30 16:46:46 +02:00
dependabot[bot]
5321673c2a
⬆️ Bump moment from 2.29.1 to 2.29.3 in /nextcloud_backup/rootfs/opt/nextcloud_backup (#126)
* :abulance: Fix registry login

* ⬆️ Bump moment in /nextcloud_backup/rootfs/opt/nextcloud_backup

Bumps [moment](https://github.com/moment/moment) from 2.29.1 to 2.29.3.
- [Release notes](https://github.com/moment/moment/releases)
- [Changelog](https://github.com/moment/moment/blob/2.29.3/CHANGELOG.md)
- [Commits](https://github.com/moment/moment/compare/2.29.1...2.29.3)

---
updated-dependencies:
- dependency-name: moment
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: Sébastien Clément <seb6596@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-04-30 16:46:36 +02:00
dependabot[bot]
ff59d74678
⬆️ Bump got from 11.8.2 to 12.0.4 in /nextcloud_backup/rootfs/opt/nextcloud_backup (#129)
* :abulance: Fix registry login

* ⬆️ Bump got in /nextcloud_backup/rootfs/opt/nextcloud_backup

Bumps [got](https://github.com/sindresorhus/got) from 11.8.2 to 12.0.4.
- [Release notes](https://github.com/sindresorhus/got/releases)
- [Commits](https://github.com/sindresorhus/got/compare/v11.8.2...v12.0.4)

---
updated-dependencies:
- dependency-name: got
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: Sébastien Clément <seb6596@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-04-30 16:46:25 +02:00
dependabot[bot]
72443bb889
⬆️ Bump express from 4.17.2 to 4.18.1 in /nextcloud_backup/rootfs/opt/nextcloud_backup (#132)
* :abulance: Fix registry login

* ⬆️ Bump express in /nextcloud_backup/rootfs/opt/nextcloud_backup

Bumps [express](https://github.com/expressjs/express) from 4.17.2 to 4.18.1.
- [Release notes](https://github.com/expressjs/express/releases)
- [Changelog](https://github.com/expressjs/express/blob/master/History.md)
- [Commits](https://github.com/expressjs/express/compare/4.17.2...4.18.1)

---
updated-dependencies:
- dependency-name: express
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: Sébastien Clément <seb6596@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-04-30 16:46:01 +02:00
0d2698bcc6
🚑 Fix registry login 2022-04-30 16:37:47 +02:00
ec957041e1
🔨 Migrate all code to ESM (#124)
* 🔨 Convert everything to es6 modules

* 🔨 Cath in manual backup

* 🔨 Change status storage method

* 🔨 Move token to static in hassioApiTools

* 🚑 Fix crash on backup upload to ha

* 🚑 Fix multiple upload

* 🔨 Add logs for protected

* 🔨 Catch some errors
2022-04-30 16:19:31 +02:00
16 changed files with 864 additions and 748 deletions

View File

@ -76,7 +76,7 @@ jobs:
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push ${{matrix.arch}}

View File

@ -1,11 +1,26 @@
const createError = require("http-errors");
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
import createError from "http-errors";
import express from "express";
import path from "path";
import cookieParser from "cookie-parser";
import logger from "morgan";
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import fs from "fs"
import newlog from "./config/winston.js"
import * as statusTools from "./tools/status.js"
import * as hassioApiTools from "./tools/hassioApiTools.js"
import webdav from "./tools/webdavTools.js"
import * as settingsTools from "./tools/settingsTools.js"
import cronTools from "./tools/cronTools.js"
const indexRouter = require("./routes/index");
const apiRouter = require("./routes/api");
import indexRouter from "./routes/index.js"
import apiRouter from "./routes/api.js"
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
// view engine setup
@ -71,17 +86,15 @@ app.use(function (err, req, res, next) {
----------------------------------------------------------
*/
const fs = require("fs");
const newlog = require("./config/winston");
newlog.info(`Log level: ${ process.env.LOG_LEVEL }`);
newlog.info(`Backup timeout: ${ parseInt(process.env.CREATE_BACKUP_TIMEOUT) || ( 90 * 60 * 1000 ) }`)
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");
hassioApiTools.getSnapshots().then(
() => {
newlog.info("Hassio API : \x1b[32mGo !\x1b[0m");
@ -92,8 +105,6 @@ hassioApiTools.getSnapshots().then(
}
);
const WebdavTools = require("./tools/webdavTools");
const webdav = new WebdavTools().getInstance();
webdav.confIsValid().then(
() => {
newlog.info("Nextcloud connection : \x1b[32mGo !\x1b[0m");
@ -103,11 +114,9 @@ webdav.confIsValid().then(
newlog.error("... " + err);
}
);
const settingTool = require('./tools/settingsTools')
settingTool.check(settingTool.getSettings(), true);
const cronTools = require("./tools/cronTools");
cronTools.startCron();
settingsTools.check(settingsTools.getSettings(), true);
cronTools.init();
module.exports = app;
export default app;

View File

@ -4,9 +4,11 @@
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('nexcloud-backup:server');
var http = require('http');
import app from "../app.js"
import http from "http"
import _debug from 'debug';
const debug = _debug('nexcloud-backup:server');
/**
* Get port from environment and store in Express.

View File

@ -1,4 +1,4 @@
const winston = require("winston");
import winston from "winston";
const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
@ -24,4 +24,5 @@ const logger = winston.createLogger({
],
});
module.exports = logger;
export default logger;

File diff suppressed because it is too large Load Diff

View File

@ -2,23 +2,24 @@
"name": "nexcloud-backup",
"version": "0.8.0",
"private": true,
"type": "module",
"scripts": {
"start": "node ./bin/www "
"start": "node ./bin/www.js "
},
"dependencies": {
"@fortawesome/fontawesome-free": "^6.0.0",
"@fortawesome/fontawesome-free": "^6.1.1",
"app-root-path": "^3.0.0",
"bootstrap": "^5.0.1",
"cookie-parser": "^1.4.6",
"cron": "^1.8.2",
"debug": "~4.3.3",
"ejs": "~3.1.6",
"express": "~4.17.2",
"debug": "~4.3.4",
"ejs": "~3.1.7",
"express": "~4.18.1",
"form-data": "^4.0.0",
"got": "^11.8.2",
"got": "12.0.4",
"http-errors": "~2.0.0",
"jquery": "^3.6.0",
"moment": "^2.29.1",
"moment": "^2.29.3",
"morgan": "~1.10.0",
"webdav": "^4.8.0",
"winston": "^3.6.0"

View File

@ -29,13 +29,15 @@ function updateDynamicListeners() {
console.log(id);
});
let manual_back_list = $(".manual-back-list");
manual_back_list.off();
manual_back_list.unbind();
manual_back_list.on("click", function () {
let id = this.getAttribute("data-id");
let name = this.getAttribute("data-name");
manualBackup(id, name);
});
$(".restore").click(function () {
let restore_btn = $(".restore");
restore_btn.unbind();
restore_btn.click(function () {
let to_restore = this.getAttribute("data-id");
console.log(to_restore);
restore(to_restore);
@ -243,7 +245,6 @@ function sendNextcloudSettings() {
console.log("Saved");
$("#nextcloud_settings_message").parent().addClass("d-none");
create_toast("success", "Nextcloud settings saved !", default_toast_timeout);
0;
})
.fail((data) => {
let nextcloud_settings_message = $("#nextcloud_settings_message");

View File

@ -1,20 +1,18 @@
const express = require("express");
const router = express.Router();
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;
import express from 'express';
import moment from "moment";
import * as statusTools from "../tools/status.js"
import webdav from "../tools/webdavTools.js"
import * as settingsTools from "../tools/settingsTools.js"
import * as pathTools from "../tools/pathTools.js"
import * as hassioApiTools from "../tools/hassioApiTools.js"
import { humanFileSize } from "../tools/toolbox.js";
import cronTools from "../tools/cronTools.js"
import logger from "../config/winston.js"
const cronTools = require("../tools/cronTools");
const logger = require("../config/winston");
var router = express.Router();
router.get("/status", (req, res, next) => {
cronTools.updatetNextDate();
cronTools.updateNextDate();
let status = statusTools.getStatus();
res.json(status);
});
@ -130,9 +128,15 @@ router.post("/manual-backup", function (req, res, next) {
hassioApiTools
.downloadSnapshot(id)
.then(() => {
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + ".tar").catch();
res.status(201);
res.send();
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + ".tar").then(()=>{
res.status(201);
res.send();
}).catch(()=>{
res.status(500);
res.send();
}
);
})
.catch(() => {
res.status(500);
@ -161,10 +165,10 @@ router.post("/new-backup", function (req, res, next) {
.then(() => {
hassioApiTools.startAddons().catch(() => {
})
});
});
});
});
}).catch(()=>{});
}).catch(()=>{});
}).catch(()=>{});
}).catch(()=>{});
})
.catch(() => {
hassioApiTools.startAddons().catch(() => {
@ -191,7 +195,7 @@ router.post("/backup-settings", function (req, res, next) {
let [result, message] = settingsTools.check(req.body)
if (result) {
settingsTools.setSettings(req.body);
cronTools.startCron();
cronTools.init();
res.send();
} else {
res.status(400);
@ -225,5 +229,5 @@ router.post("/restore", function (req, res, next) {
}
});
module.exports = router;
export default router;

View File

@ -1,9 +1,9 @@
const express = require("express");
const router = express.Router();
import express from 'express';
var router = express.Router();
/* GET home page. */
router.get("/", function (req, res, next) {
res.render("index");
});
module.exports = router;
export default router;

View File

@ -1,23 +1,13 @@
const CronJob = require("cron").CronJob;
import { CronJob } from "cron"
import * as settingsTools from "./settingsTools.js"
import * as hassioApiTools from "./hassioApiTools.js"
import * as statusTools from "./status.js"
import * as pathTools from "./pathTools.js"
import webdav from "./webdavTools.js"
const settingsTools = require("./settingsTools");
const WebdavTools = require("./webdavTools");
const webdav = new WebdavTools().getInstance();
const hassioApiTools = require("./hassioApiTools");
const statusTools = require("./status");
const pathTools = require("./pathTools");
const logger = require("../config/winston");
import logger from "../config/winston.js"
function startCron() {
let cronContainer = new Singleton().getInstance();
cronContainer.init();
}
function updatetNextDate() {
let cronContainer = new Singleton().getInstance();
cronContainer.updateNextDate();
}
class CronContainer {
constructor() {
@ -126,17 +116,6 @@ class CronContainer {
}
}
class Singleton {
constructor() {
if (!Singleton.instance) {
Singleton.instance = new CronContainer();
}
}
const INSTANCE = new CronContainer();
export default INSTANCE;
getInstance() {
return Singleton.instance;
}
}
exports.startCron = startCron;
exports.updatetNextDate = updatetNextDate;

View File

@ -1,23 +1,24 @@
const fs = require("fs");
import fs from "fs"
import moment from "moment";
import stream from "stream"
import { promisify } from "util";
import got from "got";
import FormData from "form-data";
import * as statusTools from "../tools/status.js"
import * as settingsTools from "../tools/settingsTools.js"
const moment = require("moment");
const stream = require("stream");
const { promisify } = require("util");
import logger from "../config/winston.js"
const pipeline = promisify(stream.pipeline);
const got = require("got");
const FormData = require("form-data");
const statusTools = require("./status");
const settingsTools = require("./settingsTools");
const logger = require("../config/winston");
// Default timout to 90min
const create_snap_timeout = parseInt(process.env.CREATE_BACKUP_TIMEOUT) || ( 90 * 60 * 1000 );
const token = process.env.SUPERVISOR_TOKEN;
// Default timeout to 90min
const create_snap_timeout = parseInt(process.env.CREATE_BACKUP_TIMEOUT) || (90 * 60 * 1000);
function getVersion() {
return new Promise((resolve, reject) => {
let token = process.env.HASSIO_TOKEN;
let status = statusTools.getStatus();
let option = {
headers: { "Authorization": `Bearer ${token}` },
@ -36,19 +37,14 @@ function getVersion() {
resolve(version);
})
.catch((error) => {
status.status = "error";
status.message = "Fail to fetch HA Version (" + error.message + ")";
status.error_code = 1;
statusTools.setStatus(status);
logger.error(status.message);
reject(error.message);
statusTools.setError(`Fail to fetch HA Version (${error.message})`, 1);
reject(`Fail to fetch HA Version (${error.message})`);
});
});
}
function getAddonList() {
return new Promise((resolve, reject) => {
let token = process.env.HASSIO_TOKEN;
let status = statusTools.getStatus();
let option = {
headers: { "Authorization": `Bearer ${token}` },
@ -78,12 +74,8 @@ function getAddonList() {
resolve(installed);
})
.catch((error) => {
status.status = "error";
status.message = "Fail to fetch addons list (" + error.message + ")";
status.error_code = 1;
statusTools.setStatus(status);
logger.error(status.message);
reject(error.message);
statusTools.setError(`Fail to fetch addons list (${error.message})`, 1);
reject(`Fail to fetch addons list (${error.message})`);
});
});
}
@ -146,7 +138,6 @@ function getFolderToBackup() {
function getSnapshots() {
return new Promise((resolve, reject) => {
let token = process.env.HASSIO_TOKEN;
let status = statusTools.getStatus();
let option = {
headers: { "Authorization": `Bearer ${token}` },
@ -165,12 +156,8 @@ function getSnapshots() {
resolve(snaps);
})
.catch((error) => {
status.status = "error";
status.message = "Fail to fetch Hassio snapshots (" + error.message + ")";
status.error_code = 1;
statusTools.setStatus(status);
logger.error(status.message);
reject(error.message);
statusTools.setError(`Fail to fetch Hassio backups (${error.message})`, 1);
reject(`Fail to fetch Hassio backups (${error.message})`);
});
});
}
@ -181,7 +168,6 @@ function downloadSnapshot(id) {
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(() => {
@ -203,29 +189,21 @@ function downloadSnapshot(id) {
}),
stream
)
.then((res) => {
.then(() => {
logger.info("Download success !");
status.progress = 1;
statusTools.setStatus(status);
logger.debug("Snapshot dl size : " + fs.statSync(tmp_file).size / 1024 / 1024);
resolve();
})
.catch((err) => {
.catch((error) => {
fs.unlinkSync(tmp_file);
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);
statusTools.setError(`Fail to download Hassio backup (${error.message})`, 7);
reject(`Fail to download Hassio backup (${error.message})`);
});
})
.catch((err) => {
status.status = "error";
status.message = "Fail to download Hassio snapshot. Not found ?";
status.error_code = 7;
statusTools.setStatus(status);
logger.error(status.message);
.catch(() => {
statusTools.setError("Fail to download Hassio backup. Not found ?", 7);
reject();
});
});
@ -235,8 +213,6 @@ function dellSnap(id) {
return new Promise((resolve, reject) => {
checkSnap(id)
.then(() => {
let token = process.env.HASSIO_TOKEN;
let option = {
headers: { "Authorization": `Bearer ${token}` },
responseType: "json",
@ -257,7 +233,6 @@ function dellSnap(id) {
function checkSnap(id) {
return new Promise((resolve, reject) => {
let token = process.env.HASSIO_TOKEN;
let option = {
headers: { "Authorization": `Bearer ${token}` },
responseType: "json",
@ -279,20 +254,23 @@ function createNewBackup(name) {
status.progress = -1;
statusTools.setStatus(status);
logger.info("Creating new snapshot...");
let token = process.env.HASSIO_TOKEN;
getAddonToBackup().then((addons) => {
let folders = getFolderToBackup();
let option = {
headers: { "Authorization": `Bearer ${token}` },
responseType: "json",
timeout: create_snap_timeout,
timeout: {
response: create_snap_timeout
},
json: {
name: name,
addons: addons,
folders: folders
},
};
if (settingsTools.getSettings().password_protected === "true") {
let password_protected = settingsTools.getSettings().password_protected;
logger.debug(`Is password protected ? ${password_protected}`)
if ( password_protected === "true") {
option.json.password = settingsTools.getSettings().password_protect_value
}
@ -302,12 +280,8 @@ function createNewBackup(name) {
resolve(result.body.data.slug);
})
.catch((error) => {
status.status = "error";
status.message = "Can't create new snapshot (" + error.message + ")";
status.error_code = 5;
statusTools.setStatus(status);
logger.error(status.message);
reject(status.message);
statusTools.setError(`Can't create new snapshot (${error.message})`, 5);
reject(`Can't create new snapshot (${error.message})`);
});
}).catch(reject);
@ -337,8 +311,9 @@ function clean() {
logger.info("Local clean done.");
resolve();
})
.catch(() => {
reject();
.catch((e) => {
statusTools.setError(`Fail to clean backups (${e}) !`, 6);
reject(`Fail to clean backups (${e}) !`);
});
});
}
@ -353,15 +328,11 @@ function uploadSnapshot(path) {
statusTools.setStatus(status);
logger.info("Uploading backup...");
let stream = fs.createReadStream(path);
let token = process.env.HASSIO_TOKEN;
let form = new FormData();
form.append("file", stream);
let options = {
body: form,
username: this.username,
password: this.password,
headers: { "Authorization": `Bearer ${token}` },
};
@ -399,19 +370,15 @@ function uploadSnapshot(path) {
})
.on("error", (err) => {
fs.unlinkSync(path);
status.status = "error";
status.error_code = 4;
status.message = `Fail to upload backup to home assistant (${err}) !`;
statusTools.setStatus(status);
logger.error(status.message);
reject(status.message);
statusTools.setError(`Fail to upload backup to home assistant (${err}) !`, 4);
reject(`Fail to upload backup to home assistant (${err}) !`);
});
});
}
function stopAddons() {
return new Promise(((resolve, reject) => {
logger.info('Stopping addons...')
logger.info('Stopping addons...');
let status = statusTools.getStatus();
status.status = "stopping";
status.progress = -1;
@ -419,7 +386,6 @@ function stopAddons() {
status.error_code = null;
statusTools.setStatus(status);
let promises = [];
let token = process.env.HASSIO_TOKEN;
let option = {
headers: { "Authorization": `Bearer ${token}` },
responseType: "json",
@ -427,7 +393,7 @@ function stopAddons() {
let addons_slug = settingsTools.getSettings().auto_stop_addon
for (let addon of addons_slug) {
if (addon !== "") {
logger.debug(`... Stopping addon ${addon}`)
logger.debug(`... Stopping addon ${addon}`);
promises.push(got.post(`http://hassio/addons/${addon}/stop`, option));
}
@ -439,14 +405,11 @@ function stopAddons() {
error = val.reason;
if (error) {
status.status = "error";
status.error_code = 8;
status.message = `Fail to stop addons(${error}) !`;
statusTools.setStatus(status);
statusTools.setError(`Fail to stop addons(${error}) !`, 8);
logger.error(status.message);
reject(status.message);
} else {
logger.info('... Ok')
logger.info('... Ok');
resolve();
}
});
@ -463,7 +426,6 @@ function startAddons() {
status.error_code = null;
statusTools.setStatus(status);
let promises = [];
let token = process.env.HASSIO_TOKEN;
let option = {
headers: { "Authorization": `Bearer ${token}` },
responseType: "json",
@ -482,12 +444,7 @@ function startAddons() {
error = val.reason;
if (error) {
let status = statusTools.getStatus();
status.status = "error";
status.error_code = 9;
status.message = `Fail to start addons (${error}) !`;
statusTools.setStatus(status);
logger.error(status.message);
statusTools.setError(`Fail to start addons (${error}) !`, 9)
reject(status.message);
} else {
logger.info('... Ok')
@ -502,7 +459,7 @@ function startAddons() {
}));
}
function publish_state(state){
function publish_state(state) {
// let data_error_sensor = {
// state: state.status == "error" ? "on" : "off",
@ -516,7 +473,6 @@ function publish_state(state){
// }
// let token = process.env.HASSIO_TOKEN;
// let option = {
// headers: { "Authorization": `Bearer ${token}` },
// responseType: "json",
@ -572,14 +528,16 @@ function publish_state(state){
// });
}
exports.getVersion = getVersion;
exports.getAddonList = getAddonList;
exports.getFolderList = getFolderList;
exports.getSnapshots = getSnapshots;
exports.downloadSnapshot = downloadSnapshot;
exports.createNewBackup = createNewBackup;
exports.uploadSnapshot = uploadSnapshot;
exports.stopAddons = stopAddons;
exports.startAddons = startAddons;
exports.clean = clean;
exports.publish_state = publish_state;
export {
getVersion,
getAddonList,
getFolderList,
getSnapshots,
downloadSnapshot,
createNewBackup,
uploadSnapshot,
stopAddons,
startAddons,
clean,
publish_state
}

View File

@ -1,4 +1,4 @@
let default_root = "/Hassio Backup/";
exports.default_root = default_root;
exports.manual = "Manual/";
exports.auto = "Auto/";
const default_root = "/Hassio Backup/";
export { default_root };
export const manual = "Manual/";
export const auto = "Auto/";

View File

@ -1,7 +1,8 @@
const fs = require("fs");
const logger = require("../config/winston");
const moment = require("moment");
const CronJob = require("cron").CronJob;
import fs from "fs"
import moment from "moment";
import { CronJob } from "cron";
import logger from "../config/winston.js"
const settingsPath = "/data/backup_conf.json";
@ -24,6 +25,7 @@ function check_cron(conf) {
if (conf.cron_base === "4") {
if (conf.cron_custom != null) {
try {
// TODO Need to be destroy
new CronJob(conf.cron_custom, () => {});
return true;
} catch(e) {
@ -187,8 +189,4 @@ function setSettings(settings) {
fs.writeFileSync(settingsPath, JSON.stringify(settings));
}
exports.getSettings = getSettings;
exports.setSettings = setSettings;
exports.check = check;
exports.check_cron = check_cron;
exports.getFormatedName = getFormatedName;
export { getSettings, setSettings, check, check_cron, getFormatedName };

View File

@ -1,49 +1,38 @@
const fs = require("fs");
const hassioApiTools = require("./hassioApiTools");
import * as hassioApiTools from "./hassioApiTools.js";
import logger from "../config/winston.js"
const statusPath = "/data/status.json";
let baseStatus = {
let status = {
status: "idle",
last_backup: null,
next_backup: null,
};
function init() {
if (!fs.existsSync(statusPath)) {
fs.writeFileSync(statusPath, JSON.stringify(baseStatus));
} else {
let content = getStatus();
if (content.status !== "idle") {
content.status = "idle";
content.message = null;
setStatus(content);
}
export function init() {
if (status.status !== "idle") {
status.status = "idle";
status.message = null;
}
}
function getStatus() {
if (!fs.existsSync(statusPath)) {
fs.writeFileSync(statusPath, JSON.stringify(baseStatus));
}
return JSON.parse(fs.readFileSync(statusPath).toString());
export function getStatus() {
return status;
}
function setStatus(state) {
if (fs.existsSync(statusPath)) {
let old_state_str = fs.readFileSync(statusPath).toString();
if(old_state_str !== JSON.stringify(state)){
fs.writeFileSync(statusPath, JSON.stringify(state));
hassioApiTools.publish_state(state);
}
}else{
fs.writeFileSync(statusPath, JSON.stringify(state));
hassioApiTools.publish_state(state);
export function setStatus(new_state) {
let old_state_str = JSON.stringify(status);
if(old_state_str !== JSON.stringify(new_state)){
status = new_state;
hassioApiTools.publish_state(status);
}
}
exports.init = init;
exports.getStatus = getStatus;
exports.setStatus = setStatus;
export function setError(message, error_code){
// Check if we don't have another error stored
if (status.status != "error") {
status.status = "error"
status.message = message
status.error_code = error_code;
}
logger.error(message);
}

View File

@ -18,4 +18,4 @@ function humanFileSize(bytes, si = false, dp = 1) {
return bytes.toFixed(dp) + " " + units[u];
}
exports.humanFileSize = humanFileSize;
export { humanFileSize } ;

View File

@ -1,20 +1,21 @@
const { createClient } = require("webdav");
const fs = require("fs");
const moment = require("moment");
const https = require("https");
import { createClient } from "webdav";
import fs from "fs"
import moment from "moment";
import https from "https"
import path from "path";
import got from "got";
import stream from "stream";
import { promisify } from "util";
import * as statusTools from "../tools/status.js"
import * as settingsTools from "../tools/settingsTools.js"
import * as pathTools from "../tools/pathTools.js"
import * as hassioApiTools from "../tools/hassioApiTools.js"
import logger from "../config/winston.js"
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 got = require("got");
const stream = require("stream");
const { promisify } = require("util");
const pipeline = promisify(stream.pipeline);
class WebdavTools {
@ -69,12 +70,7 @@ class WebdavTools {
});
}
__cant_connect_status(err){
let status = statusTools.getStatus();
status.status = "error";
status.error_code = 3;
status.message = "Can't connect to Nextcloud (" + err + ") !";
statusTools.setStatus(status);
logger.error("Can't connect to Nextcloud (" + err + ") !");
statusTools.setError(`Can't connect to Nextcloud (${err})`, 3);
}
async __createRoot() {
@ -427,16 +423,5 @@ function cleanTempFolder() {
});
}
class Singleton {
constructor() {
if (!Singleton.instance) {
Singleton.instance = new WebdavTools();
}
}
getInstance() {
return Singleton.instance;
}
}
module.exports = Singleton;
const INSTANCE = new WebdavTools();
export default INSTANCE;