From 3c918bec7e1a20e604e1d83afef509a75e98ca26 Mon Sep 17 00:00:00 2001 From: mtttia Date: Mon, 8 Nov 2021 21:56:04 +0100 Subject: [PATCH] added windows backup --- src/background/worker.js | 24 ++++++++++++++++++++++-- src/class/setting.js | 6 ++++-- src/client/client.js | 21 ++++++++++++++++++++- src/client/index.html | 14 ++++++++++++++ src/index.js | 2 ++ 5 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/background/worker.js b/src/background/worker.js index fc7650b..9aac838 100644 --- a/src/background/worker.js +++ b/src/background/worker.js @@ -4,6 +4,7 @@ const { existsSync } = require('fs') const { ipcRenderer } = require('electron') const Setting = require('./../class/setting') const Log = require('./../class/log') +const { exec } = require('child_process') let setting = null let task = null @@ -59,7 +60,12 @@ async function backup(arg) { report.SrcFolder = arg.SrcFolder report.StartHour = `${today.getHours()}:${today.getMinutes()}` try { - await copyDir(arg.SrcFolder, arg.DistFolder) + if (setting.WinBakcup) { + await backupWindows(arg.SrcFolder, arg.DistFolder) + } + else { + await copyDir(arg.SrcFolder, arg.DistFolder) + } let now = new Date() report.EndHour = `${now.getHours()}:${now.getMinutes()}` report.Description = "" @@ -92,6 +98,15 @@ async function backup(arg) { } } +async function backupWindows(src, dist) { + try { + alert('windows backup done') + await exec(`xcopy ${src} ${dist} /Q /C /R /Y /E /H /I`) + } catch (ex) { + throw ex + } +} + async function cronFunction() { if (pause) return @@ -105,7 +120,12 @@ async function cronFunction() { report.SrcFolder = setting.SrcFolder report.StartHour = `${today.getHours()}:${today.getMinutes()}` try { - await copyDir(setting.SrcFolder, setting.DistFolder) + if (setting.WinBakcup) { + await backupWindows(setting.SrcFolder, setting.DistFolder) + } + else { + await copyDir(setting.SrcFolder, setting.DistFolder) + } let now = new Date() report.EndHour = `${now.getHours()}:${now.getMinutes()}` report.Description = "" diff --git a/src/class/setting.js b/src/class/setting.js index 1cf190f..c1fc231 100644 --- a/src/class/setting.js +++ b/src/class/setting.js @@ -7,18 +7,20 @@ class Setting{ CronPattern; // "* * * * *" -> see documentation here https://www.npmjs.com/package/node-cron+รน SrcFolder; DistFolder; + WinBakcup; - constructor(cronPattern, srcFolder, distFolder, save = false){ + constructor(cronPattern, srcFolder, distFolder, winBakcup,save = false){ this.CronPattern = cronPattern this.SrcFolder = srcFolder this.DistFolder = distFolder + this.WinBakcup = winBakcup if (save) this.save() } static fromJson(obj) { if(obj.CronPattern && obj.SrcFolder && obj.DistFolder) - return new Setting(obj.CronPattern, obj.SrcFolder, obj.DistFolder, false) + return new Setting(obj.CronPattern, obj.SrcFolder, obj.DistFolder, obj.WinBakcup, false) else throw new Error('Object is not a Setting object') } diff --git a/src/client/client.js b/src/client/client.js index fbc03cb..b932b2d 100644 --- a/src/client/client.js +++ b/src/client/client.js @@ -88,7 +88,7 @@ function initializeSetting() { const pattern = `${minutes} ${hour} ${dayOfMonth} ${month} ${dayOfWeek}` console.log(pattern) - setting = new Setting(pattern, srcFolder, distFolder, true) + setting = new Setting(pattern, srcFolder, distFolder, false, true) //run real app document.getElementById('welcome').classList.add('d-none') document.getElementById('home').classList.remove('d-none') @@ -139,6 +139,13 @@ ipcRenderer.on('log', (event, arg) => { populateStateModal() }) +document.getElementById('btnChangeBackupType').addEventListener('click', () => { + setting.WinBakcup = document.getElementById('windowsBackup').checked + setting.save() + ipcRenderer.send('say-upload-setting', '') + populateStateModal() +}) + function populateStateModal() { //setting data document.getElementById('sm-type').innerText = "Tipo: " + type @@ -147,6 +154,18 @@ function populateStateModal() { document.getElementById('sm-time').innerText = `Orario: ${time.hour}:${time.minutes}` document.getElementById('sm-folder').innerHTML = `Sorgente: ${setting.SrcFolder}
Destinazione: ${setting.DistFolder}` + document.getElementById('normalBackup').checked = !setting.WinBakcup + document.getElementById('windowsBackup').checked = setting.WinBakcup + + if (setting.WinBakcup) { + document.getElementById('txtwindowsBackup').style.color = '#006d77' + document.getElementById('txtnormalBackup').style.color = '#000' + } + else { + document.getElementById('txtwindowsBackup').style.color = '#000' + document.getElementById('txtnormalBackup').style.color = '#006d77' + } + //logs data let html = '' let id = 0 diff --git a/src/client/index.html b/src/client/index.html index 13d6f05..07ebb98 100644 --- a/src/client/index.html +++ b/src/client/index.html @@ -107,6 +107,20 @@


+
+ + +
+
+ + +
+ +
Log
diff --git a/src/index.js b/src/index.js index 9fda5fd..aa15a5d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +const { main } = require('@popperjs/core'); const { app, BrowserWindow, ipcMain, Menu } = require('electron'); const path = require('path'); const BackupData = require('./class/backupData') @@ -188,6 +189,7 @@ const createMainWindows = () => { mainWindow.addListener('closed', (e)=>{ mainClosed = true }) + } const createWorkerWindows = () => {