PYBS (Personel Yönetim Bilgi Sistemi) / modules/yedek-al.php
yedek-al.php 68 satır • 1.96 KB
<?php
// modules/yedek-al.php
session_start();
require_once '../config/db.php';
require_once '../config/functions.php';

// Sadece bu roller yedek alabilir
yetkiKontrol(['root', 'yonetici', 'muhasebe']);

// Veritabanı Bilgileri (db.php'den gelen değişkenleri kullanıyoruz ama global yapıyoruz)
global $host, $dbname, $username, $password;

// YEDEKLEME İŞLEMİ
$tables = array();
$sql_show = "SHOW TABLES";
$result = $pdo->query($sql_show);

while ($row = $result->fetch(PDO::FETCH_NUM)) {
    $tables[] = $row[0];
}

$sqlScript = "";
foreach ($tables as $table) {
    // Tablo yapısını al
    $query = "SHOW CREATE TABLE $table";
    $result = $pdo->query($query);
    $row = $result->fetch(PDO::FETCH_NUM);
    
    $sqlScript .= "\n\n" . $row[1] . ";\n\n";
    
    // Verileri al
    $query = "SELECT * FROM $table";
    $result = $pdo->query($query);
    
    $columnCount = $result->columnCount();
    
    for ($i = 0; $i < $columnCount; $i ++) {
        while ($row = $result->fetch(PDO::FETCH_NUM)) {
            $sqlScript .= "INSERT INTO $table VALUES(";
            for ($j = 0; $j < $columnCount; $j ++) {
                $row[$j] = $row[$j];
                
                if (isset($row[$j])) {
                    $sqlScript .= '"' . addslashes($row[$j]) . '"';
                } else {
                    $sqlScript .= '""';
                }
                if ($j < ($columnCount - 1)) {
                    $sqlScript .= ',';
                }
            }
            $sqlScript .= ");\n";
        }
    }
    $sqlScript .= "\n"; 
}

// LOG KAYDI (İndirme başlamadan hemen önce)
logKaydet($pdo, $_SESSION['kullanici_id'], 'guvenlik_uyarisi', 'Veritabanı yedeği indirildi (Backup).');

// Dosyayı İndir
$backup_file_name = $dbname . '_backup_' . date("Y-m-d_H-i-s") . '.sql';
header('Content-Type: application/sql');
header('Content-Disposition: attachment; filename=' . $backup_file_name);
header('Content-Length: ' . strlen($sqlScript));
echo $sqlScript;
exit;
?>