├── README.md ├── backup.php ├── function.php └── index.php /README.md: -------------------------------------------------------------------------------- 1 | # Backup-MySQL-database-using-PHP 2 | 3 | Using PHP programming to create a database backup is simple. We'll learn how to backup a MySQL database using simple core PHP code. 4 | To get the table schema and data to be dumped into a file, I first get the database connection object. I get a list of all the table names from the 5 | database and put them in an array. The SQL script for the data/structure is then prepared by iterating through the array of database tables. 6 | This SQL script will be saved as a file and downloaded to the user's browser before being deleted from the target. 7 | -------------------------------------------------------------------------------- /backup.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /function.php: -------------------------------------------------------------------------------- 1 | connect_error) { 7 | die("Connection failed: " . $conn->connect_error); 8 | } 9 | 10 | //get all of the tables 11 | if($tables == '*'){ 12 | $tables = array(); 13 | $sql = "SHOW TABLES"; 14 | $query = $conn->query($sql); 15 | while($row = $query->fetch_row()){ 16 | $tables[] = $row[0]; 17 | } 18 | } 19 | else{ 20 | $tables = is_array($tables) ? $tables : explode(',',$tables); 21 | } 22 | 23 | //getting table structures 24 | $outsql = ''; 25 | foreach ($tables as $table) { 26 | 27 | // Prepare SQLscript for creating table structure 28 | $sql = "SHOW CREATE TABLE $table"; 29 | $query = $conn->query($sql); 30 | $row = $query->fetch_row(); 31 | 32 | $outsql .= "\n\n" . $row[1] . ";\n\n"; 33 | 34 | $sql = "SELECT * FROM $table"; 35 | $query = $conn->query($sql); 36 | 37 | $columnCount = $query->field_count; 38 | 39 | // Prepare SQLscript for dumping data for each table 40 | for ($i = 0; $i < $columnCount; $i ++) { 41 | while ($row = $query->fetch_row()) { 42 | $outsql .= "INSERT INTO $table VALUES("; 43 | for ($j = 0; $j < $columnCount; $j ++) { 44 | $row[$j] = $row[$j]; 45 | 46 | if (isset($row[$j])) { 47 | $outsql .= '"' . $row[$j] . '"'; 48 | } else { 49 | $outsql .= '""'; 50 | } 51 | if ($j < ($columnCount - 1)) { 52 | $outsql .= ','; 53 | } 54 | } 55 | $outsql .= ");\n"; 56 | } 57 | } 58 | 59 | $outsql .= "\n"; 60 | } 61 | 62 | // Save the SQL script to a backup file 63 | $backup_file_name = $dbname . '_backup.sql'; 64 | $fileHandler = fopen($backup_file_name, 'w+'); 65 | fwrite($fileHandler, $outsql); 66 | fclose($fileHandler); 67 | 68 | // Download the SQL backup file to the browser 69 | header('Content-Description: File Transfer'); 70 | header('Content-Type: application/octet-stream'); 71 | header('Content-Disposition: attachment; filename=' . basename($backup_file_name)); 72 | header('Content-Transfer-Encoding: binary'); 73 | header('Expires: 0'); 74 | header('Cache-Control: must-revalidate'); 75 | header('Pragma: public'); 76 | header('Content-Length: ' . filesize($backup_file_name)); 77 | ob_clean(); 78 | flush(); 79 | readfile($backup_file_name); 80 | exec('rm ' . $backup_file_name); 81 | 82 | } 83 | 84 | ?> 85 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Backup MySQL Database using PHP 9 | 10 | 11 |
12 |

Backup MySQL Database using PHP

13 |
14 |
15 |
16 |
17 |
18 |

Fill Database Credential

19 |
20 |
21 |
22 | 23 |
24 |
25 |
26 |
27 |
28 | 29 |
30 |
31 |
32 |
33 |
34 | 35 |
36 |
37 |
38 |
39 |
40 | 41 |
42 |
43 |
44 |
45 | 46 |
47 |
48 |
49 |
50 |
51 |
52 | 53 | 54 | --------------------------------------------------------------------------------