<?php
// MySQL database configuration
$host = 'localhost';
$username = 'dzvisual_admin';
$password = 'Farhan@123456';
$database = 'dzvisual_homestay';

// Backup destination directory
$backupDir = './backup/';

// Generate a unique backup filename
$backupFile = $backupDir . 'backup_' . date('Y-m-d_H-i-s') . '.sql';

// Construct the command to perform MySQL backup
$command = 'mysqldump --host=' . $host . ' --user=' . $username . ' --password=' . $password . ' ' . $database . ' > ' . $backupFile;

// Open a pipe to the command
$pipe = popen($command, 'r');

// Read the output of the command
$output = stream_get_contents($pipe);
$returnVar = pclose($pipe);

if ($output === false || $returnVar !== 0) {
    // Backup failed
    echo 'MySQL backup failed.';
} else {
    // Create ZIP archive
    $zipFile = $backupDir . 'backup_' . date('Y-m-d_H-i-s') . '.zip';
    $zip = new ZipArchive();
    $zip->open($zipFile, ZipArchive::CREATE);
    $zip->addFile($backupFile, 'backup.sql');
    $zip->close();

    // Set appropriate headers for ZIP file download
    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename="backup.zip"');
    header('Content-Length: ' . filesize($zipFile));

    // Read and output the ZIP file
    readfile($zipFile);

    // Clean up temporary files
    unlink($backupFile);
    unlink($zipFile);
}
?>