mirror of
https://github.com/itflow-org/itflow
synced 2026-03-22 05:25:39 +00:00
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
72
cron.php
72
cron.php
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user