Código PHP:
<?php
$db_host = "localhost";
$db_name = "db";
$db_user = "user";
$db_pass = "pass";
mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name) or die("Unable to select database.");
function datadump ($table) {
$result .= "# Dump of ".$table." \n";
$result .= "# Dump DATE : " . date("d-M-Y") ."\n\n";
$query = mysql_query("select * from ".$table);
$num_fields = @mysql_num_fields($query);
$numrow = mysql_num_rows($query);
while($row=mysql_fetch_array($query)) {
$result .= "INSERT INTO ".$table." VALUES(";
for($j=0; $j<$num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) $result .= "\"$row[$j]\"" ; else $result .= "\"\"";
if ($j<($num_fields-1)) $result .= ",";
}
$result .= ");\n";
}
return $result . "\n\n\n";
}
$qry2=mysql_list_tables($db_name);
$tot_tablas=mysql_num_rows($qry2);
for($i=0;$i<$tot_tablas;$i++){
$nombre_tabla=mysql_tablename($qry2,$i);
$content.=datadump($nombre_tabla);
}
$file_name = "MySQL_Database_Backup.sql";
header( "Content-Type: application/octet-stream");
header( "Content-Disposition: attachment; filename=".$file_name."");
echo $content;
?>