<?php
session_start();

$hostName = 'localhost';
$userName = 'satyahak_krunal';
$userPass = 'Krunal@7997';
$dbName   = 'satyahak_khakhi';

// Check login
if (!isset($_SESSION['user'])) {
    echo '<script>window.location.href = "index.php";</script>';
    exit();
}

// Backup file
$backupFile = "backup_" . $dbName . "_" . date("Y-m-d_H-i-s") . ".sql";

// Correct mysqldump command
$command = "mysqldump -h {$hostName} -u {$userName} -p'{$userPass}' {$dbName} > {$backupFile}";

// Run dump
system($command);

// Download file
if (file_exists($backupFile)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . basename($backupFile) . '"');
    header('Content-Length: ' . filesize($backupFile));
    readfile($backupFile);
    unlink($backupFile);
    exit;
} else {
    echo "<h3 style='color:red;'>❌ Backup failed. Check mysqldump path or database credentials.</h3>";
}
?>
