Skip to content

Commit

Permalink
🔨 Add settings for partial backup #33
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebclem committed Dec 6, 2020
1 parent 619aa80 commit 310ecf1
Show file tree
Hide file tree
Showing 5 changed files with 383 additions and 212 deletions.
209 changes: 109 additions & 100 deletions nextcloud_backup/rootfs/opt/nextcloud_backup/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const humanFileSize = require("../tools/toolbox").humanFileSize;
const cronTools = require("../tools/cronTools");

const logger = require("../config/winston");
const { add } = require("../config/winston");

router.get("/status", (req, res, next) => {
cronTools.updatetNextDate();
Expand All @@ -36,11 +37,11 @@ router.get("/formated-local-snap", function (req, res, next) {
res.status(500);
res.send("");
}
);
});

router.get("/formated-backup-manual", function (req, res, next) {
webdav
);
});
router.get("/formated-backup-manual", function (req, res, next) {
webdav
.getFolderContent(webdav.getConf().back_dir + pathTools.manual)
.then((contents) => {
contents.sort((a, b) => {
Expand All @@ -52,11 +53,11 @@ router.get("/formated-backup-manual", function (req, res, next) {
.catch(() => {
res.send();
});
});

router.get("/formated-backup-auto", function (req, res, next) {
let url = webdav.getConf().back_dir + pathTools.auto;
webdav
});
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) => {
Expand All @@ -68,13 +69,13 @@ router.get("/formated-backup-auto", function (req, res, next) {
.catch(() => {
res.send();
});
});

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
});
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);
Expand All @@ -84,33 +85,33 @@ router.post("/nextcloud-settings", function (req, res, next) {
res.status(406);
res.json({ message: err });
});
} else {
res.status(400);
res.send();
}
});

router.get("/nextcloud-settings", function (req, res, next) {
let conf = webdav.getConf();
if (conf == null) {
res.status(404);
res.send();
} else {
res.json(conf);
}
});

router.post("/manual-backup", function (req, res, next) {
let id = req.query.id;
let name = req.query.name;
let status = statusTools.getStatus();
if (status.status == "creating" && status.status == "upload" && status.status == "download") {
res.status(503);
res.send();
return;
}

hassioApiTools
} else {
res.status(400);
res.send();
}
});
router.get("/nextcloud-settings", function (req, res, next) {
let conf = webdav.getConf();
if (conf == null) {
res.status(404);
res.send();
} else {
res.json(conf);
}
});
router.post("/manual-backup", function (req, res, next) {
let id = req.query.id;
let name = req.query.name;
let status = statusTools.getStatus();
if (status.status == "creating" && status.status == "upload" && status.status == "download") {
res.status(503);
res.send();
return;
}
hassioApiTools
.downloadSnapshot(id)
.then(() => {
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + ".tar");
Expand All @@ -121,76 +122,84 @@ router.post("/manual-backup", function (req, res, next) {
res.status(500);
res.send();
});
});

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;
}
hassioApiTools
});
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;
}
hassioApiTools
.getVersion()
.then((version) => {
let name = settingsTools.getFormatedName(true, version);
hassioApiTools
.createNewBackup(name)
.then((id) => {
hassioApiTools
.downloadSnapshot(id)
.then(() => {
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + ".tar");
})
.catch(() => {});
.createNewBackup(name)
.then((id) => {
hassioApiTools
.downloadSnapshot(id)
.then(() => {
webdav.uploadFile(id, webdav.getConf().back_dir + pathTools.manual + name + ".tar");
})
.catch(() => {});
})
.catch(() => {});
})
.catch(() => {});

res.status(201);
res.send();
});

router.get("/backup-settings", function (req, res, next) {
res.send(settingsTools.getSettings());
});

router.post("/backup-settings", function (req, res, next) {
if (settingsTools.check(req.body)) {
settingsTools.setSettings(req.body);
cronTools.startCron();

res.status(201);
res.send();
} else {
res.status(400);
res.send();
}
});

