maintenance task DB

This commit is contained in:
jokob-sk
2022-01-14 14:48:42 +11:00
parent 25dae82cba
commit 0120d3f226
3 changed files with 126 additions and 28 deletions

23
back/maintenance.py Normal file
View File

@@ -0,0 +1,23 @@
import sys
import shutil
def main ():
argument = str(sys.argv[1])
## Main Commands
if argument == 'backup_db':
res = backup_db()
if argument == 'restore_db':
res = restore_db()
def backup_db ():
# Header
shutil.copyfile("/home/pi/pialert/db/pialert.db", "/home/pi/pialert/config/pialert.db_bak")
def restore_db ():
# Header
shutil.copyfile("/home/pi/pialert/config/pialert.db_bak", "/home/pi/pialert/db/pialert.db")

View File

@@ -36,28 +36,28 @@
<button type="button" class="btn btn-default pa-btn pa-btn-delete" style="margin-left:0px;" <button type="button" class="btn btn-default pa-btn pa-btn-delete" style="margin-left:0px;"
id="btnDeleteMAC" onclick="askDeleteDevicesWithEmptyMACs()"> Delete Devices with empty MACs </button> id="btnDeleteMAC" onclick="askDeleteDevicesWithEmptyMACs()"> Delete Devices with empty MACs </button>
</div> </div>
</div>
<div class="col-xs-12">
<div class="pull-right"> <div class="pull-right">
<button type="button" class="btn btn-default pa-btn pa-btn-delete" style="margin-left:0px;" <button type="button" class="btn btn-default pa-btn pa-btn-create" style="margin-left:0px;"
id="btnDelete" onclick="askDeleteAllDevices()"> Delete All Devices</button> id="btnDelete" onclick="askRunScan1min()"> Run 1 min scan now</button>
</div>
<div class="pull-right">
<button type="button" class="btn btn-default pa-btn pa-btn-create" style="margin-left:0px;"
id="btnDelete" onclick="askRunScan15min()"> Run 15 min scan now</button>
</div> </div>
</div>
<!-- <div class="col-xs-12">
<div class="pull-right"> <div class="pull-right">
<button type="button" class="btn btn-default pa-btn pa-btn-create" style="margin-left:0px;" <button type="button" class="btn btn-default pa-btn pa-btn-create" style="margin-left:0px;"
id="btnBackup" onclick="askCreateBackupDB()"> Backup DB </button> id="btnBackup" onclick="askCreateBackupDB()"> Backup DB </button>
</div> </div>
</div>
<div class="col-xs-12">
<div class="pull-right"> <div class="pull-right">
<button type="button" class="btn btn-default pa-btn pa-btn-delete" style="margin-left:0px;" <button type="button" class="btn btn-default pa-btn pa-btn-delete" style="margin-left:0px;"
id="btnRestore" onclick="askRestoreBackupDB()"> Restore DB </button> id="btnRestore" onclick="askRestoreBackupDB()"> Restore DB </button>
</div> </div>
</div> -->
</div>
<!-- ----------------------------------------------------------------------- --> <!-- ----------------------------------------------------------------------- -->
</section> </section>
@@ -73,6 +73,9 @@
<script> <script>
// delete devices with emty macs
function askDeleteDevicesWithEmptyMACs () { function askDeleteDevicesWithEmptyMACs () {
// Ask delete device // Ask delete device
@@ -89,7 +92,7 @@ function deleteDevicesWithEmptyMACs()
}); });
} }
// delete all devices
function askDeleteAllDevices () { function askDeleteAllDevices () {
// Ask delete device // Ask delete device
@@ -107,6 +110,39 @@ function deleteAllDevices()
} }
// Run ad-hoc scans
function askRunScan1min () {
// Ask delete device
showModalWarning('Scan 1 min now', 'This runs the 1 min scan sequence',
'Cancel', 'Scan', 'runScan1min');
}
function runScan1min()
{
// Scan
$.get('php/server/devices.php?action=runScan1min', function(msg) {
showMessage (msg);
});
}
function askRunScan15min () {
// Ask delete device
showModalWarning('Scan 15 min now', 'This runs the 15 min scan sequence',
'Cancel', 'Scan', 'runScan15min');
}
function runScan15min()
{
// Scan
$.get('php/server/devices.php?action=runScan15min', function(msg) {
showMessage (msg);
});
}
// DB backup // DB backup

View File

@@ -35,6 +35,8 @@
case 'createBackupDB': createBackupDB(); break; case 'createBackupDB': createBackupDB(); break;
case 'restoreBackupDB': restoreBackupDB(); break; case 'restoreBackupDB': restoreBackupDB(); break;
case 'deleteAllDevices': deleteAllDevices(); break; case 'deleteAllDevices': deleteAllDevices(); break;
case 'runScan15min': runScan15min(); break;
case 'runScan1min': runScan1min(); break;
@@ -217,20 +219,51 @@ function deleteAllDevices() {
} }
} }
//------------------------------------------------------------------------------
// Run scan 1 min now
//------------------------------------------------------------------------------
function runScan1min() {
$command = escapeshellcmd('/home/pi/pialert/back/pialert.py 1');
$output = shell_exec($command);
echo $output;
}
//------------------------------------------------------------------------------
// Run scan 15 min now
//------------------------------------------------------------------------------
function runScan15min() {
$command = escapeshellcmd('/home/pi/pialert/back/pialert.py 15');
$output = shell_exec($command);
echo $output;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Create a DB backup // Create a DB backup
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
function createBackupDB() { function createBackupDB() {
// // //
$result = copy("/home/pi/pialert/db/pialert.db","/home/pi/pialert/config/pialert.db_bak"); // $result = copy("/home/pi/pialert/db/pialert.db","/home/pi/pialert/config/pialert.db_bak");
// check result // // check result
if ($result == TRUE) { // if ($result == TRUE) {
echo "Copied successfully"; // echo "Copied successfully";
} else { // } else {
echo "Error copying DB. SSH into instance and copy manually."; // echo "Error copying DB. SSH into instance and copy manually.";
} // }
$command = escapeshellcmd('/home/pi/pialert/back/maintenance.py backup_DB');
$output = shell_exec($command);
echo $output;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -238,14 +271,20 @@ function createBackupDB() {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
function restoreBackupDB() { function restoreBackupDB() {
// // //
$result = copy("/home/pi/pialert/config/pialert.db_bak", "/home/pi/pialert/db/pialert.db"); // $result = copy("/home/pi/pialert/config/pialert.db_bak", "/home/pi/pialert/db/pialert.db");
// check result // // check result
if ($result == TRUE) { // if ($result == TRUE) {
echo "Copied successfully"; // echo "Copied successfully";
} else { // } else {
echo "Error copying DB. SSH into instance and copy manually."; // echo "Error copying DB. SSH into instance and copy manually.";
} // }
$command = escapeshellcmd('/home/pi/pialert/back/maintenance.py restore_DB');
$output = shell_exec($command);
echo $output;
} }