<?php
if (!isset($_SESSION)) { session_start(); }

// variables
$dbhost = $_SESSION['servidor'];
$dbname = $_SESSION['nombre_bd'];
$dbuser = $_SESSION['usuario'];
$dbpass = $_SESSION['password'];

$backup_file = $dbname.".sql";
  
// comandos a ejecutar
$commands = array("mysqldump --opt -h $dbhost -u $dbuser -p$dbpass -v $dbname > $backup_file","bzip2 $backup_file");
 
// ejecuci&oacute;n y salida de &eacute;xito o errores
foreach ( $commands as $command ) {
        system($command,$output);
        echo $output;
}

//----- DESCARGAR ARCHIVO -------
 
$fileName = basename($backup_file.'.bz2');
$filePath = $fileName;

if(!empty($fileName) && file_exists($filePath)){
    // Define headers
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$fileName");
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: binary");
    
    // Read the file
    
    ob_clean();
    flush();
    
    readfile($filePath);


   //--- BORRAR ARCHIVO ----

    unlink( $fileName );          
    exit;

}      
?>