<?php
session_start();
if (!isset($_SESSION['username'])) {
    echo "<script>window.top.location.href='index.php';</script>"; // Redirect parent window to login page
    exit();
}

if (isset($_POST['backup'])) {
    // Database connection details
    $db_host = 'localhost';
    $db_user = 'root';
    $db_password = ''; // No password
    $db_name = 'prodb';

    // Define the path to the backup file
    $backup_file = 'backup_' . $db_name . '_' . date('Y-m-d_H-i-s') . '.sql';

    // mysqldump command
    $command = "mysqldump --host=$db_host --user=$db_user --password=$db_password $db_name > $backup_file";

    // Execute the command and check if it was successful
    $output = [];
    $result_code = 0;
    exec($command, $output, $result_code);

    if ($result_code === 0) {
        echo "<p>Backup created successfully: $backup_file</p>";
    } else {
        echo "<p>Backup failed. Please check your configuration and permissions.</p>";
    }
}
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Database Backup</title>
</head>
<body>

    <h2>Create Local Backup of Database</h2>

    <form action="" method="POST">
        <input type="submit" name="backup" value="Create Backup">
    </form>

</body>
</html>
