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 'wikiwiki', # DB-username
00026 'ij1dw392', # DB-password
00027 'wikiwiki' # DB-name
00028 )) {
00029 $dumper->dump();
00030 }
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 function dump( )
00074 {
00078 set_time_limit(0);
00079
00080 $hostname="";
00081 if(isset($_SERVER["HTTP_HOST"])) {
00082 $hostname= $_SERVER["HTTP_HOST"];
00083 }
00084
00085
00086 $filename = $hostname . "_". $this->db_name.'_'. gmdate("Y-m-d_H:i"). '.gzip';
00087 $mime_type ='application/x-gzip';
00088
00089 ### Send headers
00090 header('Content-Type: ' . $mime_type);
00091 header('Content-Disposition: attachment; filename="'.$filename.'"');
00092 header('Expires: 0');
00093 header('Pragma: no-cache');
00094
00095 ### IE need specific headers
00096 #if(getBrowserAgent() == 'IE') {
00097 # #header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
00098 # header('Expires: 0');
00099 # header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
00100 # header('Pragma: public');
00101 #}
00102
00103
00104
00105
00106 ### Builds the dump
00107 $tables = mysql_list_tables($this->db_name);
00108
00109 if(!$num_tables = mysql_numrows($tables)) {
00110 echo "mysql-error:<pre>".mysql_error()."</pre>";
00111 trigger_error("no tables found", E_USER_ERROR);
00112 exit(0);
00113 }
00114
00115 $dump_buffer = "# slim phpMyAdmin MySQL-Dump\n";
00116
00117 for($i=0; $i < $num_tables; $i++) {
00118
00119 $table_name = mysql_tablename($tables, $i);
00120 $dump_buffer.= $this->crlf
00121 . '#' . $this->crlf
00122 . '#' . $this->backquote($table_name) . $this->crlf
00123 . '#' . $this->crlf . $this->crlf
00124 . $this->getTableDef($table_name) . ';' . $this->crlf
00125 . $this->getTableContentFast($table_name);
00126 }
00127
00128 $dump_buffer .= $this->crlf;
00129
00130 ### Displays the dump as gzip-file
00131 if (function_exists('gzencode')) {
00132 echo gzencode($dump_buffer); # without the optional parameter level because it bugs
00133 #echo "<pre>".$dump_buffer."</pre>";
00134 }
00135 else {
00136 trigger_error("gzencode() not defined. Saving backup failed", E_USER_ERROR);
00137 }
00138 }
00139
00140
00141
00142
00143 function getTableDef($table)
00144 {
00145 $schema_create = '';
00146
00147 if($this->add_drop_statement) {
00148 $schema_create .= 'DROP TABLE IF EXISTS ' . $this->backquote($table) . ';' . $this->crlf;
00149 }
00150
00151
00152 if ($this->use_backquotes) {
00153 mysql_query('SET SQL_QUOTE_SHOW_CREATE = 1');
00154 }
00155 else {
00156 mysql_query('SET SQL_QUOTE_SHOW_CREATE = 0');
00157 }
00158
00159 $result = mysql_query('SHOW CREATE TABLE ' . $this->backquote($this->db_name) . '.' . $this->backquote($table));
00160
00161 if ($result != FALSE && mysql_num_rows($result) > 0) {
00162 $tmpres = mysql_fetch_array($result);
00163 $schema_create .= $tmpres[1];
00164
00165 mysql_free_result($result);
00166 return $schema_create;
00167
00168 }
00169 else {
00170 echo "<pre>".mysql_error()."</pre>";
00171
00172 trigger_error("SHOW CREATE TABLE failed", E_USER_ERROR);
00173 }
00174 }
00175
00176
00183 function getTableContentFast($table)
00184 {
00185 $buffer='';
00186
00187
00188 $local_query = 'SELECT * FROM ' . '.' . $this->backquote($table);
00189 $result = mysql_query($local_query);
00190 if ($result) {
00191 $fields_cnt = mysql_num_fields($result);
00192 $rows_cnt = mysql_num_rows($result);
00193
00194 ### Checks whether the field is an integer or not
00195 for ($j = 0; $j < $fields_cnt; $j++) {
00196 $field_set[$j] = $this->backquote(mysql_field_name($result, $j));
00197 $type = mysql_field_type($result, $j);
00198 if ($type == 'tinyint' || $type == 'smallint' || $type == 'mediumint' || $type == 'int' ||
00199 $type == 'bigint' ||$type == 'timestamp') {
00200 $field_num[$j] = TRUE;
00201 }
00202 else {
00203 $field_num[$j] = FALSE;
00204 }
00205 }
00206
00207 ### Sets the scheme
00208 $fields = implode(', ', $field_set);
00209 $schema_insert = 'INSERT INTO ' . $this->backquote($table) . ' (' . $fields . ') VALUES (';
00210
00211
00212 $search = array("\x00", "\x0a", "\x0d", "\x1a");
00213 $replace = array('\0', '\n', '\r', '\Z');
00214 $current_row = 0;
00215
00216 while ($row = mysql_fetch_row($result)) {
00217
00218 $values= array();
00219 $current_row++;
00220 for ($j = 0; $j < $fields_cnt; $j++) {
00221 if (!isset($row[$j])) {
00222 $values[] = 'NULL';
00223 }
00224 else if ($row[$j] == '0' || $row[$j] != '') {
00225
00226 ### a number
00227 if ($field_num[$j]) {
00228 $values[] = $row[$j];
00229 }
00230 ### a string
00231 else {
00232 $values[] = "'" . str_replace($search, $replace, $this->sqlAddslashes($row[$j])) . "'";
00233 }
00234 }
00235 else {
00236 $values[] = "''";
00237 }
00238 }
00239
00240 $insert_line = $schema_insert . implode(', ', $values) . ');'."\n";
00241
00242 $buffer.= $insert_line;
00243
00244
00245 ### loic1: send a fake header to bypass browser timeout if data are bufferized
00246 if (!empty($GLOBALS['ob_mode'])) {
00247 header('Expires: 0');
00248 }
00249 }
00250 }
00251 mysql_free_result($result);
00252 return $buffer;
00253 }
00254
00255
00261 function backquote($a_name)
00262 {
00263 if (!$a_name && $a_name != '*') {
00264 return '`' . $a_name . '`';
00265 }
00266 else {
00267 return $a_name;
00268 }
00269 }
00270
00271 function sqlAddslashes($a_string = '')
00272 {
00273 if ($this->use_backquotes) {
00274 $a_string = str_replace('\\', '\\\\\\\\', $a_string);
00275 } else {
00276 $a_string = str_replace('\\', '\\\\', $a_string);
00277 }
00278 $a_string = str_replace('\'', '\\\'', $a_string);
00279
00280 return $a_string;
00281 }
00282
00283 }
00284
00285
00286
00287
00288 ?>