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.
This commit is contained in:
leiweibau
2022-06-22 08:58:55 +02:00
parent fac3e72dd9
commit bd4da40310
2 changed files with 61 additions and 8 deletions

View File

@@ -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);
}
?>
<div class="table">
@@ -62,26 +70,32 @@ exec($execstring, $pia_nmapscans);
<div class="table-cell">last Modification</div>
<div class="table-cell"><?php echo $pia_db_mod;?></div>
</div>
<div class="table-row">
<div class="table-cell">DB Backup</div>
<div class="table-cell"><?php echo $Pia_Archive_count.' Backups where found';?></div>
</div>
<div class="table-row">
<div class="table-cell">Scan Status (arp)</div>
<div class="table-cell"><?php echo sizeof($pia_arpscans);?> Scans currently running</div>
<div class="table-cell"><?php echo sizeof($pia_arpscans);?> scan(s) currently running</div>
</div>
<div class="table-row">
<div class="table-cell">Scan Status (nmap)</div>
<div class="table-cell"><?php echo sizeof($pia_nmapscans);?> Scans currently running</div>
<div class="table-cell"><?php echo sizeof($pia_nmapscans);?> scan(s) currently running</div>
</div>
</div>
<div class="col-xs-12" style="text-align:center; padding-top: 10px; margin-bottom: 50px;">
<button type="button" class="btn btn-default pa-btn pa-btn-delete bg-red dbtools-button" id="btnDeleteMAC" style="border-top: solid 3px #dd4b39;" onclick="askDeleteDevicesWithEmptyMACs()">Delete Devices with empty MACs</button>
<button type="button" class="btn btn-default pa-btn pa-btn-delete bg-red dbtools-button" id="btnDeleteMAC" style="border-top: solid 3px #dd4b39;" onclick="askDeleteDevicesWithEmptyMACs()">Delete Devices with empty MACs</button>
<button type="button" class="btn btn-default pa-btn pa-btn-delete bg-red dbtools-button" id="btnDeleteMAC" style="border-top: solid 3px #dd4b39;" onclick="askDeleteAllDevices()">Delete All Devices</button>
<button type="button" class="btn btn-default pa-btn pa-btn-delete bg-red dbtools-button" id="btnDeleteMAC" style="border-top: solid 3px #dd4b39;" onclick="askDeleteAllDevices()">Delete All Devices</button>
<button type="button" class="btn btn-default pa-btn pa-btn-delete bg-red dbtools-button" id="btnDeleteUnknown" style="border-top: solid 3px #dd4b39;" onclick="askDeleteUnknown()">Delete (unknown) Devices</button>
<button type="button" class="btn btn-default pa-btn pa-btn-delete bg-red dbtools-button" id="btnDeleteUnknown" style="border-top: solid 3px #dd4b39;" onclick="askDeleteUnknown()">Delete (unknown) Devices</button>
<button type="button" class="btn btn-default pa-btn pa-btn-delete bg-red dbtools-button" id="btnDeleteEvents" style="border-top: solid 3px #dd4b39;" onclick="askDeleteEvents()">Delete all Events (Reset Presence)</button>
<button type="button" class="btn btn-default pa-btn pa-btn-delete bg-red dbtools-button" id="btnDeleteEvents" style="border-top: solid 3px #dd4b39;" onclick="askDeleteEvents()">Delete all Events (Reset Presence)</button>
<button type="button" class="btn btn-default pa-btn pa-btn-delete bg-red dbtools-button" id="btnPiaBackupDBtoArchive" style="border-top: solid 3px #dd4b39;" onclick="askPiaBackupDBtoArchive()">Execute DB Backup</button>
</div>
@@ -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?<br>(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);
});
}
</script>

View File

@@ -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";
}
}
//------------------------------------------------------------------------------