Merge pull request #375 from wrongecho/backups
Add automatic database backup functionality
This commit is contained in:
1
backups/.htaccess
Normal file
1
backups/.htaccess
Normal file
@@ -0,0 +1 @@
|
||||
Deny from all
|
||||
0
backups/index.php
Normal file
0
backups/index.php
Normal file
80
cron.php
80
cron.php
@@ -44,7 +44,75 @@ while($row = mysqli_fetch_array($sql_companies)){
|
||||
|
||||
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
|
||||
|
||||
@@ -103,7 +171,7 @@ while($row = mysqli_fetch_array($sql_companies)){
|
||||
|
||||
$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
|
||||
$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");
|
||||
|
||||
}catch (Exception $e) {
|
||||
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");
|
||||
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");
|
||||
} //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");
|
||||
|
||||
}catch (Exception $e) {
|
||||
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");
|
||||
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");
|
||||
} //End Mail Try
|
||||
} //End if Autosend is on
|
||||
} //End Recurring Invoices Loop
|
||||
|
||||
Reference in New Issue
Block a user