2013-04-29 155 views
0

這是我的腳本:PHP/MySQL數據庫備份腳本無法正常工作

<?php 

ini_set('display_errors',1); 
ini_set('display_startup_errors',1); 
error_reporting(-1); 

backup_tables('localhost','xxxxx','xxxxx','xxxxx'); 


/* 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) 
    { 
     $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"; 
    } 

    $backup_file = $_SERVER['DOCUMENT_ROOT'] . '/scripts_and_crons/fr_internetlead-'.time().'.sql'; 
    //save file 
    $handle = fopen($backup_file,'w+'); 
    fwrite($handle,$return); 
    fclose($handle); 
} 

//define the receiver of the email 
$to = '[email protected]'; 
//define the subject of the email 
$subject = 'Test email with attachment'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: [email protected]\r\nReply-To: [email protected]"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string, 
//encode it with MIME base64, 
//and split it into smaller chunks 
$attachment = chunk_split(base64_encode(file_get_contents($backup_file))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/sql; name="<?php echo $backup_file; ?>" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail($to, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 

?> 

唯一的調試信息,我在PHP中得到的是

Notice: Undefined variable: return in /var/www/vhosts/domain.com/httpdocs/scripts_and_crons/internetlead_backup.php on line 38

Deprecated: Function ereg_replace() is deprecated in /var/www/vhosts/domain.com/httpdocs/scripts_and_crons/internetlead_backup.php on line 50 X無限大

唯一附件I收到的電子郵件是ATT00001

echo '<pre>'; 
echo $return; 
echo '</pre>'; 

打印出正確的數據。

即使做

echo '<pre>'; 
echo file_get_contents($backup_file); 
echo '</pre>'; 

打印出正確的數據,使文件被寫入,但由於某種原因,腳本在其附加到電子郵件失敗。

看不出有什麼可能是錯的,有什麼想法?

馬丁

UPDATE

發現在另一個SO問題的答案 - PHP mail() attachment problems

這是正確的方式去做。

+1

'它必須是唯一的,所以我們使用MD5算法生成一個隨機hash'這是一個好的笑 – Bobby 2013-04-29 12:02:24

+0

另外'mysql_ *'被棄用,被替換爲'mysqli_ *'或者更好的'PDO'。 – Bobby 2013-04-29 12:04:14

+0

是的,'mysql_ *'已被棄用,除此之外。 – martincarlin87 2013-04-29 12:06:56

回答

0

如果你的PHP版本比5.2更高,你應該用的preg_match和變更(替換ereg_replace「部分爲(「/