phpmydumper-1.10/000755 000765 000024 00000000000 11272114406 014262 5ustar00mickystaff000000 000000 phpmydumper-1.10/example1.php000644 000765 000024 00000002506 11271706436 016523 0ustar00mickystaff000000 000000 dropTable = true; // Dump DROP TABLE statement, default: true $dump->createTable = true; // Dump CREATE TABLE statement, default: true $dump->tableData = true; // Dump table data, default: true $dump->expInsert = false; // Dump expanded INSERT statements, default: false $dump->hexValue = false; // Dump strings as hex values, default: false $dump->phpMyAdmin = true; // Formats dump file like phpMyAdmin export, default: true $dump->utf8 = true; // Uses UTF-8 connection with MySQL server, default: true $dump->autoincrement = false; // Dump AUTO_INCREMENT statement using older MySQL servers, default: false $dump->doDump(); ?>phpmydumper-1.10/example2.php000644 000765 000024 00000002376 11271706436 016531 0ustar00mickystaff000000 000000 dropTable = true; // Dump DROP TABLE statement, default: true $dump->createTable = true; // Dump CREATE TABLE statement, default: true $dump->tableData = true; // Dump table data, default: true $dump->expInsert = false; // Dump expanded INSERT statements, default: false $dump->hexValue = false; // Dump strings as hex values, default: false $dump->phpMyAdmin = true; // Formats dump file like phpMyAdmin export, default: true $dump->utf8 = true; // Uses UTF-8 connection with MySQL server, default: true $dump->autoincrement = false; // Dump AUTO_INCREMENT statement using older MySQL servers, default: false $dump->doDump(); echo $dump->filestream; ?>phpmydumper-1.10/phpMyDumper.php000755 000765 000024 00000022413 11272114221 017245 0ustar00mickystaff000000 000000 connection = $connection; $this->compress = $compress; $this->hexValue = false; $this->dropTable = true; $this->createTable = true; $this->tableData = true; $this->expInsert = false; $this->phpMyAdmin = true; $this->utf8 = true; $this->autoincrement = false; $this->outputTofile = ( $filepath!='' ) ? true : false; if ( $this->outputTofile && !$this->setOutputFile($filepath) ) { //if filepath is null, then we want stream return false; } return $this->setDatabase($db); } /** * Sets the database to work on * @param string $db The database name */ function setDatabase($db){ $this->database = $db; if ( !@mysql_select_db($this->database) ) return false; return true; } /** * Sets the output file * @param string $filepath The file where the dump will be written */ function setOutputFile($filepath) { if ( $this->isWritten ) return false; echo "Creating file '".$filepath."': "; $this->filename = $filepath; $this->file = $this->openFile($this->filename); echo " DONE!\n"; return $this->file; } /** * Writes to file all the selected database tables structure with SHOW CREATE TABLE * @param string $table The table name */ function getTableStructure($table) { // Header $structure = "\n-- --------------------------------------------------------\n"; $structure .= "-- \n"; $structure .= "-- Table structure for table `$table`\n"; $structure .= "-- \n\n"; // Dump Structure if ( $this->dropTable ) $structure .= "DROP TABLE IF EXISTS `$table`;\n"; $records = @mysql_query("SHOW CREATE TABLE `$table`",$this->connection); if ( @mysql_num_rows($records) == 0 ) return false; while ( $record = @mysql_fetch_assoc($records) ) { $structure .= $record['Create Table']; } $records = @mysql_query("SHOW TABLE STATUS LIKE '$table'",$this->connection); while ( $row = @mysql_fetch_assoc($records) ) { if ($this->autoincrement AND $row['Name']==$table AND $row['Auto_increment']!='') { $structure .= " AUTO_INCREMENT=".$row['Auto_increment']; } } $structure .= ";\n"; $this->saveToFile($this->file,$structure); } /** * Writes to file the $table's data * @param string $table The table name * @param boolean $hexValue It defines if the output is base 16 or not */ function getTableData($table,$hexValue = true) { // Header $data = "\n-- --------------------------------------------------------\n"; $data .= "-- \n"; $data .= "-- Dumping data for table `$table`\n"; $data .= "-- \n\n"; // Field names if ($this->expInsert || $this->hexValue) { $records = @mysql_query("SHOW FIELDS FROM `$table`",$this->connection); $num_fields = @mysql_num_rows($records); if ( $num_fields == 0 ) return false; $hexField = array(); $insertStatement = "INSERT INTO `$table` ("; $selectStatement = "SELECT "; for ($x = 0; $x < $num_fields; $x++) { $record = @mysql_fetch_assoc($records); if ( ($hexValue) && ($this->isTextValue($record['Type'])) ) { $selectStatement .= 'HEX(`'.$record['Field'].'`)'; $hexField [$x] = true; } else $selectStatement .= '`'.$record['Field'].'`'; $insertStatement .= '`'.$record['Field'].'`'; $insertStatement .= ", "; $selectStatement .= ", "; } $insertStatement = @substr($insertStatement,0,-2).') VALUES'; $selectStatement = @substr($selectStatement,0,-2).' FROM `'.$table.'`'; } if (!$this->expInsert) $insertStatement = "INSERT INTO `$table` VALUES"; if (!$this->hexValue) $selectStatement = "SELECT * FROM $table"; // Dump data $records = @mysql_query($selectStatement,$this->connection); $num_rows = @mysql_num_rows($records); $num_fields = @mysql_num_fields($records); $procent = 0; for ($i = 1; $i <= $num_rows; $i++) { $data .= $insertStatement; $record = @mysql_fetch_assoc($records); $data .= ' ('; for ($j = 0; $j < $num_fields; $j++) { $field_name = @mysql_field_name($records, $j); if (is_null($record[$field_name])) { $data .= "NULL"; } else { if ( isset($hexField[$j]) && (@strlen($record[$field_name]) > 0) ) { $data .= "0x".$record[$field_name]; } else { $data .= '\''.@str_replace('\"','"',@mysql_escape_string($record[$field_name])).'\''; } } $data .= ','; } $data = @substr($data,0,-1).");\n"; $this->saveToFile($this->file,$data); $data = ''; } } /** * Writes to file all the selected database tables structure * @return boolean */ function getDatabaseStructure() { $records = @mysql_query('SHOW TABLES',$this->connection); if ( @mysql_num_rows($records) == 0 ) return false; while ( $record = @mysql_fetch_row($records) ) { echo "Exporting table structure for '".$record[0]."': "; $this->getTableStructure($record[0]); echo " DONE!\n"; } return true; } /** * Writes to file all the selected database tables data * @param boolean $hexValue It defines if the output is base-16 or not */ function getDatabaseData($hexValue = true) { $records = @mysql_query('SHOW TABLES',$this->connection); if ( @mysql_num_rows($records) == 0 ) return false; while ( $record = @mysql_fetch_row($records) ) { if ($this->filename) echo "Exporting table data for '".$record[0]."': "; $this->getTableData($record[0],$hexValue); if ($this->filename) echo " DONE!\n"; } } /** * Writes to file all the selected database tables data * @param boolean $hexValue It defines if the output is base-16 or not */ function getDatabaseStructureData($hexValue = true){ $records = @mysql_query('SHOW TABLES',$this->connection); if ( @mysql_num_rows($records) == 0 ) return false; while ( $record = @mysql_fetch_row($records) ) { if ( $this->createTable) { if ($this->filename) echo "Exporting table structure for '".$record[0]."': "; $this->getTableStructure($record[0]); if ($this->filename) echo " DONE!\n"; } if ( $this->tableData) { if ($this->filename) echo "Exporting table data for '".$record[0]."': "; $this->getTableData($record[0],$hexValue); if ($this->filename) echo " DONE!\n"; } } } /** * Writes the selected database to file */ function doDump() { if ( !$this->setDatabase($this->database) ) return false; if ( $this->utf8 ) { $encoding = @mysql_query("SET NAMES 'utf8'", $this->connection); } $cur_time=date("Y-m-d H:i"); $server_info=mysql_get_server_info(); $this->saveToFile($this->file,"-- Generation Time: $cur_time\n"); $this->saveToFile($this->file,"-- MySQL Server Version: $server_info\n"); $this->saveToFile($this->file,"-- Database: `$this->database`\n"); if ($this->phpMyAdmin) { $this->getDatabaseStructureData($this->hexValue); } else { if ( $this->createTable ) $this->getDatabaseStructure(); if ( $this->tableData ) $this->getDatabaseData($this->hexValue); } if ($this->outputTofile){ $this->closeFile($this->file); return true; } else { return $this->filestream; } } /** * @access private */ function isTextValue($field_type) { switch ($field_type) { case "tinytext": case "text": case "mediumtext": case "longtext": case "binary": case "varbinary": case "tinyblob": case "blob": case "mediumblob": case "longblob": return True; break; default: return False; } } /** * @access private */ function openFile($filename) { $file = false; if ( $this->compress ) $file = @gzopen($filename, "w9"); else $file = @fopen($filename, "w"); return $file; } /** * @access private */ function saveToFile($file, $data) { if ($this->outputTofile){ if ( $this->compress ) @gzwrite($file, $data); else @fwrite($file, $data); $this->isWritten = true; } else { $this->saveToStream($data); } } /** * @access private */ function saveToStream($data) { $this->filestream .= $data; } /** * @access private */ function closeFile($file) { if ( $this->compress ) @gzclose($file); else @fclose($file); } } ?>phpmydumper-1.10/README000644 000765 000024 00000000704 11271430452 015144 0ustar00mickystaff000000 000000 phpMyDumper ----------- Version: 1.10 Copyright (c) 2009 by Micky Holdorf Holdorf.dk/Software - micky.holdorf@gmail.com GNU Public License http://opensource.org/licenses/gpl-license.php Introduction ------------ phpMyDumper is a php class, that can be used for exporting databases and tables from a MySQL server. This is useful if you want to do automatic backup of your databases using a cronjob and you do not have access to the mysqldump command. phpmydumper-1.10/VERSION000644 000765 000024 00000001500 11272114221 015321 0ustar00mickystaff000000 000000 phpMyDumper ----------- Version: 1.10 Copyright (c) 2009 by Micky Holdorf Holdorf.dk/Software - micky.holdorf@gmail.com GNU Public License http://opensource.org/licenses/gpl-license.php Version History --------------- 1.10: (2009-10-28) Support for UTF-8 connections New option to dump AUTO_INCREMENT in table structure when used with older MySQL versions Reports progress information when creating sql file New example of how to use the stream feature Dumps MySQL server version 1.00: (2009-03-13) Based on MySQLDump by Daniele ViganĂ², http://www.creativefactory.it Uses SHOW CREATE TABLES to get table structures Adds AUTO_INCREMENT in table structures Adds option to formats dump file like phpMyAdmin export Adds options to dump DROP TABLE and CREATE TABLE statements Adds options to dump data and expanded INSERT statements