Viewing file: backup.php (3.37 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php // Database config $host = "localhost"; $user = "sql_idfdtce_co_in"; // change to your DB username $pass = "02a26fdfdb2e4265"; // change to your DB password $db = "sql_itcdfde_co_in"; // change to your DB name
// ======================== // Connect to database // ======================== $conn = new mysqli($host, $user, $pass, $db); if ($conn->connect_error) { die("❌ Connection failed: " . $conn->connect_error); }
// ======================== // Fetch all tables // ======================== $tables = []; $result = $conn->query("SHOW TABLES"); while ($row = $result->fetch_row()) { $tables[] = $row[0]; }
$sqlScript = "";
// ======================== // Loop through tables // ======================== foreach ($tables as $table) { // Drop table statement $sqlScript .= "DROP TABLE IF EXISTS `$table`;\n";
// Table structure $res = $conn->query("SHOW CREATE TABLE `$table`"); $row = $res->fetch_row(); $sqlScript .= $row[1] . ";\n\n";
// Table data $resData = $conn->query("SELECT * FROM `$table`"); $columnCount = $resData->field_count;
while ($rowData = $resData->fetch_row()) { $sqlScript .= "INSERT INTO `$table` VALUES("; for ($i = 0; $i < $columnCount; $i++) { $rowData[$i] = isset($rowData[$i]) ? $conn->real_escape_string($rowData[$i]) : ''; $sqlScript .= '"' . $rowData[$i] . '"'; if ($i < ($columnCount - 1)) { $sqlScript .= ','; } } $sqlScript .= ");\n"; }
$sqlScript .= "\n"; }
// ======================== // Close connection // ======================== $conn->close();
// ======================== // Save SQL file locally // ======================== if (!empty($sqlScript)) { $backup_date = date("Y-m-d H:i:s"); $backup_file_name = $db . "_backup_" . date("Y-m-d_H-i-s") . ".sql"; $backup_dir = "backups";
if (!file_exists($backup_dir)) { mkdir($backup_dir, 0777, true); }
file_put_contents($backup_dir . "/" . $backup_file_name, $sqlScript); } else { die("⚠️ No data found in database!"); } ?>
<!DOCTYPE html> <html> <head> <title>Database Backup List</title> <style> body { font-family: Arial, sans-serif; background: #f7f7f7; } table { border-collapse: collapse; width: 80%; margin: 20px auto; background: #fff; } th, td { border: 1px solid #ccc; padding: 10px; text-align: center; } th { background: #007BFF; color: #fff; } tr:nth-child(even) { background: #f2f2f2; } h2 { text-align: center; margin-top: 20px; } a { text-decoration: none; color: #007BFF; font-weight: bold; } </style> </head> <body> <h2>📂 Database Backup List</h2> <table> <tr> <th>Website / DB Name</th> <th>Backup Date</th> <th>Backup File Name</th> <th>Download</th> </tr> <?php $files = glob($backup_dir . "/*.sql"); rsort($files); // latest first foreach ($files as $file) { $filename = basename($file); $filedate = date("Y-m-d H:i:s", filemtime($file)); echo "<tr> <td>$db</td> <td>$filedate</td> <td>$filename</td> <td><a href='$file' download>⬇ Download</a></td> </tr>"; } ?> </table> </body> </html>
|