From 97cfb84bdc7ea6b23dabb1cae243851465a3685c Mon Sep 17 00:00:00 2001 From: leiweibau <105860611+leiweibau@users.noreply.github.com> Date: Wed, 22 Jun 2022 08:58:55 +0200 Subject: [PATCH] Add DB Backup to Maintenance Added the possibility to perform a backup of the database. This creates a "pending" backup (a copy of the database) and from it an archive with the current date. The "pending" backup is overwritten with the next backup. The status information on the maintenance page has been supplemented accordingly. --- front/maintenance.php | 46 +++++++++++++++++++++++++++++------- front/php/server/devices.php | 23 ++++++++++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/front/maintenance.php b/front/maintenance.php index cef40efe..bf5c49ec 100644 --- a/front/maintenance.php +++ b/front/maintenance.php @@ -35,7 +35,7 @@ $pia_db = str_replace('front', 'db', getcwd()).'/pialert.db'; //echo $pia_db; -$pia_db_size = number_format(filesize($pia_db),2,",",".") . ' Byte'; +$pia_db_size = number_format(filesize($pia_db),0,",",".") . ' Byte'; //echo $pia_db_size; $pia_db_mod = date ("F d Y H:i:s", filemtime($pia_db)); @@ -47,6 +47,14 @@ $execstring = 'ps -f -u pi | grep "nmap" 2>&1'; $pia_nmapscans = ""; exec($execstring, $pia_nmapscans); +$Pia_Archive_Path = "/home/pi/pialert/db/"; +$Pia_Archive_count = 0; +$files = glob($Pia_Archive_Path . "*.zip"); +if ($files){ + $Pia_Archive_count = count($files); +} + + ?>
@@ -62,26 +70,32 @@ exec($execstring, $pia_nmapscans);
last Modification
+
+
DB Backup
+
+
Scan Status (arp)
-
Scans currently running
+
scan(s) currently running
Scan Status (nmap)
-
Scans currently running
+
scan(s) currently running
- + - + - + - + + +
@@ -104,7 +118,7 @@ exec($execstring, $pia_nmapscans); // delete devices with emty macs - function askDeleteDevicesWithEmptyMACs () { +function askDeleteDevicesWithEmptyMACs () { // Ask showModalWarning('Delete Devices', 'Are you sure you want to delete all devices with empty MAC addresses?
(maybe you prefer to archive it)', 'Cancel', 'Delete', 'deleteDevicesWithEmptyMACs'); @@ -168,6 +182,22 @@ function deleteEvents() } +// Backup DB to Archive +function askPiaBackupDBtoArchive () { + // Ask + showModalWarning('DB Backup', 'Are you sure you want to exectute the the DB Backup? Be sure that no scan is currently running.', + 'Cancel', 'Run Backup', 'PiaBackupDBtoArchive'); +} + + +function PiaBackupDBtoArchive() +{ + // Execute + $.get('php/server/devices.php?action=PiaBackupDBtoArchive', function(msg) { + showMessage (msg); + }); +} + diff --git a/front/php/server/devices.php b/front/php/server/devices.php index 39585f08..c77494a8 100644 --- a/front/php/server/devices.php +++ b/front/php/server/devices.php @@ -39,6 +39,7 @@ case 'runScan1min': runScan1min(); break; case 'deleteUnknownDevices': deleteUnknownDevices(); break; case 'deleteEvents': deleteEvents(); break; + case 'PiaBackupDBtoArchive': PiaBackupDBtoArchive(); break; case 'getDevicesTotals': getDevicesTotals(); break; @@ -260,7 +261,29 @@ function deleteEvents() { } } +//------------------------------------------------------------------------------ +// Backup DB to Archiv +//------------------------------------------------------------------------------ +function PiaBackupDBtoArchive() { + $file = '/home/pi/pialert/db/pialert.db'; + $newfile = '/home/pi/pialert/db/pialert.db.backup'; + + if (!copy($file, $newfile)) { + echo "Test Function executed not successfully"; + } else { + $Pia_Archive_Name = 'pialertdb_'.date("Ymd_his").'.zip'; + $Pia_Archive_Path = '/home/pi/pialert/db/'; + exec('zip -j '.$Pia_Archive_Path.$Pia_Archive_Name.' /home/pi/pialert/db/pialert.db', $output); + if (file_exists($Pia_Archive_Path.$Pia_Archive_Name)) { + echo 'Test Function executed successfully ('.$Pia_Archive_Name.')'; + } else { + echo 'Test Function executed successfully'; + } + // echo "Test Function executed successfully"; + } + +} //------------------------------------------------------------------------------