From 2f6c5963c5076c246de1591ae41b92b26ab00932 Mon Sep 17 00:00:00 2001 From: Ponkhy Date: Wed, 1 Sep 2021 17:09:32 +0200 Subject: [PATCH] Added import and export function --- server/server.js | 74 ++++++++++++++++++++++++++++++++++++++++++ src/mixins/socket.js | 3 ++ src/pages/Settings.vue | 54 ++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) diff --git a/server/server.js b/server/server.js index d4fe668b3..8485c3841 100644 --- a/server/server.js +++ b/server/server.js @@ -552,6 +552,80 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); } }); + socket.on("uploadBackup", async (uploadedJSON, callback) => { + try { + checkLogin(socket) + + console.log(`Import Backup User ID: ${socket.userID}`) + + let notificationList = uploadedJSON[0]; + let monitorList = uploadedJSON[1]; + + monitorList = JSON.stringify(monitorList); + monitorList = JSON.parse(monitorList); + monitorList = Object.values(monitorList); + + if (notificationList.length >= 1) { + for (let i = 0; i < notificationList.length; i++) { + let notification = JSON.parse(notificationList[i].config); + await Notification.save(notification, null, socket.userID) + } + } + + if (monitorList.length >= 1) { + for (let i = 0; i < monitorList.length; i++) { + let monitor = { + name: monitorList[i].name, + type: monitorList[i].type, + url: monitorList[i].url, + interval: monitorList[i].interval, + hostname: monitorList[i].hostname, + maxretries: monitorList[i].maxretries, + port: monitorList[i].port, + keyword: monitorList[i].keyword, + ignoreTls: monitorList[i].ignoreTls, + upsideDown: monitorList[i].upsideDown, + maxredirects: monitorList[i].maxredirects, + accepted_statuscodes: monitorList[i].accepted_statuscodes, + dns_resolve_type: monitorList[i].dns_resolve_type, + dns_resolve_server: monitorList[i].dns_resolve_server, + notificationIDList: {}, + } + + let bean = R.dispense("monitor") + + let notificationIDList = monitor.notificationIDList; + delete monitor.notificationIDList; + + monitor.accepted_statuscodes_json = JSON.stringify(monitor.accepted_statuscodes); + delete monitor.accepted_statuscodes; + + bean.import(monitor) + bean.user_id = socket.userID + await R.store(bean) + + await updateMonitorNotification(bean.id, notificationIDList) + + await startMonitor(socket.userID, bean.id); + } + + await sendNotificationList(socket) + await sendMonitorList(socket); + } + + callback({ + ok: true, + msg: "Backup successfully restored.", + }); + + } catch (e) { + callback({ + ok: false, + msg: e.message, + }); + } + }); + debug("added all socket handlers") // *************************** diff --git a/src/mixins/socket.js b/src/mixins/socket.js index 454422e94..867abfd01 100644 --- a/src/mixins/socket.js +++ b/src/mixins/socket.js @@ -254,6 +254,9 @@ export default { this.importantHeartbeatList = {} }, + uploadBackup(uploadedJSON, callback) { + socket.emit("uploadBackup", uploadedJSON, callback) + }, }, computed: { diff --git a/src/pages/Settings.vue b/src/pages/Settings.vue index d71ec109b..c15b1410a 100644 --- a/src/pages/Settings.vue +++ b/src/pages/Settings.vue @@ -120,6 +120,14 @@ +

{{ $t("Import/Export Backup") }}

+ +
+ + + +
+

{{ $t("Advanced") }}

@@ -293,6 +301,41 @@ export default { this.$root.storage().removeItem("token"); }, + downloadBackup() { + let time = dayjs().format("YYYY_MM_DD-hh_mm_ss"); + let fileName = `Uptime_Kuma_Backup_${time}.json`; + let exportData = { + notificationList: this.$root.notificationList, + monitorList: this.$root.monitorList, + } + exportData = JSON.stringify(exportData); + let downloadItem = document.createElement("a"); + downloadItem.setAttribute("href", `data:application/json;charset=utf-8, ${exportData}`); + downloadItem.setAttribute("download", fileName); + downloadItem.click(); + }, + + importBackup() { + let uploadItem = document.getElementById("importBackup").files; + if (uploadItem.length <= 0) { + return false; + } + + let fileReader = new FileReader(); + fileReader.readAsText(uploadItem.item(0)); + + fileReader.onload = item => { + let resultParse = JSON.parse(item.target.result); + let result = Object.values(resultParse); + this.$root.uploadBackup(result, (res) => { + if (res.ok) { + toast.success(res.msg); + } else { + toast.error(res.msg); + } + }) + } + }, }, } @@ -321,6 +364,17 @@ export default { .btn-check:hover + .btn-outline-primary { color: #000; } + + input[type=file]::file-selector-button { + color: $primary; + background-color: $dark-bg; + } + + //Does not work for some reason + input[type=file]::file-selector-button:hover { + color: $dark-font-color; + background-color: $primary; + } } footer {