PHP / MySQL数据库备份脚本无法正常工作

时间:2013-04-29 12:00:00

标签: mysql php

这是我的剧本:

<?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@domain.com'; 
//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: backups@domain.com\r\nReply-To: noreply@domain.com"; 
//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 infinity

我在电子邮件中收到的唯一附件是ATT00001

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

打印出正确的数据。

即使在做

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

打印出正确的数据,以便写入文件,但由于某种原因,脚本无法将其附加到电子邮件中。

看不出有什么不对,有什么想法吗?

马丁

更新

在另一个SO问题中找到答案 - PHP mail() attachment problems

这是正确的方法。

1 个答案:

答案 0 :(得分:0)

如果你的php版本高于5.2你应该用preg_match替换ereg_replace并更改(“parts as(”/ /

相关问题