Skip to content

Commit

Permalink
added windows backup
Browse files Browse the repository at this point in the history
  • Loading branch information
mtttia committed Nov 8, 2021
1 parent 6e29d1f commit 3c918be
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
24 changes: 22 additions & 2 deletions src/background/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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

Expand All @@ -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 = ""
Expand Down
6 changes: 4 additions & 2 deletions src/class/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down
21 changes: 20 additions & 1 deletion src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -147,6 +154,18 @@ function populateStateModal() {
document.getElementById('sm-time').innerText = `Orario: ${time.hour}:${time.minutes}`
document.getElementById('sm-folder').innerHTML = `Sorgente: ${setting.SrcFolder} <br>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
Expand Down
14 changes: 14 additions & 0 deletions src/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ <h2 class="accordion-header" id="headingOne">
</div>
</div>
<hr>
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="normalBackup" checked>
<label class="form-check-label" for="normalBackup" id="txtnormalBackup">
Backup normale (default)
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="windowsBackup">
<label class="form-check-label" for="windowsBackup" id="txtwindowsBackup">
Windows backup (in caso di non funzionamento del normale)
</label>
</div>
<button class="btn btn-primary" id="btnChangeBackupType">Salva</button>
<hr>
Log
<div class="accordion" id="sm-accordationLog">

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -188,6 +189,7 @@ const createMainWindows = () => {
mainWindow.addListener('closed', (e)=>{
mainClosed = true
})

}

const createWorkerWindows = () => {
Expand Down

0 comments on commit 3c918be

Please sign in to comment.