db.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @file
  4. * General database dump/restore code for Backup and Migrate.
  5. */
  6. /**
  7. * Restore from a previously backed up files. File must be a decompressed SQL file.
  8. */
  9. function backup_migrate_db_restore($source, $file, $settings) {
  10. $num = 0;
  11. if ($type = _backup_migrate_db_get_db_type($source->dest_url['scheme'])) {
  12. // Switch to a different db if specified.
  13. _backup_migrate_db_switch_db($source->get_location());
  14. backup_migrate_filters_invoke_all('pre_restore', $file, $settings);
  15. // Open the file using the file wrapper. Check that the dump is of the right type (allow .sql for legacy reasons).
  16. if ($file->type_id() !== $type && $file->type_id() !== 'sql') {
  17. _backup_migrate_message("Unable to restore from file %file because it is of an unknown file type.", array("%file" => $file->filepath()), 'error');
  18. }
  19. else {
  20. // Dump the database.
  21. $function = 'backup_migrate_restore_db_from_file_'. $type;
  22. if (function_exists($function)) {
  23. $num = $function($file, $settings);
  24. }
  25. }
  26. backup_migrate_filters_invoke_all('post_restore', $file, $settings, $num);
  27. // Switch back to the previous db.
  28. _backup_migrate_db_switch_db();
  29. }
  30. return $num;
  31. }
  32. /**
  33. * Build the database dump file. Takes a list of tables to exclude and some formatting options.
  34. */
  35. function backup_migrate_db_backup($source, $file, $settings) {
  36. $success = FALSE;
  37. if ($type = _backup_migrate_db_get_db_type($source->dest_url['scheme'])) {
  38. $file->push_type($type);
  39. // Switch to a different db if specified.
  40. _backup_migrate_db_switch_db($source->get_location());
  41. backup_migrate_filters_invoke_all('pre_backup', $source, $file, $settings);
  42. // Dump the database.
  43. $function = 'backup_migrate_backup_db_to_file_'. $type;
  44. if (function_exists($function)) {
  45. $success = $function($file, $settings);
  46. }
  47. backup_migrate_filters_invoke_all('post_backup', $source, $file, $settings, $success);
  48. // Switch back to the previous db.
  49. _backup_migrate_db_switch_db();
  50. }
  51. return $success ? $file : FALSE;
  52. }
  53. /**
  54. * Get the list of table names.
  55. */
  56. function _backup_migrate_get_table_names($source) {
  57. $out = array();
  58. if ($source && $type = _backup_migrate_db_get_db_type($source->dest_url['scheme'])) {
  59. $function = '_backup_migrate_get_table_names_'. $type;
  60. if (function_exists($function)) {
  61. $out = $function();
  62. }
  63. }
  64. return $out;
  65. }
  66. /**
  67. * Tables to ingore altogether. None by default.
  68. */
  69. function _backup_migrate_default_exclude_tables() {
  70. return array();
  71. }
  72. /**
  73. * Return the default tables whose data can be ignored. These tables mostly contain
  74. * info which can be easily reproducted (such as cache or search index)
  75. * but also tables which can become quite bloated but are not necessarily extremely
  76. * important to back up or migrate during development (such ass access log and watchdog)
  77. */
  78. function _backup_migrate_default_structure_only_tables() {
  79. $core = array(
  80. 'cache',
  81. 'cache_admin_menu',
  82. 'cache_browscap',
  83. 'cache_content',
  84. 'cache_filter',
  85. 'cache_calendar_ical',
  86. 'cache_location',
  87. 'cache_menu',
  88. 'cache_page',
  89. 'cache_reptag',
  90. 'cache_views',
  91. 'cache_views_data',
  92. 'cache_block',
  93. 'cache_update',
  94. 'cache_form',
  95. 'cache_bootstrap',
  96. 'cache_field',
  97. 'cache_image',
  98. 'cache_path',
  99. 'sessions',
  100. 'search_dataset',
  101. 'search_index',
  102. 'search_keywords_log',
  103. 'search_total',
  104. 'watchdog',
  105. 'accesslog',
  106. 'devel_queries',
  107. 'devel_times',
  108. );
  109. $alltables = array_merge($core, module_invoke_all('devel_caches'));
  110. return $alltables;
  111. }
  112. /**
  113. * Switch to the db described by the DB URL, or back to previous if none selected.
  114. */
  115. function _backup_migrate_db_switch_db($in_url = NULL) {
  116. static $db_stack = array();
  117. global $db_url;
  118. // If no DB URL is specified, pop the previous one and set to it.
  119. if ($in_url === NULL && $db_stack) {
  120. db_set_active(array_pop($db_stack));
  121. }
  122. // If there is a valid DB URL, switch to it.
  123. if ($in_url) {
  124. // Make the db_url into an array if needed.
  125. if (!is_array($db_url)) {
  126. $db_url = array('default' => $db_url);
  127. }
  128. // Add the new db to the db_url array.
  129. $db_url[$in_url] = $in_url;
  130. // Switch to the new db and push the old one on the stack
  131. $db_stack[] = db_set_active($in_url);
  132. }
  133. }
  134. /**
  135. * Get the appropriate db type for file inclusion and calling the right function.
  136. */
  137. function _backup_migrate_db_get_db_type($scheme) {
  138. static $type = NULL;
  139. if ($type === NULL) {
  140. switch ($scheme) {
  141. case 'mysql':
  142. case 'mysqli':
  143. $type = 'mysql';
  144. backup_migrate_include('db.mysql');
  145. break;
  146. default:
  147. $type = false;
  148. break;
  149. }
  150. if (!$type) {
  151. if ($scheme) {
  152. _backup_migrate_message("Backup and migrate does not support @type databases.", array("@type" => $scheme), 'error');
  153. }
  154. else {
  155. _backup_migrate_message("Backup and migrate does not support this database type.", array(), 'error');
  156. }
  157. }
  158. }
  159. return $type;
  160. }