exclude_tables) ? $settings->exclude_tables : array(); $nodata = !empty($settings->nodata_tables) ? $settings->nodata_tables : array(); if ($file->open(TRUE)) { $file->write(_backup_migrate_get_sql_file_header_mysql()); $alltables = _backup_migrate_get_tables_mysql(); foreach ($alltables as $table) { if (_backup_migrate_check_timeout()) { return FALSE; } if ($table['Name'] && !isset($exclude[$table['Name']])) { $file->write(_backup_migrate_get_table_structure_sql_mysql($table)); $lines++; if (!in_array($table['Name'], $nodata)) { $lines += _backup_migrate_dump_table_data_sql_to_file($file, $table); } } } $file->write(_backup_migrate_get_sql_file_footer_mysql()); $file->close(); return $lines; } else { return FALSE; } } /** * Restore the db from a valid backup file. */ function backup_migrate_restore_db_from_file_mysql($file, $settings) { $num = 0; if ($file->open()) { // Read one line at a time and run the query. while ($line = $file->read()) { if (_backup_migrate_check_timeout()) { return FALSE; } $line = trim($line); if ($line) { // Use the helper instead of the api function to avoid substitution of '{' etc. _db_query($line); $num++; } } // Close the file with fclose/gzclose. $file->close(); } else { drupal_set_message(t("Unable to open file %file to restore database", array("%file" => $file->filepath())), 'error'); $num = FALSE; } return $num; } /** * Get the sql for the structure of the given table. */ function _backup_migrate_get_table_structure_sql_mysql($table) { $out = ""; $result = db_query("SHOW CREATE TABLE `". $table['Name'] ."`"); if ($create = db_fetch_array($result)) { $out .= "DROP TABLE IF EXISTS `". $table['Name'] ."`;\n"; $out .= strtr($create['Create Table'], "\n", " "); if ($table['Auto_increment']) { $out .= " AUTO_INCREMENT=". $table['Auto_increment']; } $out .= ";\n"; } return $out; } /** * Get the sql to insert the data for a given table */ function _backup_migrate_dump_table_data_sql_to_file($file, $table) { $rows_per_line = variable_get('backup_migrate_data_rows_per_line', 30); $bytes_per_line = variable_get('backup_migrate_data_bytes_per_line', 2000); $lines = 0; $data = db_query("SELECT * FROM `". $table['Name'] ."`"); $rows = $bytes = 0; $line = array(); while ($row = db_fetch_array($data)) { // DB Escape the values. $items = array(); foreach ($row as $key => $value) { $items[] = is_null($value) ? "null" : "'". db_escape_string($value) ."'"; } // If there is a row to be added. if ($items) { // Start a new line if we need to. if ($rows == 0) { $file->write("INSERT INTO `". $table['Name'] ."` VALUES "); $bytes = $rows = 0; } // Otherwise add a comma to end the previous entry. else { $file->write(","); } // Write the data itself. $sql = implode(',', $items); $file->write('('. $sql .')'); $bytes += strlen($sql); $rows++; // Finish the last line if we've added enough items if ($rows >= $rows_per_line || $bytes >= $bytes_per_line) { $file->write(";\n"); $lines++; $bytes = $rows = 0; } } } // Finish any unfinished insert statements. if ($rows > 0) { $file->write(";\n"); $lines++; } return $lines; } /** * The header for the top of the sql dump file. These commands set the connection * character encoding to help prevent encoding conversion issues. */ function _backup_migrate_get_sql_file_header_mysql() { return "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=NO_AUTO_VALUE_ON_ZERO */; SET NAMES utf8; "; } /** * The footer of the sql dump file. */ function _backup_migrate_get_sql_file_footer_mysql() { return " /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; "; } /** * Get a list of tables in the db. */ function _backup_migrate_get_tables_mysql() { $out = array(); // get auto_increment values and names of all tables $tables = db_query("show table status"); while ($table = db_fetch_array($tables)) { $out[$table['Name']] = $table; } return $out; } /** * Get the list of table names. */ function _backup_migrate_get_table_names_mysql() { $out = array(); foreach (_backup_migrate_get_tables_mysql() as $table) { $out[$table['Name']] = $table['Name']; } return $out; }