我被要求在旧的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);
我从哪里开始?如果有错误我会有什么可以对抗但是我没有想法,有人可以帮助我吗?
答案 0 :(得分:1)
尝试将mysqldump命令重写为:
$command = "mysqldump -u ".$user." -h ".$host." -p".$password." ".$database." | gzip > ".$fileNameBackup;
注意我在-p之后删除了空格,因为它应该是连续的。
来自docs -
连接服务器时使用的密码。如果您使用短选项表单(
-p
), ,则选项和密码之间不能有空格 。如果省略命令行上--password或-p选项后面的密码值,mysql会提示输入密码值。
答案 1 :(得分:1)
<?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',"*");
?>
答案 2 :(得分:1)
我会检查以确保IIS下运行的用户帐户(通常是IIS_IUSRS)或运行MySQL的用户帐户对您要备份到的文件夹具有写入权限。在ASP代码中会抛出Access Denied错误,但我不知道PHP。