2017-04-17 24 views
0

我被要求在舊的vps服務器上設置一個定時備份。該網站運行在經典的ASP上,我一直在試圖找到使用ASP進行備份的代碼,但無法看到它的簡單代碼。PHP MySql備份不起作用 - 無錯誤顯示

我已經嘗試使用我在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); 

我該從哪裏出發?如果有錯誤,我會有一些反對,但沒有想法,有人可以幫我嗎?

+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',"*"); 

?> 
+0

謝謝,我已經嘗試了這一點,它隨之而來,然後回來有錯誤: mysql_num_fields():提供的參數不是線57上的有效MySQL結果資源:$ result = mysql_query('SELECT * FROM' 。$表);和第63行:$ row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE'。$ table)); 除了這些錯誤,它可能會起作用。 – GeneralTomfoolery

+0

即使這已經引發了一些錯誤它似乎工作,謝謝@ karunakaran – GeneralTomfoolery

1

嘗試重新寫mysqldump命令到這一點:

$command = "mysqldump -u ".$user." -h ".$host." -p".$password." ".$database." | gzip > ".$fileNameBackup; 

通知我-p後取出的空間,因爲它應該是連續的。

docs -

的密碼連接到服務器時使用。如果使用短期選項表格(-p),,則在選項和密碼之間不能有空格。如果您在命令行中輸入--password或-p選項後忽略密碼值,mysql會提示輸入密碼值。

+0

謝謝,我確實嘗試了這兩種方法。沒有快樂。感謝您的期待。 – GeneralTomfoolery

1

我會檢查,以確保用戶帳戶IIS下(通常IIS_IUSRS)正在運行或MySQL的運行下有寫權限您要備份的文件夾。在ASP代碼會拋出一個訪問拒絕錯誤,但我不知道PHP。