所以这对我来说是新的.当使用David Walsh的这个MySQL数据库PHP脚本时,我在运行它时得到一个空的SQL文件.
<?php
backup_tables('localhost','username','password','blog');
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$return = '';
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//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)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= '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])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//save file
$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
mysql_close($link);
}
?>
Run Code Online (Sandbox Code Playgroud)
当我理解代码时,我输入了DB名称,其中blog backup_tables()位于顶部的函数中.
为什么脚本会创建一个空的SQL文件?
我尝试了这个脚本,它对我有用,尽管有一些错误和一些折旧警告。
为了保存文件,请确保您的脚本所在的目录具有写入权限。
chmod 0777 /home/site/dir
Run Code Online (Sandbox Code Playgroud)
为了防止Undefined variable: return通知,添加以下代码
$return = '';
Run Code Online (Sandbox Code Playgroud)
就在这条线之前
$link = mysql_connect($host,$user,$pass);
Run Code Online (Sandbox Code Playgroud)
而且 - 我知道这看起来很明显 - 但请确保您连接到正确的数据库,或者您的数据库名称没有拼写错误。
backup_tables('localhost','myUsername','myPassword','myDatabase');
Run Code Online (Sandbox Code Playgroud)
最后:确保数据库有表。我在数据库中没有表的情况下运行它以查看发生了什么 - 它吐出一个空白文档。就像你描述的那样。:)
我从一开始就关注你的问题,我做了研究,发现你的问题很有趣。
大多数通过php代码备份mysql的解决方案已经过时,并且在当今的环境中存在问题。因此我很好奇是否有任何更新和有用的曾经。我的计划是,如果我找不到一个,那么我想开发一个。
但我找到了一个解决方案并且有效。我通过导出和重新导入表对其进行了测试。我想你会喜欢它。
在阅读代码之前,我有以下几点说明:
注意:代码可以在代码所有者的页面上找到,我添加了额外的代码行只是为了使其适用于 OP。
非常重要的注意事项: 根据您的服务器资源和容量以及数据库大小,执行此类代码可能需要时间,特别是如果您有共享主机,通常情况下,因此您需要将执行时间延长至 300 秒(5 分钟)并且应在低负载时段进行备份。最坏的情况是,如果该部分被代码阻止,您可能需要要求主机提供商延长执行时间。
工作代码
<?php
define("MAX_EXECUTION_TIME", 100); // seconds
$timeline = time() + MAX_EXECUTION_TIME;
EXPORT_TABLES('localhost', 'root', '', 'blog');
function EXPORT_TABLES($host, $user, $pass, $name, $tables = false, $backup_name = false)
{
$mysqli = new mysqli($host, $user, $pass, $name);
$mysqli->select_db($name);
$mysqli->query("SET NAMES 'utf8'");
$queryTables = $mysqli->query('SHOW TABLES');
while ($row = $queryTables->fetch_row())
{
$target_tables[] = $row[0];
}
if ($tables !== false)
{
$target_tables = array_intersect($target_tables, $tables);
}
try
{
foreach ($target_tables as $table)
{
$result = $mysqli->query('SELECT * FROM ' . $table);
$fields_amount = $result->field_count;
$rows_num = $mysqli->affected_rows;
$res = $mysqli->query('SHOW CREATE TABLE ' . $table);
$TableMLine = $res->fetch_row();
$content = (!isset($content) ? '' : $content) . "\n\n" . $TableMLine[1] . ";\n\n";
for ($i = 0, $st_counter = 0; $i < $fields_amount; $i ++, $st_counter = 0)
{
while ($row = $result->fetch_row())
{ //when started (and every after 100 command cycle):
if ($st_counter % 100 == 0 || $st_counter == 0)
{
$content .= "\nINSERT INTO " . $table . " VALUES";
}
$content .= "\n(";
for ($j = 0; $j < $fields_amount; $j ++)
{
$row[$j] = str_replace("\n", "\\n", addslashes($row[$j]));
if (isset($row[$j]))
{
$content .= '"' . $row[$j] . '"';
} else
{
$content .= '""';
}
if ($j < ($fields_amount - 1))
{
$content .= ',';
}
}
$content .= ")";
//every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
if ((($st_counter + 1) % 100 == 0 && $st_counter != 0) || $st_counter + 1 == $rows_num)
{
$content .= ";";
} else
{
$content .= ",";
}
$st_counter = $st_counter + 1;
}
}
$content .= "\n\n\n";
}
} catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
$backup_name = $backup_name ? $backup_name : $name . "___(" . date('H-i-s') . "_" . date('d-m-Y') . ")__rand" . rand(1, 11111111) . ".sql";
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . $backup_name . "\"");
echo $content;
exit;
}
?>
Run Code Online (Sandbox Code Playgroud)