2014-08-28 51 views
-1

我发现了一个PHP脚本,用于备份我网站的数据库并将文件发送到我选择的电子邮件地址。我在我的网站上实现了它,但它失败了。PHP备份数据库并发送电子邮件

备份类。这种处理备份和电子邮件:

<?php 
    class Backup 
    { 
     /** 
     * @var stores the options 
     */ 
     var $config; 

     /** 
     * @var stores the final sql dump 
     */ 
     var $dump; 

     /** 
     * @var stores the table structure + inserts for every table 
     */ 
     var $struktur = array(); 

     /** 
     * @var zip file name 
     */ 
     var $datei; 


     /** 
     * this function is the constructor and phrase the options 
     * and connect to the database 
     * @return 
     */ 
     public function Backup($options) 
     { 
       // write options 
       foreach($options AS $name => $value) 
       { 
         $this->config[$name] = $value; 
       } 

       // check mysql connection 
       mysql_connect($this->config['mysql'][0], $this->config['mysql'][1], $this->config['mysql'][2]) or die(mysql_error()); 
       mysql_select_db($this->config['mysql'][3]) or die(mysql_error()); 
     } 

     /** 
     * this function start the backup progress its the core function 
     * @return 
     */ 
     public function backupDB() 
     { 
       // start backup 
       if(isset($_POST['backup'])) 
       { 
         // check if tables are selected 
         if(empty($_POST['table'])) 
         { 
           die("Please select a table."); 
         } 

         /** start backup **/ 
         $tables = array(); 
         $insert = array(); 
         $sql_statement = ''; 

         // lock tables 
         foreach($_POST['table'] AS $table) 
         { 
           mysql_query("LOCK TABLE $table WRITE"); 

           // Read table structure 
           $res = mysql_query('SHOW CREATE TABLE '.$table.''); 
           $createtable = mysql_result($res, 0, 1); 
           $str = "\n\n".$createtable."\n\n"; 

           array_push($tables, $str); 

           // Read table "inserts" 
        $sql = 'SELECT * FROM '.$table; 
        $query = mysql_query($sql) or die(mysql_error()); 
        $feld_anzahl = mysql_num_fields($query); 

           $sql_statement = '-- 
-- Data Table `$table` 
-- 

             '; 

           // start reading progress 
        while($ds = mysql_fetch_object($query)){ 
         $sql_statement .= 'INSERT INTO `'.$table.'` ('; 

         for ($i = 0;$i <$feld_anzahl;$i++){ 
          if ($i ==$feld_anzahl-1){ 
           $sql_statement .= mysql_field_name($query,$i); 
          } else { 
           $sql_statement .= mysql_field_name($query,$i).', '; 
          } 
         } 

         $sql_statement .= ') VALUES ('; 

         for ($i = 0;$i <$feld_anzahl;$i++){ 
          $name = mysql_field_name($query,$i); 
          if (empty($ds->$name)){ 
           $ds->$name = 'NULL'; 
          } 
          if ($i ==$feld_anzahl-1){ 
           $sql_statement .= '"'.$ds->$name.'"'; 
          } else { 
           $sql_statement .= '"'.$ds->$name.'", '; 
          } 
         } 
         $sql_statement .= ");\n"; 
        } 

           // insert "Inserts" into an array if not exists 
           if(!in_array($sql_statement, $insert)) 
           { 
             array_push($insert, $sql_statement); 
             unset($sql_statement); 
           } 

           unset($sql_statement); 

         } 

         // put table structure and inserts together in one var 
         $this->struktur = array_combine($tables, $insert); 

         // create full dump 
         $this->createDUMP($this->struktur); 

         // create zip file 
         $this->createZIP(); 

         /** end backup **/ 

         // send an email with the sql dump 
         if(isset($this->config['email']) && !empty($this->config['email'])) 
         { 
           $this->sendEmail(); 
         } 

         // output 
         echo '<h3 style="color:green;">Backup war erfolgreich</h3><a href="'.$this->datei.'">Download Backup</a> 
         <br /> 
         <br />'; 
       } 
     } 

     /** 
     * this function generate an email with attachment 
     * @return 
     */ 
     protected function sendEmail() 
     { 
         // start sending emails 
         foreach($this->config['email'] AS $email) 
         { 
           $to = $email; 

           $from = $this->config['email'][0]; 

           $message_body = "This email contains the database backup as a zip file."; 

           $msep = strtoupper (md5 (uniqid (time()))); 

           // set email header (only text) 
           $header = 
             "From: $from\r\n" . 
             "MIME-Version: 1.0\r\n" . 
             "Content-Type: multipart/mixed; boundary=" . $msep ."\r\n\r\n" . 
             "--" . $msep . "\r\n" . 
             "Content-Type: text/plain\r\n" . 
             "Content-Transfer-Encoding: 8bit\r\n\r\n" . 
             $message_body . "\r\n"; 

           // file name 
           $dateiname = $this->datei; 

           // get filesize of zip file 
           $dateigroesse = filesize ($dateiname); 

           // open file to read 
           $f = fopen ($dateiname, "r"); 
           // save content 
           $attached_file = fread ($f, $dateigroesse); 
           // close file 
           fclose ($f); 

           // create attachment 
           $attachment = chunk_split (base64_encode ($attached_file)); 

           // set attachment header 
           $header .= 
              "--" . $msep . "\r\n" . 
              "Content-Type: application/zip; name='Backup'\r\n" . 
              "Content-Transfer-Encoding: base64\r\n" . 
              "Content-Disposition: attachment; filename='Backup.zip'\r\n" . 
              "Content-Description: Mysql Datenbank Backup im Anhang\r\n\r\n" . 
              $attachment . "\r\n"; 

           // mark end of attachment 
           $header .= "--" . "$msep--" . "; 

           // eMail Subject 
           $subject = "Database Backup"; 

           // send email to emails^^ 
           if(mail($to, $subject, '', $header) == FALSE) 
           { 
             die("The email could not be sent. Please check the email address."); 
           } 

           echo "<p><small>Email was successfully sent.</small></p>"; 
         } 
     } 

     /** 
     * this function create the zip file with the database dump and save it on the ftp server 
     * @return 
     */ 
     protected function createZIP() 
     { 

       // Set permissions to 777 
       chmod($this->config['folder'], 0777); 

       // create zip file 
       $zip = new ZipArchive(); 
       // Create file name 
       $this->datei = $this->config['folder'] . $this->config['mysql'][3] . "_" . date("j_F_Y_g:i_a") . ".zip"; 

       // Checking if file could be created 
       if ($zip->open($this->datei, ZIPARCHIVE::CREATE)!==TRUE) { 
         exit("cannot open <".$this->datei.">\n"); 
       } 

       // add mysql dump to zip file 
       $zip->addFromString("dump.sql", $this->dump); 
       // close file 
       $zip->close(); 

       // Check whether file has been created 
       if(!file_exists($this->datei)) 
       { 
         die("The ZIP file could not be created."); 
       } 

       echo "<p><small>The zip was created.</small></p>"; 
     } 

     /** 
     * this function create the full sql dump 
     * @param object $dump 
     * @return 
     */ 
     protected function createDUMP($dump) 
     { 
       $date = date("F j, Y, g:i a"); 

       $header = <<<HEADER 
-- SQL Dump 
-- 
-- Host: {$_SERVER['HTTP_HOST']} 
-- Erstellungszeit: {$date} 

-- 
-- Datenbank: `{$this->config['mysql'][3]}` 
-- 

-- -------------------------------------------------------- 

HEADER; 
       foreach($dump AS $name => $value) 
       { 
         $sql .= $name.$value; 
       } 
       $this->dump = $header.$sql; 
     } 


     /** 
     * this function displays the output form to select tables 
     * @return 
     */ 
     public function outputForm() 
     { 
       // select all tables from database 
       $result = mysql_list_tables($this->config['mysql'][3]); 

       $buffer = ' 
       <fieldset> 
         <legend>Select some tables</legend> 
         <form method="post" action=""> 
       <select name="table[]" multiple="multiple" size="30">'; 
       while($row = mysql_fetch_row($result)) 
       { 
         $buffer .= '<option value="'.$row[0].'">'.$row[0].'</option>'; 
       } 
       $buffer .= '</select> 
       <br /><br /> 
       <input type="submit" name="backup" value="Backup Tables" /> 
       </form> 
       </fieldset>'; 

       echo $buffer; 
     } 
    } 
