2011-11-17 272 views
1

在我的網站管理面板中有一個選項可用於備份數據庫。爲此,我使用下面的代碼,但我無法使用此代碼下載數據庫。有沒有人知道這是如何發生的。 我試圖回顯$return變量和它的輸出是正確的,但我不能下載文件爲SQL。使用php下載mysql數據庫作爲sql文件使用php

<?php 

backup_tables('localhost','root','','dbname'); 


/* backup the db OR just a table */ 
function backup_tables($host,$user,$pass,$name,$tables = '*') 
{ 

    $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) 
    { 
    $return=""; 
    $result = mysql_query('SELECT * FROM '.$table); 
    $num_fields = mysql_num_fields($result); 
    //print_r($num_fields);exit; 
    $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] = preg_replace("\n","\\n",$row[$j]); 

      $row[$j] = preg_replace("/(\n){2,}/", "\\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+'); 
// print_r($handle);exit; 
    fwrite($handle,$return); 
    fclose($handle); 

} 
?> 
+0

可能重複http://stackoverflow.com/questions/22195493/export- MySQL的數據庫,使用的PHP只) –

回答

2

而不是自己創建所有東西你可以使用system()調用,只使用mysql備份功能。這是一個小小的聲明,它將在您想要的服務器上創建一個文件。 爲了下載文件,請在回顯內容之前使用header()設置正確的標頭。

0

您正在保存在服務器上的文件,而不是將其發送回瀏覽器下載的使用mysqldump。您需要發送包含文件內容的標題以供下載。

header("Content-type: application/octet-stream"); 
header('Content-Disposition: attachment; filename=db_dump'); 
echo backup_tables('localhost','root','','dbname'); // change your function to return the data instead saving in a file 

但請記住,這不是製作數據庫轉儲的最佳方法。

0

將整個數據庫加載到PHP內存並不意味着我是一個非常明智的備份數據庫的方式。

假設您沒有CLI訪問來運行mysqldump,也不能在別處複製數據庫,唯一合理的解決方案是將select語句的輸出寫入web服務器緩衝區(定期刷新)。

您嘗試使用的腳本還包含系統性缺陷。數據寫入「返回」變量,但該變量永遠不會被函數返回。該變量在處理每個表的開始時被截斷。

所有這些問題都是3將通過替換的每個實例解析:

$return.= 

隨着

print 

(但不要忘記添加一些刷新)

HTH

2
//save file 
    $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+'); 
// print_r($handle);exit; 
    fwrite($handle,$return); 
    fclose($handle); 
//add below code to download it as a sql file 
Header('Content-type: application/octet-stream'); 
Header('Content-Disposition: attachment; filename=db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql'); 
echo $return; 
0

所有先前提到的問題(加載到PHP內存和mysqldump的可用性)放在一邊,腳本中還有一個錯誤,它只返回最後一個表的數據。 $return="";需要行被外界和foreach循環之前,它在喜歡的東西:

<?php 

backup_tables('localhost','root','','dbname'); 


/* backup the db OR just a table */ 
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 
    { 
    $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); 
    //print_r($num_fields);exit; 
    $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] = preg_replace("\n","\\n",$row[$j]); 

      $row[$j] = preg_replace("/(\n){2,}/", "\\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+'); 
// print_r($handle);exit; 
    fwrite($handle,$return); 
    fclose($handle); 

} 
?> 
[使用PHP只導出MySQL數據庫(的