And one more: generation hungs at a large table (containing a log), but I think this is no matter 'cause information written row by row.
Actually this is the cause: I should use mysql_unbuffered_query
instead mysql_query
. Now this function looks like this:
function backup_tables($backup_filename, $tables = '*')
{
$conf = new JConfig();
$dbhost = $conf->host;
$dbuser = $conf->user;
$dbpassword = $conf->password;
$dbname = $conf->db;
$link = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbname, $link) or die(mysql_error());
$return = "drop database if exists `$dbname`;\n\ncreate database `$dbname`;\n\nuse `$dbname`;\n\n";
$return .= "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n\n";
$return .= "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n\n";
$return .= "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n\n";
$return .= "/*!40101 SET NAMES utf8 */;\n\n";
$handle = fopen($backup_filename, 'w+');
fwrite($handle, $return); $return = "";
// get all of the tables
if ($tables == '*') {
$tables = array();
$result = mysql_query("SHOW TABLES");
while ($row = mysql_fetch_row($result)) {
$tables[] = $row[0];
}
} else {
$tables = is_array($tables) ? $tables : explode(',', $tables);
}
// cycle through
foreach ($tables as $table) {
$return .= "DROP TABLE IF EXISTS `$table`;";
$return .= "\n\n" . mysql_fetch_row(mysql_query("SHOW CREATE TABLE `$table`;"))[1] . " DEFAULT CHARSET=cp1251;\n\n";
$result = mysql_unbuffered_query("SELECT * FROM `$table`");
$num_fields = mysql_num_fields($result);
while ($row = mysql_fetch_row($result)) {
$return .= "INSERT INTO `$table` VALUES(";
for ($i = 0; $i < $num_fields; $i++) {
$row[$i] = str_replace("\n", "\\n", addslashes($row[$i]));
$return .= '"' . (isset($row[$i])? $row[$i] : '') . '"';
if ($num_fields - $i - 1) {
$return .= ',';
}
}
$return .= ");\n";
fwrite($handle, $return); $return = "";
}
if($return)
fwrite($handle, $return);
$return = "\n\n\n";
}
fclose($handle);
}