?> 

接下来是处理程序,它当用户按压网站上的备份按钮叫:

<?php 
    //You can add as many email addresses as you like 
    $options = array( 'email' => array('email1, email2'), 
         'folder' => './backup/', 
         'mysql' => array('localhost', 'root', '', 'database') 
        ); 

    $b = new Backup($options); 

    // if submit form start backup 
    if(isset($_POST['backup'])) 
    { 
     // start backup 
     $b->backupDB(); 
    } 

    // display tables 
    $b->outputForm(); 
?> 

最后的脚本工作,据我可以告诉。但对方提供了以下错误消息

Parse error: syntax error, unexpected T_VARIABLE in /home/u164555197/public_html/ChiroDB2/Scripts/PHP/backupdb.php on line 170 

170线位于函数来处理电子邮件的代(功能SendEmail())

人可以帮我找到这个错误,也许有一个解?

+1

使用适当的变量连接:'' - '。$ msep。'\ r \ n“'而不是'” - $ msep \ r \ n“' – DanFromGermany 2014-08-28 09:49:12

+0

好的,它没有帮助虽然错误。 – RafS 2014-08-28 09:55:18

+1

你真的*为PHP4开发?当你调查这个bug时,你还试过了什么? – PeeHaa 2014-08-28 09:59:00

