db.mysql.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Get the sql dump file.
  4. *
  5. * Returns a list of sql commands, one command per line.
  6. * That makes it easier to import without loading the whole file into memory.
  7. * The files are a little harder to read, but human-readability is not a priority
  8. */
  9. function backup_migrate_backup_db_to_file_mysql($file, $settings) {
  10. $lines = 0;
  11. $exclude = !empty($settings->exclude_tables) ? $settings->exclude_tables : array();
  12. $nodata = !empty($settings->nodata_tables) ? $settings->nodata_tables : array();
  13. if ($file->open(TRUE)) {
  14. $file->write(_backup_migrate_get_sql_file_header_mysql());
  15. $alltables = _backup_migrate_get_tables_mysql();
  16. foreach ($alltables as $table) {
  17. if (_backup_migrate_check_timeout()) {
  18. return FALSE;
  19. }
  20. if ($table->name && !isset($exclude[$table->name])) {
  21. $file->write(_backup_migrate_get_table_structure_sql_mysql($table));
  22. $lines++;
  23. if (!in_array($table->name, $nodata)) {
  24. $lines += _backup_migrate_dump_table_data_sql_to_file($file, $table);
  25. }
  26. }
  27. }
  28. $file->write(_backup_migrate_get_sql_file_footer_mysql());
  29. $file->close();
  30. return $lines;
  31. }
  32. else {
  33. return FALSE;
  34. }
  35. }
  36. /**
  37. * Restore the db from a valid backup file.
  38. */
  39. function backup_migrate_restore_db_from_file_mysql($file, $settings) {
  40. $num = 0;
  41. if ($file->open()) {
  42. // Read one line at a time and run the query.
  43. while ($line = $file->read()) {
  44. if (_backup_migrate_check_timeout()) {
  45. return FALSE;
  46. }
  47. $line = trim($line);
  48. if ($line) {
  49. // Use the helper instead of the api function to avoid substitution of '{' etc.
  50. Database::getConnection()->prepare($line)->execute();
  51. $num++;
  52. }
  53. }
  54. // Close the file with fclose/gzclose.
  55. $file->close();
  56. }
  57. else {
  58. drupal_set_message(t("Unable to open file %file to restore database", array("%file" => $file->filepath())), 'error');
  59. $num = FALSE;
  60. }
  61. return $num;
  62. }
  63. /**
  64. * Get the sql for the structure of the given table.
  65. */
  66. function _backup_migrate_get_table_structure_sql_mysql($table) {
  67. $out = "";
  68. $result = db_query("SHOW CREATE TABLE `". $table->name ."`");
  69. if ($create = db_fetch_array($result)) {
  70. $out .= "DROP TABLE IF EXISTS `". $table->name ."`;\n";
  71. $out .= strtr($create['create table'], array("\n" => " ", '"' => '`'));
  72. if ($table->auto_increment) {
  73. $out .= " AUTO_INCREMENT=". $table->auto_increment;
  74. }
  75. $out .= ";\n";
  76. }
  77. return $out;
  78. }
  79. /**
  80. * Get the sql to insert the data for a given table
  81. */
  82. function _backup_migrate_dump_table_data_sql_to_file($file, $table) {
  83. $lines = 0;
  84. $data = db_query("SELECT * FROM `". $table->name ."`");
  85. while ($row = db_fetch_array($data)) {
  86. $items = array();
  87. foreach ($row as $key => $value) {
  88. $items[] = is_null($value) ? "null" : "'". mysql_escape_string($value) ."'";
  89. }
  90. if ($items) {
  91. $file->write("INSERT INTO `". $table->name ."` VALUES (". implode(",", $items) .");\n");
  92. $lines++;
  93. }
  94. }
  95. return $lines;
  96. }
  97. /**
  98. * The header for the top of the sql dump file. These commands set the connection
  99. * character encoding to help prevent encoding conversion issues.
  100. */
  101. function _backup_migrate_get_sql_file_header_mysql() {
  102. return "
  103. /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
  104. /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
  105. /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
  106. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
  107. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
  108. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=NO_AUTO_VALUE_ON_ZERO */;
  109. SET NAMES utf8;
  110. ";
  111. }
  112. /**
  113. * The footer of the sql dump file.
  114. */
  115. function _backup_migrate_get_sql_file_footer_mysql() {
  116. return "
  117. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
  118. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
  119. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
  120. /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
  121. /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
  122. /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
  123. ";
  124. }
  125. /**
  126. * Get a list of tables in the db.
  127. */
  128. function _backup_migrate_get_tables_mysql() {
  129. $out = array();
  130. // get auto_increment values and names of all tables
  131. $tables = db_query("show table status");
  132. foreach ($tables as $table) {
  133. $out[$table->name] = $table;
  134. }
  135. return $out;
  136. }
  137. /**
  138. * Get the list of table names.
  139. */
  140. function _backup_migrate_get_table_names_mysql() {
  141. $out = array();
  142. foreach (_backup_migrate_get_tables_mysql() as $key => $table) {
  143. $out[$key] = $table->name;
  144. }
  145. return $out;
  146. }