Added import and export function

pull/306/head
Ponkhy 3 years ago
parent 8ea4dec5a0
commit 2f6c5963c5

@ -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")
// ***************************

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

@ -120,6 +120,14 @@
</form>
</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>
<div class="mb-3">
@ -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);
}
})
}
},
},
}
</script>
@ -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 {

Loading…
Cancel
Save