Export/Backup MySQL Database like phpMyAdmin
Call the function with database name and a path to save backup( leave blank for current dir)
<?php
function db_bkf($dbname, $path=''){
ini_set('display_errors',1);
error_reporting(E_ALL);
echo 'Backuping database....';
$drop=0;
mysql_connect('localhost', 'root', '');
mysql_select_db("logs");
$hostname='localhost'; $username='root'; $password='';
$backupFile = $path.$dbname . date("Y-m-d-H-i-s") . '.sql';
$tab_status = mysql_query("SHOW TABLE STATUS");
while($all = mysql_fetch_assoc($tab_status)):
$tbl_stat[$all['Name']] = $all['Auto_increment'];
endwhile;
$backup='';
$tables = mysql_query("SHOW TABLES FROM $dbname");
echo ($tables=="")?"No db table found !":count($tables)." Tables";
while($tabs = mysql_fetch_row($tables)):
$droptbl=(($drop==1)?"DROP IF EXISTS TABLE `".$tabs[0]."`;":"");
$backup .= "--\n--Table structure for `$tabs[0]`\n--\n\n".$droptbl."\nCREATE TABLE IF NOT EXISTS `$tabs[0]` (";
$res = mysql_query("SHOW CREATE TABLE $tabs[0]");
while($all = mysql_fetch_assoc($res)):
$str = str_replace("CREATE TABLE `$tabs[0]` (", "", $all['Create Table']);
$str = str_replace(",", ",", $str);
$str2 = str_replace("`) ) TYPE=MyISAM ", "`)\n ) TYPE=MyISAM ", $str);
$backup .= $str2." AUTO_INCREMENT=".$tbl_stat[$tabs[0]].";\n\n";
endwhile;
$backup .= "--\n--Data to be executed for table `$tabs[0]`\n--\n\n";
$data = mysql_query("SELECT * FROM $tabs[0]");
while($dt = mysql_fetch_row($data)):
$backup .= "INSERT INTO `$tabs[0]` VALUES('$dt[0]'";
for($i=1; $i<sizeof($dt); $i++):
$backup .= ", '$dt[$i]'";
endfor;
$backup .= ");\n";
endwhile;
$backup .= "\n-- --------------------------------------------------------\n\n";
endwhile;
$fh=fopen($backupFile,'w') or die("Backup not done! file error");
fwrite($fh, $backup);
fclose($fh);
echo "<br />Backup Complete !";
}
?>