<?php
include 'db.php'; // must define $host, $username, $password, $dbname

$filename = "backup_" . date('Y-m-d_H-i-s') . ".sql";

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Pragma: no-cache");
header("Expires: 0");

$command = "mysqldump --user=$username --password=$password --host=$host $dbname 2>&1";

passthru($command, $return_var);

if ($return_var !== 0) {
    echo "Error: Backup failed. Please check server permissions or credentials.";
}
exit;
