'')) . "` LIKE '" . str_replace('_', '\_', $match[2]) . "%'", false, false); } else { $queryTables = db_query(" SHOW TABLES LIKE '" . str_replace('_', '\_', $db_prefix) . "%'", false, false); } // Dump each table. while ($tableName = mysql_fetch_row($queryTables)) { if (function_exists('apache_reset_timeout')) apache_reset_timeout(); // Are we dumping the structures? if (isset($_GET['struct'])) { echo $crlf, '#', $crlf, '# Table structure for table `', $tableName[0], '`', $crlf, '#', $crlf, $crlf, 'DROP TABLE IF EXISTS `', $tableName[0], '`;', $crlf, $crlf, getTableSQLData($tableName[0]), ';', $crlf; } // How about the data? if (!isset($_GET['data']) || substr($tableName[0], -10) == 'log_errors') continue; // Are there any rows in this table? $get_rows = getTableContent($tableName[0]); // No rows to get - skip it. if (empty($get_rows)) continue; echo $crlf, '#', $crlf, '# Dumping data in `', $tableName[0], '`', $crlf, '#', $crlf, $crlf, $get_rows, '# --------------------------------------------------------', $crlf; } mysql_free_result($queryTables); echo $crlf, '# Done', $crlf; exit; } // Get the content (INSERTs) for a table. function getTableContent($tableName) { global $crlf; // Get everything from the table. $result = db_query(" SELECT /*!40001 SQL_NO_CACHE */ * FROM `$tableName`", false, false); // The number of rows, just for record keeping and breaking INSERTs up. $num_rows = @mysql_num_rows($result); $current_row = 0; if ($num_rows == 0) return ''; $fields = array_keys(mysql_fetch_assoc($result)); mysql_data_seek($result, 0); // Start it off with the basic INSERT INTO. $data = 'INSERT INTO `' . $tableName . '`' . $crlf . "\t(`" . implode('`, `', $fields) . '`)' . $crlf . 'VALUES '; // Loop through each row. while ($row = mysql_fetch_row($result)) { $current_row++; // Get the fields in this row... $field_list = array(); for ($j = 0; $j < mysql_num_fields($result); $j++) { // Try to figure out the type of each field. (NULL, number, or 'string'.) if (!isset($row[$j])) $field_list[] = 'NULL'; elseif (is_numeric($row[$j])) $field_list[] = $row[$j]; else $field_list[] = "'" . mysql_escape_string($row[$j]) . "'"; } // 'Insert' the data. $data .= '(' . implode(', ', $field_list) . ')'; // Start a new INSERT statement after every 250.... if ($current_row > 249 && $current_row % 250 == 0) $data .= ';' . $crlf . 'INSERT INTO `' . $tableName . '`' . $crlf . "\t(`" . implode('`, `', $fields) . '`)' . $crlf . 'VALUES '; // All done! elseif ($current_row == $num_rows) $data .= ';' . $crlf; // Otherwise, go to the next line. else $data .= ',' . $crlf . "\t"; } mysql_free_result($result); // Return an empty string if there were no rows. return $num_rows == 0 ? '' : $data; } // Get the schema (CREATE) for a table. function getTableSQLData($tableName) { global $crlf; // Start the create table... $schema_create = 'CREATE TABLE `' . $tableName . '` (' . $crlf; // Find all the fields. $result = db_query(" SHOW FIELDS FROM `$tableName`", false, false); while ($row = @mysql_fetch_assoc($result)) { // Make the CREATE for this column. $schema_create .= ' ' . $row['Field'] . ' ' . $row['Type'] . ($row['Null'] != 'YES' ? ' NOT NULL' : ''); // Add a default...? if (isset($row['Default'])) { // Make a special case of auto-timestamp. if ($row['Default'] == 'CURRENT_TIMESTAMP') $schema_create .= ' /*!40102 NOT NULL default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP */'; else $schema_create .= ' default ' . (is_numeric($row['Default']) ? $row['Default'] : "'" . mysql_escape_string($row['Default']) . "'"); } // And now any extra information. (such as auto_increment.) $schema_create .= ($row['Extra'] != '' ? ' ' . $row['Extra'] : '') . ',' . $crlf; } @mysql_free_result($result); // Take off the last comma. $schema_create = substr($schema_create, 0, -strlen($crlf) - 1); // Find the keys. $result = db_query(" SHOW KEYS FROM `$tableName`", false, false); $indexes = array(); while ($row = @mysql_fetch_assoc($result)) { // IS this a primary key, unique index, or regular index? $row['Key_name'] = $row['Key_name'] == 'PRIMARY' ? 'PRIMARY KEY' : (empty($row['Non_unique']) ? 'UNIQUE ' : ($row['Comment'] == 'FULLTEXT' || (isset($row['Index_type']) && $row['Index_type'] == 'FULLTEXT') ? 'FULLTEXT ' : 'KEY ')) . $row['Key_name']; // Is this the first column in the index? if (empty($indexes[$row['Key_name']])) $indexes[$row['Key_name']] = array(); // A sub part, like only indexing 15 characters of a varchar. if (!empty($row['Sub_part'])) $indexes[$row['Key_name']][$row['Seq_in_index']] = $row['Column_name'] . '(' . $row['Sub_part'] . ')'; else $indexes[$row['Key_name']][$row['Seq_in_index']] = $row['Column_name']; } @mysql_free_result($result); // Build the CREATEs for the keys. foreach ($indexes as $keyname => $columns) { // Ensure the columns are in proper order. ksort($columns); $schema_create .= ',' . $crlf . ' ' . $keyname . ' (' . implode($columns, ', ') . ')'; } // Now just get the comment and type... (MyISAM, etc.) $result = db_query(" SHOW TABLE STATUS LIKE '" . strtr($tableName, array('_' => '\\_', '%' => '\\%')) . "'", false, false); $row = @mysql_fetch_assoc($result); @mysql_free_result($result); // Probably MyISAM.... and it might have a comment. $schema_create .= $crlf . ') TYPE=' . (isset($row['Type']) ? $row['Type'] : $row['Engine']) . ($row['Comment'] != '' ? ' COMMENT="' . $row['Comment'] . '"' : ''); return $schema_create; } ?>