router.post("/clean-now", function (req, res, next) {
webdav
});

router.get("/backup-settings", function (req, res, next) {
hassioApiTools.getAddonList().then((addonList)=>{
let data = {};
data['folders'] = hassioApiTools.getFolderList();
data['addonList'] = addonList;
data['settings'] = settingsTools.getSettings();
res.send(data);
})

});

router.post("/backup-settings", function (req, res, next) {
if (settingsTools.check(req.body)) {
settingsTools.setSettings(req.body);
cronTools.startCron();
res.send();
} else {
res.status(400);
res.send();
}
});

router.post("/clean-now", function (req, res, next) {
webdav
.clean()
.then(() => {
hassioApiTools.clean().catch();
})
.catch(() => {
hassioApiTools.clean().catch();
});
res.status(201);
res.send();
});

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.status(400);
res.status(201);
res.send();
}
});

module.exports = router;
});

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.status(400);
res.send();
}
});

module.exports = router;

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,75 @@ function getVersion() {
});
}

function getAddonList() {
return new Promise((resolve, reject) => {
let token = process.env.HASSIO_TOKEN;
let status = statusTools.getStatus();
let option = {
headers: { "X-HASSIO-KEY": token },
responseType: "json",
};

got("http://hassio/addons", option)
.then((result) => {
if (status.error_code === 1) {
status.status = "idle";
status.message = null;
status.error_code = null;
statusTools.setStatus(status);
}
let addons = result.body.data.addons;
let instaled = [];
for(let index in addons){
let current = addons[index];
if(current.installed == true){
instaled.push({slug:current.slug, name: current.name})
}
}
instaled.sort((a,b)=>{
var textA = a.name.toUpperCase();
var textB = b.name.toUpperCase();
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});
resolve(instaled);
})
.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);
});
});
}

function getFolderList(){
return [
{
name: "Homme Assistant configuration",
slug: "homeassistant"

},
{
name: "SSL",
slug: "ssl"
},
{
name: "Share",
slug: "share"
},
{
name: "Media",
slug: "media"
},
{
name: "Local add-ons",
slug: "addons/local"
}
]
}


function getSnapshots() {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -302,6 +371,8 @@ function uploadSnapshot(path) {
}

exports.getVersion = getVersion;
exports.getAddonList = getAddonList;
exports.getFolderList = getFolderList;
exports.getSnapshots = getSnapshots;
exports.downloadSnapshot = downloadSnapshot;
exports.createNewBackup = createNewBackup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function check_cron(conf){


function check(conf, fallback = false){
let needSave = false;
if(!check_cron(conf)){
if(fallback){
logger.warn("Bad value for cron settings, fallback to default ")
Expand Down Expand Up @@ -92,7 +93,39 @@ function check(conf, fallback = false){
return false;
}
}
if(fallback){
if(conf.exclude_addon == null){
if(fallback){
logger.warn("Bad value for 'exclude_addon', fallback to [] ")
conf.exclude_addon = []
}
else {
logger.error("Bad value for 'exclude_addon'")
return false;
}
}
if(conf.exclude_folder == null){
if(fallback){
logger.warn("Bad value for 'exclude_folder', fallback to [] ")
conf.exclude_folder = []
}
else {
logger.error("Bad value for 'exclude_folder'")
return false;
}
}

if(!Array.isArray(conf.exclude_folder)){
logger.debug("exclude_folder is not array (Empty value), reset...");
conf.exclude_folder = []
needSave = true;
}
if(!Array.isArray(conf.exclude_addon)){
logger.debug("exclude_addon is not array (Empty value), reset...");
conf.exclude_addon = []
needSave = true;
}

if(fallback || needSave){
setSettings(conf);
}
return true
Expand Down
Loading

0 comments on commit 310ecf1

Please sign in to comment.