SQL bilgilerini forma gir backups dizinine dbyi yedek alır.
<?php
class DatabaseBackup {
private $host;
private $username;
private $password;
private $database;
private $conn;
public function __construct($host, $username, $password, $database) {
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
public function connect() {
try {
$this->conn = new mysqli($this->host, $this->username, $this->password, $this->database);
if ($this->conn->connect_error) {
throw new Exception("Bağlantı hatası: " . $this->conn->connect_error);
}
$this->conn->set_charset("utf8");
return true;
} catch (Exception $e) {
die("Hata: " . $e->getMessage());
}
}
public function backup() {
try {
$this->connect();
$tables = array();
$result = $this->conn->query("SHOW TABLES");
while ($row = $result->fetch_row()) {
$tables[] = $row[0];
}
$sqlScript = "";
// SQL başlangıç bilgileri
$sqlScript .= "-- Database Backup\n";
$sqlScript .= "-- Oluşturulma Tarihi: " . date("Y-m-d H:i:s") . "\n";
$sqlScript .= "-- Sunucu versiyonu: " . $this->conn->server_info . "\n";
$sqlScript .= "-- PHP Versiyonu: " . phpversion() . "\n\n";
$sqlScript .= "SET FOREIGN_KEY_CHECKS=0;\n";
$sqlScript .= "SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\";\n";
$sqlScript .= "SET AUTOCOMMIT = 0;\n";
$sqlScript .= "START TRANSACTION;\n";
$sqlScript .= "SET time_zone = \"+00:00\";\n\n";
foreach ($tables as $table) {
// Tabloyu temizleme komutu
$sqlScript .= "\nDROP TABLE IF EXISTS `" . $table . "`;\n";
// Tablo yapısını al
$query = "SHOW CREATE TABLE " . $table;
$result = $this->conn->query($query);
$row = $result->fetch_row();
$sqlScript .= $row[1] . ";\n\n";
// Tablo verilerini al
$query = "SELECT * FROM " . $table;
$result = $this->conn->query($query);
if ($result->num_rows > 0) {
$sqlScript .= "INSERT INTO `" . $table . "` VALUES";
$first_row = true;
while ($row = $result->fetch_assoc()) {
if (!$first_row) {
$sqlScript .= ",";
} else {
$first_row = false;
}
$sqlScript .= "\n(";
foreach ($row as $value) {
if ($value === null) {
$sqlScript .= "NULL,";
} else {
$sqlScript .= "'" . $this->conn->real_escape_string($value) . "',";
}
}
$sqlScript = rtrim($sqlScript, ",");
$sqlScript .= ")";
}
$sqlScript .= ";\n";
}
}
$sqlScript .= "\nSET FOREIGN_KEY_CHECKS=1;\n";
$sqlScript .= "COMMIT;\n";
if (!empty($sqlScript)) {
// Dosya adını oluştur
$backupFileName = $this->database . '_backup_' . date('Y-m-d_H-i-s') . '.sql';
// Dosyayı kaydet
$backup_folder = "backups/";
if (!file_exists($backup_folder)) {
mkdir($backup_folder, 0777, true);
}
$file_path = $backup_folder . $backupFileName;
file_put_contents($file_path, $sqlScript);
echo "<div style='background-color: #dff0d8; color: #3c763d; padding: 15px; margin: 10px 0; border-radius: 4px;'>";
echo "Yedekleme başarıyla tamamlandı!<br>";
echo "Dosya adı: " . $backupFileName . "<br>";
echo "Dosya yolu: " . $file_path;
echo "</div>";
}
} catch (Exception $e) {
die("Yedekleme hatası: " . $e->getMessage());
}
}
}
// Kullanım örneği:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$host = $_POST['host'];
$username = $_POST['username'];
$password = $_POST['password'];
$database = $_POST['database'];
$backup = new DatabaseBackup($host, $username, $password, $database);
$backup->backup();
}
?>
<!-- HTML Form -->
<!DOCTYPE html>
<html>
<head>
<title>Veritabanı Yedekleme</title>
<meta charset="utf-8">
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
.container { max-width: 500px; margin: 0 auto; }
.form-group { margin-bottom: 15px; }
label { display: block; margin-bottom: 5px; }
input[type="text"], input[type="password"] {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover { background-color: #45a049; }
</style>
</head>
<body>
<div class="container">
<h2>Veritabanı Yedekleme</h2>
<form method="POST">
<div class="form-group">
<label>Host:</label>
<input type="text" name="host" required>
</div>
<div class="form-group">
<label>Kullanıcı Adı:</label>
<input type="text" name="username" required>
</div>
<div class="form-group">
<label>Şifre:</label>
<input type="password" name="password" required>
</div>
<div class="form-group">
<label>Veritabanı Adı:</label>
<input type="text" name="database" required>
</div>
<button type="submit">Yedekle</button>
</form>
</div>
</body>
</html>