Added import and export function

This commit is contained in:
Ponkhy 2021-09-01 17:09:32 +02:00
parent 8ea4dec5a0
commit 2f6c5963c5
3 changed files with 131 additions and 0 deletions

View File

@ -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") debug("added all socket handlers")
// *************************** // ***************************

View File

@ -254,6 +254,9 @@ export default {
this.importantHeartbeatList = {} this.importantHeartbeatList = {}
}, },
uploadBackup(uploadedJSON, callback) {
socket.emit("uploadBackup", uploadedJSON, callback)
},
}, },
computed: { computed: {

View File

@ -120,6 +120,14 @@
</form> </form>
</template> </template>
<h2 class="mt-5 mb-2">{{ $t("Import/Export Backup") }}</h2>
<div class="input-group mb-3">
<button class="btn btn-outline-primary" @click="downloadBackup">{{ $t("Export") }}</button>
<button type="button" class="btn btn-outline-primary" @click="importBackup">{{ $t("Import") }}</button>
<input id="importBackup" type="file" class="form-control">
</div>
<h2 class="mt-5 mb-2">{{ $t("Advanced") }}</h2> <h2 class="mt-5 mb-2">{{ $t("Advanced") }}</h2>
<div class="mb-3"> <div class="mb-3">
@ -293,6 +301,41 @@ export default {
this.$root.storage().removeItem("token"); 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);
}
})
}
},
}, },
} }
</script> </script>
@ -321,6 +364,17 @@ export default {
.btn-check:hover + .btn-outline-primary { .btn-check:hover + .btn-outline-primary {
color: #000; 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 { footer {