Compare commits

..

9 Commits

Author SHA1 Message Date
dependabot[bot]
e1b27f8925
⬆️ Bump got in /nextcloud_backup/rootfs/opt/nextcloud_backup
Bumps [got](https://github.com/sindresorhus/got) from 11.8.2 to 12.0.1.
- [Release notes](https://github.com/sindresorhus/got/releases)
- [Commits](https://github.com/sindresorhus/got/compare/v11.8.2...v12.0.1)

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

Signed-off-by: dependabot[bot] <support@github.com>
2022-01-25 09:33:58 +00:00
dependabot[bot]
2c155b1060
⬆️ Bump winston in /nextcloud_backup/rootfs/opt/nextcloud_backup (#105)
Bumps [winston](https://github.com/winstonjs/winston) from 3.3.3 to 3.4.0.
- [Release notes](https://github.com/winstonjs/winston/releases)
- [Changelog](https://github.com/winstonjs/winston/blob/master/CHANGELOG.md)
- [Commits](https://github.com/winstonjs/winston/compare/v3.3.3...v3.4.0)

---
updated-dependencies:
- dependency-name: winston
  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-01-25 10:33:36 +01:00
dependabot[bot]
2f883e83ca
⬆️ Bump http-errors (#98)
Bumps [http-errors](https://github.com/jshttp/http-errors) from 1.8.0 to 2.0.0.
- [Release notes](https://github.com/jshttp/http-errors/releases)
- [Changelog](https://github.com/jshttp/http-errors/blob/master/HISTORY.md)
- [Commits](https://github.com/jshttp/http-errors/compare/1.8.0...v2.0.0)

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

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-01-25 10:32:48 +01:00
dependabot[bot]
ce23c6ac08
⬆️ Bump webdav in /nextcloud_backup/rootfs/opt/nextcloud_backup (#104)
Bumps [webdav](https://github.com/perry-mitchell/webdav-client) from 4.7.0 to 4.8.0.
- [Release notes](https://github.com/perry-mitchell/webdav-client/releases)
- [Changelog](https://github.com/perry-mitchell/webdav-client/blob/master/CHANGELOG.md)
- [Commits](https://github.com/perry-mitchell/webdav-client/compare/v4.7.0...v4.8.0)

---
updated-dependencies:
- dependency-name: webdav
  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-01-25 10:31:56 +01:00
dependabot[bot]
2850e2c054
⬆️ Bump express in /nextcloud_backup/rootfs/opt/nextcloud_backup (#97)
Bumps [express](https://github.com/expressjs/express) from 4.17.1 to 4.17.2.
- [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.1...4.17.2)

---
updated-dependencies:
- dependency-name: express
  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-01-25 10:29:49 +01:00
dependabot[bot]
c2b0236764
⬆️ Bump debug in /nextcloud_backup/rootfs/opt/nextcloud_backup (#95)
Bumps [debug](https://github.com/debug-js/debug) from 4.3.2 to 4.3.3.
- [Release notes](https://github.com/debug-js/debug/releases)
- [Commits](https://github.com/debug-js/debug/compare/4.3.2...4.3.3)

---
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-01-25 10:29:36 +01:00
dependabot[bot]
fdd4e5287e
⬆️ Bump cookie-parser (#93)
Bumps [cookie-parser](https://github.com/expressjs/cookie-parser) from 1.4.5 to 1.4.6.
- [Release notes](https://github.com/expressjs/cookie-parser/releases)
- [Changelog](https://github.com/expressjs/cookie-parser/blob/master/HISTORY.md)
- [Commits](https://github.com/expressjs/cookie-parser/compare/1.4.5...1.4.6)

---
updated-dependencies:
- dependency-name: cookie-parser
  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-01-25 10:29:10 +01:00
aa99b360d3
🔨 Temporary disable home assistant sensor (suspected to create crash) #102 2022-01-24 17:02:42 +01:00
15ed577682
🚑 Use new supervisor URL + new auth method #102 2022-01-24 16:59:14 +01:00
6 changed files with 513 additions and 588 deletions

File diff suppressed because it is too large Load Diff

View File

@ -9,18 +9,18 @@
"@fortawesome/fontawesome-free": "^5.15.3",
"app-root-path": "^3.0.0",
"bootstrap": "^5.0.1",
"cookie-parser": "^1.4.5",
"cookie-parser": "^1.4.6",
"cron": "^1.8.2",
"debug": "~4.3.1",
"debug": "~4.3.3",
"ejs": "~3.1.6",
"express": "~4.17.1",
"express": "~4.17.2",
"form-data": "^4.0.0",
"got": "^12.0.1",
"http-errors": "~1.8.0",
"http-errors": "~2.0.0",
"jquery": "^3.6.0",
"moment": "^2.29.1",
"morgan": "~1.10.0",
"webdav": "^4.5.0",
"winston": "^3.3.3"
"webdav": "^4.8.0",
"winston": "^3.4.0"
}
}

View File

@ -21,7 +21,7 @@ function updatetNextDate() {
class CronContainer {
constructor() {
this.cronJobs = [];
this.cronJob = null;
this.cronClean = null;
}
@ -33,70 +33,52 @@ class CronContainer {
this.cronClean = new CronJob("0 1 * * *", this._clean, null, false, Intl.DateTimeFormat().resolvedOptions().timeZone);
this.cronClean.start();
}
if (this.cronJob != null) {
logger.info("Stopping Cron...");
this.cronJob.stop();
this.cronJob = null;
}
if (!settingsTools.check_cron(settingsTools.getSettings())) {
logger.warn("No Cron settings available.");
return;
}
if(this.cronJobs.length != 0){
this._clean_cron_jobs();
}
if (settings.cron.length == 0){
switch (settings.cron_base) {
case "0":
logger.warn("No Cron settings available.");
}
else {
let i = 0;
for(let cron of settings.cron){
logger.info(`Starting cron #${i} ...`)
switch (cron.cron_base) {
return;
case "1": {
let splited = cron.cron_hour.split(":");
let splited = settings.cron_hour.split(":");
cronStr = "" + splited[1] + " " + splited[0] + " * * *";
break;
}
case "2": {
let splited = cron.cron_hour.split(":");
cronStr = "" + splited[1] + " " + splited[0] + " * * " + cron.cron_weekday;
let splited = settings.cron_hour.split(":");
cronStr = "" + splited[1] + " " + splited[0] + " * * " + settings.cron_weekday;
break;
}
case "3": {
let splited = cron.cron_hour.split(":");
cronStr = "" + splited[1] + " " + splited[0] + " " + cron.cron_month_day + " * *";
let splited = settings.cron_hour.split(":");
cronStr = "" + splited[1] + " " + splited[0] + " " + settings.cron_month_day + " * *";
break;
}
case "4": {
cronStr = cron.cron_custom;
cronStr = settings.cron_custom;
break;
}
}
this.cronJobs.push(new CronJob(cronStr, this._createBackup, null, true, Intl.DateTimeFormat().resolvedOptions().timeZone));
i ++ ;
}
}
logger.info("Starting Cron...");
this.cronJob = new CronJob(cronStr, this._createBackup, null, false, Intl.DateTimeFormat().resolvedOptions().timeZone);
this.cronJob.start();
this.updateNextDate();
}
updateNextDate() {
let date;
if (this.cronJobs.length == 0)
date = null;
else {
let last_date = null;
for(let item of this.cronJobs){
if(last_date == null)
last_date = item.nextDate();
else{
if(last_date > item.nextDate())
last_date = item.nextDate();
}
}
date = last_date.format("MMM D, YYYY HH:mm");
}
if (this.cronJob == null) date = null;
else date = this.cronJob.nextDate().format("MMM D, YYYY HH:mm");
let status = statusTools.getStatus();
status.next_backup = date;
statusTools.setStatus(status);
@ -142,16 +124,6 @@ class CronContainer {
webdav.clean().catch();
}
}
_clean_cron_jobs(){
let i = 0;
for(let elem of this.cronJobs){
logger.info(`Stopping Crong job #${i}`)
elem.stop();
i++;
}
this.cronJobs = []
}
}
class Singleton {

View File

@ -19,7 +19,7 @@ function getVersion() {
let token = process.env.HASSIO_TOKEN;
let status = statusTools.getStatus();
let option = {
headers: { "X-HASSIO-KEY": token },
headers: { "Authorization": `Bearer ${token}` },
responseType: "json",
};
@ -50,7 +50,7 @@ function getAddonList() {
let token = process.env.HASSIO_TOKEN;
let status = statusTools.getStatus();
let option = {
headers: { "X-HASSIO-KEY": token },
headers: { "Authorization": `Bearer ${token}` },
responseType: "json",
};
@ -97,6 +97,8 @@ function getAddonToBackup() {
if (!excluded_addon.includes(addon.slug))
slugs.push(addon.slug)
}
logger.debug("Addon to backup:")
logger.debug(slugs)
resolve(slugs)
})
.catch(() => reject());
@ -136,6 +138,8 @@ function getFolderToBackup() {
if (!excluded_folder.includes(folder.slug))
slugs.push(folder.slug)
}
logger.debug("Folders to backup:");
logger.debug(slugs)
return slugs;
}
@ -144,11 +148,11 @@ function getSnapshots() {
let token = process.env.HASSIO_TOKEN;
let status = statusTools.getStatus();
let option = {
headers: { "X-HASSIO-KEY": token },
headers: { "Authorization": `Bearer ${token}` },
responseType: "json",
};
got("http://hassio/snapshots", option)
got("http://hassio/backups", option)
.then((result) => {
if (status.error_code === 1) {
status.status = "idle";
@ -156,7 +160,7 @@ function getSnapshots() {
status.error_code = null;
statusTools.setStatus(status);
}
let snaps = result.body.data.snapshots;
let snaps = result.body.data.backups;
resolve(snaps);
})
.catch((error) => {
@ -184,11 +188,11 @@ function downloadSnapshot(id) {
status.progress = 0;
statusTools.setStatus(status);
let option = {
headers: { "X-HASSIO-KEY": token },
headers: { "Authorization": `Bearer ${token}` },
};
pipeline(
got.stream.get(`http://hassio/snapshots/${id}/download`, option)
got.stream.get(`http://hassio/backups/${id}/download`, option)
.on("downloadProgress", (e) => {
let percent = Math.round(e.percent * 100) / 100;
if (status.progress !== percent) {
@ -233,11 +237,11 @@ function dellSnap(id) {
let token = process.env.HASSIO_TOKEN;
let option = {
headers: { "X-HASSIO-KEY": token },
headers: { "Authorization": `Bearer ${token}` },
responseType: "json",
};
got.post(`http://hassio/snapshots/${id}/remove`, option)
got.post(`http://hassio/backups/${id}/remove`, option)
.then(() => resolve())
.catch(() => reject());
})
@ -251,11 +255,11 @@ function checkSnap(id) {
return new Promise((resolve, reject) => {
let token = process.env.HASSIO_TOKEN;
let option = {
headers: { "X-HASSIO-KEY": token },
headers: { "Authorization": `Bearer ${token}` },
responseType: "json",
};
got(`http://hassio/snapshots/${id}/info`, option)
got(`http://hassio/backups/${id}/info`, option)
.then((result) => {
logger.debug(`Snapshot size: ${result.body.data.size}`);
resolve();
@ -275,7 +279,7 @@ function createNewBackup(name) {
getAddonToBackup().then((addons) => {
let folders = getFolderToBackup();
let option = {
headers: { "X-HASSIO-KEY": token },
headers: { "Authorization": `Bearer ${token}` },
responseType: "json",
timeout: create_snap_timeout,
json: {
@ -288,7 +292,7 @@ function createNewBackup(name) {
option.json.password = settingsTools.getSettings().password_protect_value
}
got.post(`http://hassio/snapshots/new/partial`, option)
got.post(`http://hassio/backups/new/partial`, option)
.then((result) => {
logger.info(`Snapshot created with id ${result.body.data.slug}`);
resolve(result.body.data.slug);
@ -354,11 +358,11 @@ function uploadSnapshot(path) {
body: form,
username: this.username,
password: this.password,
headers: { "X-HASSIO-KEY": token },
headers: { "Authorization": `Bearer ${token}` },
};
got.stream
.post(`http://hassio/snapshots/new/upload`, options)
.post(`http://hassio/backups/new/upload`, options)
.on("uploadProgress", (e) => {
let percent = e.percent;
if (status.progress !== percent) {
@ -413,7 +417,7 @@ function stopAddons() {
let promises = [];
let token = process.env.HASSIO_TOKEN;
let option = {
headers: { "X-HASSIO-KEY": token },
headers: { "Authorization": `Bearer ${token}` },
responseType: "json",
};
let addons_slug = settingsTools.getSettings().auto_stop_addon
@ -496,72 +500,72 @@ function startAddons() {
function publish_state(state){
let data_error_sensor = {
state: state.status == "error" ? "on" : "off",
attributes: {
friendly_name: "Nexcloud Backup Error",
device_class: "problem",
error_code: state.error_code,
message: state.message,
icon: state.status == "error" ? "mdi:cloud-alert" : "mdi:cloud-check"
},
}
// let data_error_sensor = {
// state: state.status == "error" ? "on" : "off",
// attributes: {
// friendly_name: "Nexcloud Backup Error",
// device_class: "problem",
// error_code: state.error_code,
// message: state.message,
// icon: state.status == "error" ? "mdi:cloud-alert" : "mdi:cloud-check"
// },
// }
let token = process.env.HASSIO_TOKEN;
let option = {
headers: { "Authorization": `Bearer ${token}` },
responseType: "json",
json: data_error_sensor
};
got.post(`http://hassio/core/api/states/binary_sensor.nextcloud_backup_error`, option)
.then((result) => {
logger.debug('Home assistant sensor updated (error status)');
})
.catch((error) => {
logger.error(error);
});
// let token = process.env.HASSIO_TOKEN;
// let option = {
// headers: { "Authorization": `Bearer ${token}` },
// responseType: "json",
// json: data_error_sensor
// };
// got.post(`http://hassio/core/api/states/binary_sensor.nextcloud_backup_error`, option)
// .then((result) => {
// logger.debug('Home assistant sensor updated (error status)');
// })
// .catch((error) => {
// logger.error(error);
// });
let icon = ""
switch(state.status){
case "error":
icon = "mdi:cloud-alert";
break;
case "download":
case "download-b":
icon = "mdi:cloud-download";
break;
case "upload":
case "upload-b":
icon = "mdi:cloud-upload";
break;
case "idle":
icon = "mdi:cloud-check";
break;
default:
icon = "mdi:cloud-sync";
break;
}
// let icon = ""
// switch(state.status){
// case "error":
// icon = "mdi:cloud-alert";
// break;
// case "download":
// case "download-b":
// icon = "mdi:cloud-download";
// break;
// case "upload":
// case "upload-b":
// icon = "mdi:cloud-upload";
// break;
// case "idle":
// icon = "mdi:cloud-check";
// break;
// default:
// icon = "mdi:cloud-sync";
// break;
// }
let data_state_sensor = {
state: state.status,
attributes: {
friendly_name: "Nexcloud Backup Status",
error_code: state.error_code,
message: state.message,
icon: icon,
last_backup: state.last_backup == null || state.last_backup == "" ? "" : new Date(state.last_backup).toISOString(),
next_backup: state.next_backup == null || state.next_backup == "" ? "" : new Date(state.next_backup).toISOString()
},
}
option.json = data_state_sensor
got.post(`http://hassio/core/api/states/sensor.nextcloud_backup_status`, option)
.then((result) => {
logger.debug('Home assistant sensor updated (status)');
})
.catch((error) => {
logger.error(error);
});
// let data_state_sensor = {
// state: state.status,
// attributes: {
// friendly_name: "Nexcloud Backup Status",
// error_code: state.error_code,
// message: state.message,
// icon: icon,
// last_backup: state.last_backup == null || state.last_backup == "" ? "" : new Date(state.last_backup).toISOString(),
// next_backup: state.next_backup == null || state.next_backup == "" ? "" : new Date(state.next_backup).toISOString()
// },
// }
// option.json = data_state_sensor
// got.post(`http://hassio/core/api/states/sensor.nextcloud_backup_status`, option)
// .then((result) => {
// logger.debug('Home assistant sensor updated (status)');
// })
// .catch((error) => {
// logger.error(error);
// });
}
exports.getVersion = getVersion;

View File

@ -6,92 +6,48 @@ const CronJob = require("cron").CronJob;
const settingsPath = "/data/backup_conf.json";
function check_cron(conf) {
if (conf.cron != null) {
if (!Array.isArray(conf.cron))
return false;
for (let elem of conf.cron){
if (elem.cron_base != null) {
if (elem.cron_base === "1" || elem.cron_base === "2" || elem.cron_base === "3") {
if (elem.cron_hour != null && elem.cron_hour.match(/\d\d:\d\d/)) {
if (elem.cron_base === "1")
continue;
}
else
return false;
if (conf.cron_base != null) {
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 (elem.cron_base === "2") {
if( elem.cron_weekday != null && elem.cron_weekday >= 0 && elem.cron_weekday <= 6)
continue;
else
return false;
if (conf.cron_base === "2") {
return conf.cron_weekday != null && conf.cron_weekday >= 0 && conf.cron_weekday <= 6;
}
if (elem.cron_base === "3") {
if( elem.cron_month_day != null && elem.cron_month_day >= 1 && elem.cron_month_day <= 28)
continue;
else
return false
if (conf.cron_base === "3") {
return conf.cron_month_day != null && conf.cron_month_day >= 1 && conf.cron_month_day <= 28;
}
if (elem.cron_base === "4") {
if (elem.cron_custom != null) {
if (conf.cron_base === "4") {
if (conf.cron_custom != null) {
try {
new CronJob(elem.cron_custom, () => { });
continue;
new CronJob(conf.cron_custom, () => {});
return true;
} catch(e) {
return false;
}
}else return false;
}
else
if (conf.cron_base === "0") return true;
} else return false;
return false;
}
}
else
return false
}
}
else
return false;
return true;
}
function migrate_cron_conf(conf) {
let obj = {
cron_base: conf.cron_base,
cron_hour: conf.cron_hour,
cron_weekday: conf.cron_weekday,
cron_month_day: conf.cron_month_day,
cron_custom: conf.cron_custom,
}
conf.cron = [obj];
delete conf.cron_base;
delete conf.cron_hour;
delete conf.cron_weekday;
delete conf.cron_month_day;
delete conf.cron_custom;
return conf
}
function check(conf, fallback = false) {
let needSave = false;
if (conf.cron == null && !Array.isArray(conf.cron) && conf.cron_base != null) {
// Migrate to new cron conf format
logger.warn("Migrating to new cron conf format !")
conf = migrate_cron_conf(conf);
needSave = true;
}
if (!check_cron(conf)) {
if (fallback) {
logger.warn("Bad value for cron settings, fallback to default ");
conf.cron = []
conf.cron_base = "0";
conf.cron_hour = "00:00";
conf.cron_weekday = "0";
conf.cron_month_day = "1";
conf.cron_custom = "5 4 * * *";
} else {
logger.error("Bad value for cron settings");
return [false, "Bad cron settings"];
@ -228,7 +184,7 @@ function getSettings() {
}
function setSettings(settings) {
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
fs.writeFileSync(settingsPath, JSON.stringify(settings));
}
exports.getSettings = getSettings;

View File

@ -32,8 +32,8 @@ function getStatus() {
function setStatus(state) {
if (fs.existsSync(statusPath)) {
let old_state_str = fs.readFileSync(statusPath).toString();
if(old_state_str !== JSON.stringify(state, null, 2)){
fs.writeFileSync(statusPath, JSON.stringify(state, null, 2));
if(old_state_str !== JSON.stringify(state)){
fs.writeFileSync(statusPath, JSON.stringify(state));
hassioApiTools.publish_state(state);
}
}else{