install/db_backup.php

00001 <?php
00002 
00018 #error_reporting (E_ERROR | E_WARNING | E_PARSE | E_NOTICE | E_STRICT|E_PARSE|E_CORE_ERROR|E_CORE_WARNING|E_COMPILE_ERROR);
00019 
00020 
00021 $dumper= new MySQLDumper();
00022 
00023 if($dumper->connect(
00024         'localhost',    # hostname
00025         'user', # DB-username
00026         'password', # DB-password
00027         'database'  # DB-name
00028 )) {
00029    #$dumper->dump();
00030    #$dumper->executeFromFile("streber.pixtur.de.sql");
00031 }
00032 
00033 
00034 
00039 class MySQLDumper {
00040 
00041     var $dbh                = NULL;
00042     var $add_drop_statement = true;
00043     var $crlf               ="\n";
00044     var $use_backquotes     = true;
00045 
00046     function connect($hostname,$db_username,$db_password,$db_name) {
00047 
00048         $this->hostname     = $hostname;
00049         $this->db_username  = $db_username;
00050         $this->db_password  = $db_password;
00051         $this->db_name      = $db_name;
00052 
00053         $this->dbh = mysql_pconnect(
00054             $this->hostname,        # hostname
00055             $this->db_username,     # db_username
00056             $this->db_password      # db_password
00057         );
00058 
00059         if(!$this->dbh || !is_resource($this->dbh)) {
00060             echo "mysql-error:<pre>".mysql_error()."</pre>";
00061             return NULL;
00062         }
00063 
00064         ### select db ###
00065         if(!mysql_select_db($this->db_name, $this->dbh)) {
00066             echo "mysql-error:<pre>".mysql_error()."</pre>";
00067             return NULL;
00068         }
00069         return true;
00070     }
00071 
00072 
00073 
00074     function dump( ) 
00075     {
00079         set_time_limit(0);
00080 
00081         $hostname="";
00082         if(isset($_SERVER["HTTP_HOST"])) {
00083             $hostname= $_SERVER["HTTP_HOST"];
00084         }
00085 
00086 
00087         $filename   = $hostname . "_". $this->db_name.'_'. gmdate("Y-m-d_H:i"). '.gzip';
00088         $mime_type  ='application/x-gzip';
00089 
00090         ### Send headers
00091         header('Content-Type: ' . $mime_type);
00092         header('Content-Disposition: attachment; filename="'.$filename.'"');
00093         header('Expires: 0');
00094         header('Pragma: no-cache');
00095 
00096         ### IE need specific headers
00097         #if(getBrowserAgent() == 'IE') {
00098         #    #header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
00099         #    header('Expires: 0');
00100         #    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
00101         #    header('Pragma: public');
00102         #}
00103 
00104 
00105 
00106 
00107         ### Builds the dump
00108         $tables     = mysql_list_tables($this->db_name);
00109 
00110         if(!$num_tables = mysql_numrows($tables)) {
00111             echo "mysql-error:<pre>".mysql_error()."</pre>";
00112             trigger_error("no tables found", E_USER_ERROR);
00113             exit(0);
00114         }
00115 
00116         $dump_buffer    =  "# slim phpMyAdmin MySQL-Dump\n";
00117 
00118         for($i=0; $i < $num_tables; $i++) {
00119 
00120             $table_name = mysql_tablename($tables, $i);
00121             $dump_buffer.= $this->crlf
00122                         .  '#' . $this->crlf
00123                         .  '#' . $this->backquote($table_name) . $this->crlf
00124                         .  '#' . $this->crlf . $this->crlf
00125                         .  $this->getTableDef($table_name) . ';' . $this->crlf
00126                         .  $this->getTableContentFast($table_name);
00127         }
00128 
00129         $dump_buffer .= $this->crlf;
00130 
00131         ### Displays the dump as gzip-file
00132         if (function_exists('gzencode')) {
00133             echo gzencode($dump_buffer);                    # without the optional parameter level because it bugs
00134             #echo "<pre>".$dump_buffer."</pre>";
00135         }
00136         else {
00137             trigger_error("gzencode() not defined. Saving backup failed", E_USER_ERROR);
00138         }
00139     }
00140 
00141 
00142 
00143     function executeFromFile($filename) 
00144     {
00148         set_time_limit(0);
00149         $handle = @fopen("$filename", "r");
00150         if ($handle) {
00151             $exec_buffer= '';
00152             while (!feof($handle)) {
00153             
00154                 $line_buffer = fgets($handle, 4096);
00155                 $exec_buffer.= $line_buffer;
00156 
00157                 if(preg_match("/;\s*$/s", $line_buffer))     {
00158                     $result= mysql_query($exec_buffer);
00159                     echo $exec_buffer . "<br>";
00160                     if($result == FALSE) {
00161                         echo "<pre>".mysql_error()."</pre>";                        
00162                     }
00163                     
00164                     $exec_buffer="";
00165                 }
00166            }
00167            fclose($handle);
00168         }
00169     }
00170 
00171 
00172 
00173     function getTableDef($table)
00174     {
00175         $schema_create = '';
00176 
00177         if($this->add_drop_statement) {
00178             $schema_create .= 'DROP TABLE IF EXISTS ' . $this->backquote($table) . ';' . $this->crlf;
00179         }
00180 
00181         // Whether to quote table and fields names or not
00182         if ($this->use_backquotes) {
00183             mysql_query('SET SQL_QUOTE_SHOW_CREATE = 1');
00184         }
00185         else {
00186             mysql_query('SET SQL_QUOTE_SHOW_CREATE = 0');
00187         }
00188 
00189         $result = mysql_query('SHOW CREATE TABLE ' . $this->backquote($this->db_name) . '.' . $this->backquote($table));
00190 
00191         if ($result != FALSE &&  mysql_num_rows($result) > 0) {
00192             $tmpres        = mysql_fetch_array($result);
00193             $schema_create .= $tmpres[1];
00194 
00195             mysql_free_result($result);
00196             return $schema_create;
00197 
00198         }
00199         else {
00200             echo "<pre>".mysql_error()."</pre>";
00201 
00202             trigger_error("SHOW CREATE TABLE failed", E_USER_ERROR);
00203         }
00204     }
00205 
00206 
00213     function getTableContentFast($table)
00214     {
00215         $buffer='';
00216 
00217 
00218         $local_query = 'SELECT * FROM ' . '.' . $this->backquote($table);
00219         $result      = mysql_query($local_query);
00220         if ($result) {
00221             $fields_cnt = mysql_num_fields($result);
00222             $rows_cnt   = mysql_num_rows($result);
00223 
00224             ### Checks whether the field is an integer or not
00225             for ($j = 0; $j < $fields_cnt; $j++) {
00226                 $field_set[$j] = $this->backquote(mysql_field_name($result, $j));
00227                 $type          = mysql_field_type($result, $j);
00228                 if ($type == 'tinyint' || $type == 'smallint' || $type == 'mediumint' || $type == 'int' ||
00229                     $type == 'bigint'  ||$type == 'timestamp') {
00230                     $field_num[$j] = TRUE;
00231                 }
00232                 else {
00233                     $field_num[$j] = FALSE;
00234                 }
00235             }
00236 
00237             ### Sets the scheme
00238             $fields        = implode(', ', $field_set);
00239             $schema_insert = 'INSERT INTO ' . $this->backquote($table) . ' (' . $fields . ') VALUES (';
00240 
00241 
00242             $search       = array("\x00", "\x0a", "\x0d", "\x1a"); //\x08\\x09, not required
00243             $replace      = array('\0', '\n', '\r', '\Z');
00244             $current_row  = 0;
00245 
00246             while ($row = mysql_fetch_row($result)) {
00247 
00248                 $values= array();
00249                 $current_row++;
00250                 for ($j = 0; $j < $fields_cnt; $j++) {
00251                     if (!isset($row[$j])) {
00252                         $values[]     = 'NULL';
00253                     }
00254                     else if ($row[$j] == '0' || $row[$j] != '') {
00255 
00256                         ### a number
00257                         if ($field_num[$j]) {
00258                             $values[] = $row[$j];
00259                         }
00260                         ### a string
00261                         else {
00262                             $values[] = "'" . str_replace($search, $replace, $this->sqlAddslashes($row[$j])) . "'";
00263                         }
00264                     }
00265                     else {
00266                         $values[]     = "''";
00267                     }
00268                 }
00269 
00270                 $insert_line      = $schema_insert . implode(', ', $values) . ');'."\n";
00271 
00272                 $buffer.= $insert_line;
00273 
00274 
00275                 ### loic1: send a fake header to bypass browser timeout if data are bufferized
00276                 if (!empty($GLOBALS['ob_mode'])) {
00277                     header('Expires: 0');
00278                 }
00279             }
00280         }
00281         mysql_free_result($result);
00282         return $buffer;
00283     }
00284 
00285 
00291     function backquote($a_name)
00292     {
00293         if (!$a_name && $a_name != '*') {
00294             return '`' . $a_name . '`';
00295         }
00296         else {
00297             return $a_name;
00298         }
00299     }
00300 
00301     function sqlAddslashes($a_string = '')
00302     {
00303         if ($this->use_backquotes) {
00304             $a_string = str_replace('\\', '\\\\\\\\', $a_string);
00305         } else {
00306             $a_string = str_replace('\\', '\\\\', $a_string);
00307         }
00308         $a_string = str_replace('\'', '\\\'', $a_string);
00309 
00310         return $a_string;
00311     }
00312 
00313 }
00314 
00315 
00316 
00317 
00318 ?>

Generated on Sun Mar 4 17:19:28 2007 for streber by  doxygen 1.5.1-p1