Created
October 18, 2022 07:33
-
-
Save denihida1216/3ce841e755285fd68e67a15af51b539c to your computer and use it in GitHub Desktop.
backup php to file .sql and zip file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('max_execution_time', '0'); | |
ini_set('memory_limit', '-1'); | |
$backupdir = "backup/";//folder backup file | |
$host = "";//host / ip database | |
$root = "";//user database | |
$pass = "";//password database | |
$db_name = "";//name database | |
$set_name = "utf8mb4"; | |
$mysqli = new mysqli($host, $root, $pass, $db_name); | |
$mysqli->select_db($db_name); | |
$mysqli->query("SET NAMES '" . $set_name . "'"); | |
//get table list | |
$queryTables = $mysqli->query("SHOW FULL TABLES WHERE Table_Type = 'BASE TABLE';"); | |
while ($row = $queryTables->fetch_row()) { | |
$target_tables[] = $row[0]; | |
} | |
//get table structure | |
foreach ($target_tables as $table) { | |
$result = $mysqli->query("SELECT * FROM " . $table); | |
$fields_amount = $result->field_count; | |
$rows_num = $mysqli->affected_rows; | |
$res = $mysqli->query("SHOW CREATE TABLE " . $table); | |
$TableMLine = $res->fetch_row(); | |
$content = (!isset($content) ? '' : $content) . "\n\nDROP TABLE IF EXISTS `" . $table . "`;\n" . $TableMLine[1] . ";\n\n"; | |
for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter = 0) { | |
while ($row = $result->fetch_row()) { //when started (and every after 100 command cycle): | |
if ($st_counter % 100 == 0 || $st_counter == 0) { | |
$content .= "\nINSERT INTO " . $table . " VALUES"; | |
} | |
$content .= "\n("; | |
for ($j = 0; $j < $fields_amount; $j++) { | |
$row[$j] = str_replace(array("\r\n\r\n", "\n\r\n", "\r\n", "\n\n", "\n"), array("\\r\\n", "\\r\\n", "\\r\\n", "\\r\\n", "\\r\\n"), addslashes($row[$j])); | |
if (isset($row[$j])) { | |
$content .= '"' . $row[$j] . '"'; | |
} else { | |
$content .= '""'; | |
} | |
if ($j < ($fields_amount - 1)) { | |
$content .= ','; | |
} | |
} | |
$content .= ")"; | |
//every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler | |
if ((($st_counter + 1) % 100 == 0 && $st_counter != 0) || $st_counter + 1 == $rows_num) { | |
$content .= ";"; | |
} else { | |
$content .= ","; | |
} | |
$st_counter = $st_counter + 1; | |
} | |
} | |
$content .= "\n\n"; | |
} | |
//get view list | |
$queryViews = $mysqli->query("SHOW FULL TABLES WHERE Table_Type = 'VIEW';"); | |
while ($row = $queryViews->fetch_row()) { | |
$target_views[] = $row[0]; | |
} | |
//get view structure | |
foreach ($target_views as $view) { | |
$selectViews = $mysqli->query("SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = '" . $view . "';"); | |
while ($row = $selectViews->fetch_row()) { | |
$content .= "DROP VIEW IF EXISTS `" . $view . "`;" . "\n"; | |
$select_data = str_replace("`" . $db_name . "`.", '', $row[0]); | |
$content .= "CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `" . $view . "` AS " . $select_data . ";" . "\n\n"; | |
} | |
} | |
// save as .sql file | |
//give additional description | |
$content_ = "\n-- Database Backup --\n"; | |
$content_ .= "-- Ver. : 1.0.2\n"; | |
$content_ .= "-- Host : 127.0.0.1\n"; | |
$content_ .= "-- Generating Time : " . date("M d") . ", " . date("Y") . " at " . date("H:i:s:") . date("A") . "\n\n"; | |
$content_ .= "\n" . "SET NAMES " . $set_name . ";"; | |
$content_ .= "\n" . "SET FOREIGN_KEY_CHECKS = 0;"; | |
$content_ .= $content; | |
$content_ .= "\n\n" . "SET FOREIGN_KEY_CHECKS = 1;"; | |
$content_ .= "\n"; | |
//save the file | |
$filename = $db_name . " " . date("Y-m-d H-i-s") . ".sql"; | |
$backup_file_name = $backupdir . $filename; | |
$fp = fopen($backup_file_name, 'w+'); | |
$result = fwrite($fp, $content_); | |
fclose($fp); | |
$zip = new ZipArchive; | |
if ($zip->open($backup_file_name . '.zip', ZipArchive::CREATE) === TRUE) { | |
// Add file to the zip file | |
$zip->addFile($backup_file_name, $filename); | |
$zip->close(); | |
unlink($backup_file_name); | |
} | |
echo json_encode([ | |
"metadata" => [ | |
"code" => 200, | |
"message" => "OK", | |
] | |
]); | |
// //download file directly from browser | |
// $file_path = $backup_file_name; | |
// if (!empty($file_path) && file_exists($file_path)) { | |
// header("Pragma:public"); | |
// header("Expired:0"); | |
// header("Cache-Control:must-revalidate"); | |
// header("Content-Control:public"); | |
// header("Content-Description: File Transfer"); | |
// header("Content-Type: application/octet-stream"); | |
// header("Content-Disposition:attachment; filename=\"" . basename($file_path) . "\""); | |
// header("Content-Transfer-Encoding:binary"); | |
// header("Content-Length:" . filesize($file_path)); | |
// flush(); | |
// readfile($file_path); | |
// exit(); | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment