<?php
session_start();

$backup_pass = "sdaesi2025";  // Change this to your desired password

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!isset($_POST['password']) || $_POST['password'] !== $backup_pass) {
        $_SESSION['error'] = "Incorrect backup password!";
        header("Location: dashboard.php");
        exit;
    }

    $backup_file = "backup_" . date("Y-m-d_H-i-s") . ".sql";
    $db = "school_checkin";
    $user = "root";
    $pass = "";  // leave empty if no MySQL password
    $mysqldump = "C:\\xampp\\mysql\\bin\\mysqldump.exe";

    $command = "\"$mysqldump\" -u $user $db";

    $output = shell_exec($command);

    if ($output) {
        file_put_contents($backup_file, $output);
        header('Content-Type: application/sql');
        header('Content-Disposition: attachment; filename="' . $backup_file . '"');
        readfile($backup_file);
        unlink($backup_file);
        exit;
    } else {
        $_SESSION['error'] = "Backup failed. Please check mysqldump path.";
        header("Location: dashboard.php");
        exit;
    }
} else {
    header("Location: dashboard.php");
    exit;
}
