Merge pull request #375 from wrongecho/backups

Add automatic database backup functionality
This commit is contained in:
Johnny 2022-02-24 15:42:48 -05:00 committed by GitHub
commit f416d25fc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 16 deletions

1
backups/.htaccess Normal file
View File

@ -0,0 +1 @@
Deny from all

0
backups/index.php Normal file
View File

View File

@ -44,7 +44,75 @@ while($row = mysqli_fetch_array($sql_companies)){
if($config_enable_cron == 1){ if($config_enable_cron == 1){
// GET Notifications // DATABASE BACKUP
$backup_dir = "backups/";
// Get All Table Names From the Database
$tables = array();
$sql = "SHOW TABLES";
$result = mysqli_query($mysqli, $sql);
while ($row = mysqli_fetch_row($result)) {
$tables[] = $row[0];
}
$sqlScript = "";
foreach ($tables as $table) {
// Prepare SQLscript for creating table structure
$query = "SHOW CREATE TABLE $table";
$result = mysqli_query($mysqli, $query);
$row = mysqli_fetch_row($result);
$sqlScript .= "\n\n" . $row[1] . ";\n\n";
$query = "SELECT * FROM $table";
$result = mysqli_query($mysqli, $query);
$columnCount = mysqli_num_fields($result);
// Prepare SQLscript for dumping data for each table
for ($i = 0; $i < $columnCount; $i ++) {
while ($row = mysqli_fetch_row($result)) {
$sqlScript .= "INSERT INTO $table VALUES(";
for ($j = 0; $j < $columnCount; $j ++) {
$row[$j] = $row[$j];
if (isset($row[$j])) {
$sqlScript .= '"' . $row[$j] . '"';
} else {
$sqlScript .= '""';
}
if ($j < ($columnCount - 1)) {
$sqlScript .= ',';
}
}
$sqlScript .= ");\n";
}
}
$sqlScript .= "\n";
}
// Save the SQL script to a backup file
if(!empty($sqlScript)) {
$random_string = key32gen();
if(!empty($random_string)){
$backup_file_name = date('Y-m-d') . '_backup__' . $random_string . '.sql';
$fileHandler = fopen($backup_dir . '/' .$backup_file_name, 'w+');
$number_of_lines = fwrite($fileHandler, $sqlScript);
fclose($fileHandler);
}
}
// Delete backups older than 30 days
$now = time();
foreach (glob($backup_dir."*.sql") as $file) {
if(time() - filectime($file) > 2592000){
unlink($file);
}
}
// GET NOTIFICATIONS
// DOMAINS EXPIRING // DOMAINS EXPIRING
@ -103,7 +171,7 @@ while($row = mysqli_fetch_array($sql_companies)){
$warranty_alert_array = [1,7,14,30,90,120]; $warranty_alert_array = [1,7,14,30,90,120];
foreach($$warranty_alert_array as $day){ foreach($warranty_alert_array as $day){
//Get Asset Warranty Expiring //Get Asset Warranty Expiring
$sql = mysqli_query($mysqli,"SELECT * FROM assets $sql = mysqli_query($mysqli,"SELECT * FROM assets
@ -255,8 +323,8 @@ while($row = mysqli_fetch_array($sql_companies)){
mysqli_query($mysqli,"INSERT INTO history SET history_date = CURDATE(), history_status = 'Sent', history_description = 'Cron Emailed Overdue Invoice', history_created_at = NOW(), history_invoice_id = $invoice_id, company_id = $company_id"); mysqli_query($mysqli,"INSERT INTO history SET history_date = CURDATE(), history_status = 'Sent', history_description = 'Cron Emailed Overdue Invoice', history_created_at = NOW(), history_invoice_id = $invoice_id, company_id = $company_id");
}catch (Exception $e) { }catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
mysqli_query($mysqli,"INSERT INTO history SET history_date = CURDATE(), history_status = 'Sent', history_description = 'Cron Failed to send Overdue Invoice', history_created_at = NOW(), history_invoice_id = $new_invoice_id, company_id = $company_id"); mysqli_query($mysqli,"INSERT INTO history SET history_date = CURDATE(), history_status = 'Sent', history_description = 'Cron Failed to send Overdue Invoice', history_created_at = NOW(), history_invoice_id = $new_invoice_id, company_id = $company_id");
} //End Mail Try } //End Mail Try
} }
@ -380,8 +448,8 @@ while($row = mysqli_fetch_array($sql_companies)){
mysqli_query($mysqli,"UPDATE invoices SET invoice_status = 'Sent', invoice_updated_at = NOW(), invoice_client_id = $client_id WHERE invoice_id = $new_invoice_id"); mysqli_query($mysqli,"UPDATE invoices SET invoice_status = 'Sent', invoice_updated_at = NOW(), invoice_client_id = $client_id WHERE invoice_id = $new_invoice_id");
}catch (Exception $e) { }catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
mysqli_query($mysqli,"INSERT INTO history SET history_date = CURDATE(), history_status = 'Draft', history_description = 'Cron Failed to send Invoice!', history_created_at = NOW(), history_invoice_id = $new_invoice_id, company_id = $company_id"); mysqli_query($mysqli,"INSERT INTO history SET history_date = CURDATE(), history_status = 'Draft', history_description = 'Cron Failed to send Invoice!', history_created_at = NOW(), history_invoice_id = $new_invoice_id, company_id = $company_id");
} //End Mail Try } //End Mail Try
} //End if Autosend is on } //End if Autosend is on
} //End Recurring Invoices Loop } //End Recurring Invoices Loop