回答

1

变化的线,所以他们

// mark end of attachment 
$header .= "--" . $msep . "--"; 

// eMail Subject 
$subject = "Database Backup"; 

这是你粘贴类线202。

<?php 
    class Backup 
    { 
     /** 
     * @var stores the options 
     */ 
     var $config; 

     /** 
     * @var stores the final sql dump 
     */ 
     var $dump; 

     /** 
     * @var stores the table structure + inserts for every table 
     */ 
     var $struktur = array(); 

     /** 
     * @var zip file name 
     */ 
     var $datei; 


     /** 
     * this function is the constructor and phrase the options 
     * and connect to the database 
     * @return 
     */ 
     public function Backup($options) 
     { 
       // write options 
       foreach($options AS $name => $value) 
       { 
         $this->config[$name] = $value; 
       } 

       // check mysql connection 
       mysql_connect($this->config['mysql'][0], $this->config['mysql'][1], $this->config['mysql'][2]) or die(mysql_error()); 
       mysql_select_db($this->config['mysql'][3]) or die(mysql_error()); 
     } 

     /** 
     * this function start the backup progress its the core function 
     * @return 
     */ 
     public function backupDB() 
     { 
       // start backup 
       if(isset($_POST['backup'])) 
       { 
         // check if tables are selected 
         if(empty($_POST['table'])) 
         { 
           die("Please select a table."); 
         } 

         /** start backup **/ 
         $tables = array(); 
         $insert = array(); 
         $sql_statement = ''; 

         // lock tables 
         foreach($_POST['table'] AS $table) 
         { 
           mysql_query("LOCK TABLE $table WRITE"); 

           // Read table structure 
           $res = mysql_query('SHOW CREATE TABLE '.$table.''); 
           $createtable = mysql_result($res, 0, 1); 
           $str = "\n\n".$createtable."\n\n"; 

           array_push($tables, $str); 

           // Read table "inserts" 
        $sql = 'SELECT * FROM '.$table; 
        $query = mysql_query($sql) or die(mysql_error()); 
        $feld_anzahl = mysql_num_fields($query); 

           $sql_statement = '-- 
-- Data Table `$table` 
-- 

             '; 

           // start reading progress 
        while($ds = mysql_fetch_object($query)){ 
         $sql_statement .= 'INSERT INTO `'.$table.'` ('; 

         for ($i = 0;$i <$feld_anzahl;$i++){ 
          if ($i ==$feld_anzahl-1){ 
           $sql_statement .= mysql_field_name($query,$i); 
          } else { 
           $sql_statement .= mysql_field_name($query,$i).', '; 
          } 
         } 

         $sql_statement .= ') VALUES ('; 

         for ($i = 0;$i <$feld_anzahl;$i++){ 
          $name = mysql_field_name($query,$i); 
          if (empty($ds->$name)){ 
           $ds->$name = 'NULL'; 
          } 
          if ($i ==$feld_anzahl-1){ 
           $sql_statement .= '"'.$ds->$name.'"'; 
          } else { 
           $sql_statement .= '"'.$ds->$name.'", '; 
          } 
         } 
         $sql_statement .= ");\n"; 
        } 

           // insert "Inserts" into an array if not exists 
           if(!in_array($sql_statement, $insert)) 
           { 
             array_push($insert, $sql_statement); 
             unset($sql_statement); 
           } 

           unset($sql_statement); 

         } 

         // put table structure and inserts together in one var 
         $this->struktur = array_combine($tables, $insert); 

         // create full dump 
         $this->createDUMP($this->struktur); 

         // create zip file 
         $this->createZIP(); 

         /** end backup **/ 

         // send an email with the sql dump 
         if(isset($this->config['email']) && !empty($this->config['email'])) 
         { 
           $this->sendEmail(); 
         } 

         // output 
         echo '<h3 style="color:green;">Backup war erfolgreich</h3><a href="'.$this->datei.'">Download Backup</a> 
         <br /> 
         <br />'; 
       } 
     } 

     /** 
     * this function generate an email with attachment 
     * @return 
     */ 
     protected function sendEmail() 
     { 
         // start sending emails 
         foreach($this->config['email'] AS $email) 
         { 
           $to = $email; 

           $from = $this->config['email'][0]; 

           $message_body = "This email contains the database backup as a zip file."; 

           $msep = strtoupper (md5 (uniqid (time()))); 

           // set email header (only text) 
           $header = 
             "From: $from\r\n" . 
             "MIME-Version: 1.0\r\n" . 
             "Content-Type: multipart/mixed; boundary=" . $msep ."\r\n\r\n" . 
             "--" . $msep . "\r\n" . 
             "Content-Type: text/plain\r\n" . 
             "Content-Transfer-Encoding: 8bit\r\n\r\n" . 
             $message_body . "\r\n"; 

           // file name 
           $dateiname = $this->datei; 

           // get filesize of zip file 
           $dateigroesse = filesize ($dateiname); 

           // open file to read 
           $f = fopen ($dateiname, "r"); 
           // save content 
           $attached_file = fread ($f, $dateigroesse); 
           // close file 
           fclose ($f); 

           // create attachment 
           $attachment = chunk_split (base64_encode ($attached_file)); 

           // set attachment header 
           $header .= 
              "--" . $msep . "\r\n" . 
              "Content-Type: application/zip; name='Backup'\r\n" . 
              "Content-Transfer-Encoding: base64\r\n" . 
              "Content-Disposition: attachment; filename='Backup.zip'\r\n" . 
              "Content-Description: Mysql Datenbank Backup im Anhang\r\n\r\n" . 
              $attachment . "\r\n"; 

           // mark end of attachment 
           $header .= "--" . $msep . "--"; 

           // eMail Subject 
           $subject = "Database Backup"; 

           // send email to emails^^ 
           if(mail($to, $subject, '', $header) == FALSE) 
           { 
             die("The email could not be sent. Please check the email address."); 
           } 

           echo "<p><small>Email was successfully sent.</small></p>"; 
         } 
     } 

     /** 
     * this function create the zip file with the database dump and save it on the ftp server 
     * @return 
     */ 
     protected function createZIP() 
     { 

       // Set permissions to 777 
       chmod($this->config['folder'], 0777); 

       // create zip file 
       $zip = new ZipArchive(); 
       // Create file name 
       $this->datei = $this->config['folder'] . $this->config['mysql'][3] . "_" . date("j_F_Y_g:i_a") . ".zip"; 

       // Checking if file could be created 
       if ($zip->open($this->datei, ZIPARCHIVE::CREATE)!==TRUE) { 
         exit("cannot open <".$this->datei.">\n"); 
       } 

       // add mysql dump to zip file 
       $zip->addFromString("dump.sql", $this->dump); 
       // close file 
       $zip->close(); 

       // Check whether file has been created 
       if(!file_exists($this->datei)) 
       { 
         die("The ZIP file could not be created."); 
       } 

       echo "<p><small>The zip was created.</small></p>"; 
     } 

     /** 
     * this function create the full sql dump 
     * @param object $dump 
     * @return 
     */ 
     protected function createDUMP($dump) 
     { 
       $date = date("F j, Y, g:i a"); 

       $header = <<<HEADER 
-- SQL Dump 
-- 
-- Host: {$_SERVER['HTTP_HOST']} 
-- Erstellungszeit: {$date} 

-- 
-- Datenbank: `{$this->config['mysql'][3]}` 
-- 

-- -------------------------------------------------------- 

HEADER; 
       foreach($dump AS $name => $value) 
       { 
         $sql .= $name.$value; 
       } 
       $this->dump = $header.$sql; 
     } 


     /** 
     * this function displays the output form to select tables 
     * @return 
     */ 
     public function outputForm() 
     { 
       // select all tables from database 
       $result = mysql_list_tables($this->config['mysql'][3]); 

       $buffer = ' 
       <fieldset> 
         <legend>Select some tables</legend> 
         <form method="post" action=""> 
       <select name="table[]" multiple="multiple" size="30">'; 
       while($row = mysql_fetch_row($result)) 
       { 
         $buffer .= '<option value="'.$row[0].'">'.$row[0].'</option>'; 
       } 
       $buffer .= '</select> 
       <br /><br /> 
       <input type="submit" name="backup" value="Backup Tables" /> 
       </form> 
       </fieldset>'; 

       echo $buffer; 
     } 
    } 
?> 
+1

我通常很快找到缺失的报价/分号/ ..但我找不到那一个。恭喜+1:D – DanFromGermany 2014-08-28 10:01:16

+0

谢谢@DanFromGermany! – 2014-08-28 10:02:27

0

使用MysqlDumper。它是我能给的最好的建议。
我也将它用于我的网站。

有一个自己的接口配置它。并且还可以选择自动备份您的数据库和在特定时间发送邮件并显示成功消息。如果你愿意,你可以将备份追加到邮件中(不建议使用big dbs)。

继承人a link

不幸的是,我可以提供任何代码,因为没有什么你需要通过php代码本身设置。

如何安装:
只需复制&的文件夹粘贴到你的网络服务器,并打开索引页。安装程序将自动启动,要求进行数据库设置(登录细节,连接等)。之后即可使用。

+2

推荐其他工具最多只能作为评论。这个答案对解决目前的问题没有帮助。 – Jonast92 2014-08-28 10:02:50

+0

我会这么做的。你说得对。但是我现在不知道现在有了50个代表。它在我的帖子下可见!我更喜欢帮助而不是无所事事。不幸的是我不能downvote您的评论。 – C4u 2014-08-28 10:04:57

相关问题