PHP MySql 备份不工作 - 没有显示错误
PHP MySql Backup not working - no errors showing
有人要求我在旧 vps 服务器上设置计划备份。该网站在经典 ASP 上运行,我一直在尝试使用 ASP 查找要备份的代码,但看不到简单的代码。
我现在尝试使用我在 PHP 网站上使用的 PHP 代码进行备份,它不会显示错误或创建备份文件。 PHP运行网站上的版本是5.2.17
这是我试过的 PHP 代码。
$fileNameBackup = 'daily-db-backup-'.date( 'Y-m-d' ).'.sql.gz';
$return_var = NULL;
$output = NULL;
$uniqueFilename = uniqid();
$command = "mysqldump -u ".$user." -h ".$host." -p ".$password." ".$database." | gzip > ".$fileNameBackup;
exec($command, $output, $return_var);
我从这里去哪里?如果有错误,我会反对但我没有想法,有人可以帮助我吗?
尝试将 mysqldump 命令重写为:
$command = "mysqldump -u ".$user." -h ".$host." -p".$password." ".$database." | gzip > ".$fileNameBackup;
注意我删除了 -p 后的 space,因为它应该是连续的。
来自 docs -
The password to use when connecting to the server. If you use the short option form (-p
), you cannot have a space between the option and the password. If you omit the password value following the --password or -p option on the command line, mysql prompts for one.
<?php
set_time_limit (0);
!defined('DB_HOST_NAME') ? define('DB_HOST_NAME',"localhost") : '' ;
!defined('DATABASE') ? define('DATABASE',"yourdb") : '' ;
!defined('USERNAME') ? define('USERNAME',"root") : '' ;
!defined('PASSWORD') ? define('PASSWORD',"") : '' ;
$DBCONN = new PDO("mysql:host=".DB_HOST_NAME.";dbname=".DATABASE,USERNAME,PASSWORD);
$DBCONN->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$sql = "SHOW TABLES FROM ".DATABASE;
$result = $DBCONN->prepare($sql);
$result->execute();
while($tblnms=$result->fetch()){
$table_name[] = $tblnms[0];
}
function backup_tables($host,$user,$pass,$name,$tables)
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
$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 {
if (is_array($tables)) {
$tables = explode(',', $tables);
}
}
// Cycle through each provided table
foreach($tables as $table) {
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
// First part of the output – remove the table
// $return .= 'DROP TABLE ' . $table . ';<|||||||>';
// Second part of the output – create table
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return .= "\n\n" . $row2[1] . ";\n\n";
// Third part of the output – insert values into new table
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] = str_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";
}
// Generate the filename for the sql file
$filess ='uploads/dbbackup_' . date('dmY').'_'.time() . '.sql';
$zipfilenm='dbbackup_' . date('dmY').'_'.time() . '.zip';
// Save the sql file
$handle = fopen($filess,'w+');
fwrite($handle,$return);
fclose($handle);
if(extension_loaded('zip'))
{
// Checking ZIP extension is available
$zip = new ZipArchive(); // Load zip library
$zip_name = 'uploads/'.$zipfilenm;
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
// Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time";
}
$zip->addFile($filess); // Adding files into zip
$zip->close();
if(file_exists($zip_name))
{
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
}
else
$error .= "* You dont have ZIP extension";
mysql_close();
}
backup_tables('localhost','root','','yourdb',"*");
?>
我会检查以确保用户帐户 IIS 在 运行 下(通常是 IIS_IUSRS)或者 MySQL 在 运行 下具有写入权限您要备份到的文件夹。在 ASP 代码中会抛出拒绝访问错误,但我不知道 PHP.
有人要求我在旧 vps 服务器上设置计划备份。该网站在经典 ASP 上运行,我一直在尝试使用 ASP 查找要备份的代码,但看不到简单的代码。
我现在尝试使用我在 PHP 网站上使用的 PHP 代码进行备份,它不会显示错误或创建备份文件。 PHP运行网站上的版本是5.2.17
这是我试过的 PHP 代码。
$fileNameBackup = 'daily-db-backup-'.date( 'Y-m-d' ).'.sql.gz';
$return_var = NULL;
$output = NULL;
$uniqueFilename = uniqid();
$command = "mysqldump -u ".$user." -h ".$host." -p ".$password." ".$database." | gzip > ".$fileNameBackup;
exec($command, $output, $return_var);
我从这里去哪里?如果有错误,我会反对但我没有想法,有人可以帮助我吗?
尝试将 mysqldump 命令重写为:
$command = "mysqldump -u ".$user." -h ".$host." -p".$password." ".$database." | gzip > ".$fileNameBackup;
注意我删除了 -p 后的 space,因为它应该是连续的。
来自 docs -
The password to use when connecting to the server. If you use the short option form (
-p
), you cannot have a space between the option and the password. If you omit the password value following the --password or -p option on the command line, mysql prompts for one.
<?php
set_time_limit (0);
!defined('DB_HOST_NAME') ? define('DB_HOST_NAME',"localhost") : '' ;
!defined('DATABASE') ? define('DATABASE',"yourdb") : '' ;
!defined('USERNAME') ? define('USERNAME',"root") : '' ;
!defined('PASSWORD') ? define('PASSWORD',"") : '' ;
$DBCONN = new PDO("mysql:host=".DB_HOST_NAME.";dbname=".DATABASE,USERNAME,PASSWORD);
$DBCONN->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$sql = "SHOW TABLES FROM ".DATABASE;
$result = $DBCONN->prepare($sql);
$result->execute();
while($tblnms=$result->fetch()){
$table_name[] = $tblnms[0];
}
function backup_tables($host,$user,$pass,$name,$tables)
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
$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 {
if (is_array($tables)) {
$tables = explode(',', $tables);
}
}
// Cycle through each provided table
foreach($tables as $table) {
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
// First part of the output – remove the table
// $return .= 'DROP TABLE ' . $table . ';<|||||||>';
// Second part of the output – create table
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return .= "\n\n" . $row2[1] . ";\n\n";
// Third part of the output – insert values into new table
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] = str_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";
}
// Generate the filename for the sql file
$filess ='uploads/dbbackup_' . date('dmY').'_'.time() . '.sql';
$zipfilenm='dbbackup_' . date('dmY').'_'.time() . '.zip';
// Save the sql file
$handle = fopen($filess,'w+');
fwrite($handle,$return);
fclose($handle);
if(extension_loaded('zip'))
{
// Checking ZIP extension is available
$zip = new ZipArchive(); // Load zip library
$zip_name = 'uploads/'.$zipfilenm;
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
// Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time";
}
$zip->addFile($filess); // Adding files into zip
$zip->close();
if(file_exists($zip_name))
{
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
}
else
$error .= "* You dont have ZIP extension";
mysql_close();
}
backup_tables('localhost','root','','yourdb',"*");
?>
我会检查以确保用户帐户 IIS 在 运行 下(通常是 IIS_IUSRS)或者 MySQL 在 运行 下具有写入权限您要备份到的文件夹。在 ASP 代码中会抛出拒绝访问错误,但我不知道 PHP.