Add DB Restore to Maintenance

The function to restore a backup has been added. The "pending" backups are now deleted after the restore.
A backup is also created before a restore, but it is also deleted after the restore. When restoring, the existing database is overwritten by the backup.

CSS adjustments were made to eliminate errors in darkmode and lightmode.
This commit is contained in:
leiweibau
2022-06-22 19:11:42 +02:00
parent 1dcdea964a
commit 9097421ad6
4 changed files with 91 additions and 40 deletions

View File

@@ -662,3 +662,5 @@ input[type="password"]::-webkit-caps-lock-indicator {
color: #444;
}
.db_info_table_cell:nth-child(1) {background: #272c30}
.db_info_table_cell:nth-child(2) {background: #272c30}

View File

@@ -456,20 +456,20 @@
margin: 5px; display: inline-block; width: 140px; height: 140px; white-space: normal; word-wrap: break-word; font-size: 16px; border-radius: 10px;
}
.table-cell:nth-child(1) {background: #272c30}
.table-cell:nth-child(2) {background: #272c30}
.db_info_table_cell:nth-child(1) {background: white}
.db_info_table_cell:nth-child(2) {background: white}
.table {
.db_info_table {
display: table;
border-spacing: 0em;
font-weight: 400;
font-size: 16px;
font-size: 15px;
width: 95%;
margin: auto;
margin-top: 40px;
margin-bottom: 40px;
}
.table-row {display: table-row; padding: 3px; }
.table-cell {display: table-cell; padding: 3px; padding-left: 10px;}
.db_info_table_row {display: table-row; padding: 3px; }
.db_info_table_cell {display: table-cell; padding: 3px; padding-left: 10px;}
}

View File

@@ -28,7 +28,7 @@
</section>
<!-- Main content ---------------------------------------------------------- -->
<section class="content" style="min-height: 400px;">
<section class="content" style="min-height: 750px;">
<?php
@@ -49,38 +49,42 @@ exec($execstring, $pia_nmapscans);
$Pia_Archive_Path = str_replace('front', 'db', getcwd()).'/';
$Pia_Archive_count = 0;
$files = glob($Pia_Archive_Path . "*.zip");
$files = glob($Pia_Archive_Path."*.zip");
if ($files){
$Pia_Archive_count = count($files);
}
$latestfiles = glob($Pia_Archive_Path."*.zip");
natsort($latestfiles);
$latestfiles = array_reverse($latestfiles,False);
$latestbackup = $latestfiles[0];
$latestbackup_date = date ("Y-m-d H:i:s", filemtime($latestbackup));
?>
<div class="table">
<div class="table-row">
<div class="table-cell">Database-Path</div>
<div class="table-cell"><?php echo $pia_db;?></div>
<div class="db_info_table">
<div class="db_info_table_row">
<div class="db_info_table_cell">Database-Path</div>
<div class="db_info_table_cell"><?php echo $pia_db;?></div>
</div>
<div class="table-row">
<div class="table-cell">Database-Size</div>
<div class="table-cell"><?php echo $pia_db_size;?></div>
<div class="db_info_table_row">
<div class="db_info_table_cell">Database-Size</div>
<div class="db_info_table_cell"><?php echo $pia_db_size;?></div>
</div>
<div class="table-row">
<div class="table-cell">last Modification</div>
<div class="table-cell"><?php echo $pia_db_mod;?></div>
<div class="db_info_table_row">
<div class="db_info_table_cell">last Modification</div>
<div class="db_info_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 class="db_info_table_row">
<div class="db_info_table_cell">DB Backup</div>
<div class="db_info_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);?> scan(s) currently running</div>
<div class="db_info_table_row">
<div class="db_info_table_cell">Scan Status (arp)</div>
<div class="db_info_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);?> scan(s) currently running</div>
<div class="db_info_table_row">
<div class="db_info_table_cell">Scan Status (nmap)</div>
<div class="db_info_table_cell"><?php echo sizeof($pia_nmapscans);?> scan(s) currently running</div>
</div>
</div>
@@ -95,7 +99,9 @@ if ($files){
<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>
<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()">DB Backup</button>
<button type="button" class="btn btn-default pa-btn pa-btn-delete bg-red dbtools-button" id="btnPiaRestoreDBfromArchive" style="border-top: solid 3px #dd4b39;" onclick="askPiaRestoreDBfromArchive()">DB Restore<br><?php echo $latestbackup_date;?></button>
</div>
@@ -199,6 +205,22 @@ function PiaBackupDBtoArchive()
}
// Restore DB from Archive
function askPiaRestoreDBfromArchive () {
// Ask
showModalWarning('DB Restore', 'Are you sure you want to exectute the the DB Restore? Be sure that no scan is currently running.',
'Cancel', 'Run Restore', 'PiaRestoreDBfromArchive');
}
function PiaRestoreDBfromArchive()
{
// Execute
$.get('php/server/devices.php?action=PiaRestoreDBfromArchive', function(msg) {
showMessage (msg);
});
}
</script>

View File

@@ -40,8 +40,8 @@
case 'deleteUnknownDevices': deleteUnknownDevices(); break;
case 'deleteEvents': deleteEvents(); break;
case 'PiaBackupDBtoArchive': PiaBackupDBtoArchive(); break;
case 'PiaRestoreDBfromArchive': PiaRestoreDBfromArchive(); break;
case 'getDevicesTotals': getDevicesTotals(); break;
case 'getDevicesList': getDevicesList(); break;
case 'getDevicesListCalendar': getDevicesListCalendar(); break;
@@ -265,27 +265,54 @@ function deleteEvents() {
// Backup DB to Archiv
//------------------------------------------------------------------------------
function PiaBackupDBtoArchive() {
// $DBFILE = '../../../db/pialert.db';
// prepare fast Backup
$file = '../../../db/pialert.db';
$newfile = '../../../db/pialert.db.backup';
$newfile = '../../../db/pialert.db.latestbackup';
// copy files as a fast Backup
if (!copy($file, $newfile)) {
echo "Test Function executed not successfully";
echo "The Backup executed not successfully";
} else {
$Pia_Archive_Name = 'pialertdb_'.date("Ymd_his").'.zip';
// Create archive with actual date
$Pia_Archive_Name = 'pialertdb_'.date("Ymd_His").'.zip';
$Pia_Archive_Path = '../../../db/';
exec('zip -j '.$Pia_Archive_Path.$Pia_Archive_Name.' ../../../db/pialert.db', $output);
if (file_exists($Pia_Archive_Path.$Pia_Archive_Name)) {
echo 'Test Function executed successfully ('.$Pia_Archive_Name.')';
// chheck if archive exists
if (file_exists($Pia_Archive_Path.$Pia_Archive_Name) && filesize($Pia_Archive_Path.$Pia_Archive_Name) > 0) {
echo 'The backup executed successfully with the new archive: ('.$Pia_Archive_Name.')';
unlink($newfile);
} else {
echo 'Test Function executed successfully';
echo 'Test backup executed partially successfully. The archive could not be created or is empty. (pialert.db.latestbackup)';
}
// echo "Test Function executed successfully";
}
}
//------------------------------------------------------------------------------
// Restore DB from Archiv
//------------------------------------------------------------------------------
function PiaRestoreDBfromArchive() {
// prepare fast Backup
$file = '../../../db/pialert.db';
$oldfile = '../../../db/pialert.db.prerestore';
// copy files as a fast Backup
if (!copy($file, $oldfile)) {
echo "Test Function executed not successfully";
} else {
// extract latest archive and overwrite the actual pialert.db
$Pia_Archive_Path = '../../../db/';
exec('/bin/ls -Art '.$Pia_Archive_Path.'*.zip | /bin/tail -n 1 | /usr/bin/xargs -n1 /bin/unzip -o -d ../../../db/', $output);
// check if the pialert.db exists
if (file_exists($file)) {
echo 'Restore executed successfully';
unlink($oldfile);
} else {
echo 'Restore Failed. Please restore the backup manually.';
}
}
}
//------------------------------------------------------------------------------
// Query total numbers of Devices by status