destinations.db.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * @file
  4. * Functions to handle the direct to database destination.
  5. */
  6. /**
  7. * A destination type for saving to a database server.
  8. *
  9. * @ingroup backup_migrate_destinations
  10. */
  11. class backup_migrate_destination_db extends backup_migrate_destination_remote {
  12. var $supported_ops = array('scheduled backup', 'manual backup', 'configure', 'source');
  13. var $db_target = 'default';
  14. var $connection = null;
  15. function type_name() {
  16. return t("Database");
  17. }
  18. /**
  19. * Save the info by importing it into the database.
  20. */
  21. function save_file($file, $settings) {
  22. backup_migrate_include('files');
  23. // Set the source_id to the destination_id in the settings since for a restore, the source_id is the
  24. // database that gets restored to.
  25. $settings->set_source($this->get_id());
  26. // Restore the file to the source database.
  27. $file = backup_migrate_perform_restore($this->get_id(), $file, $settings);
  28. return $file;
  29. }
  30. /**
  31. * Destination configuration callback.
  32. */
  33. function edit_form() {
  34. $form = parent::edit_form();
  35. $form['scheme']['#title'] = t('Database type');
  36. // $form['scheme']['#options'] = array($GLOBALS['db_type'] => $GLOBALS['db_type']);
  37. $form['scheme']['#description'] = t('The type of the database. Drupal only supports one database type at a time, so this must be the same as the current database type.');
  38. $form['path']['#title'] = t('Database name');
  39. $form['path']['#description'] = t('The name of the database. The database must exist, it will not be created for you.');
  40. $form['user']['#description'] = t('Enter the name of a user who has write access to the database.');
  41. return $form;
  42. }
  43. /**
  44. * Validate the configuration form. Make sure the db info is valid.
  45. */
  46. function edit_form_validate($form, &$form_state) {
  47. if (!preg_match('/[a-zA-Z0-9_\$]+/', $form_state['values']['path'])) {
  48. form_set_error('path', t('The database name is not valid.'));
  49. }
  50. parent::edit_form_validate($form, $form_state);
  51. }
  52. /**
  53. * Get the form for the settings for this destination.
  54. *
  55. * Return the default tables whose data can be ignored. These tables mostly contain
  56. * info which can be easily reproducted (such as cache or search index)
  57. * but also tables which can become quite bloated but are not necessarily extremely
  58. * important to back up or migrate during development (such ass access log and watchdog)
  59. */
  60. function backup_settings_default() {
  61. $core = array(
  62. 'cache',
  63. 'cache_admin_menu',
  64. 'cache_browscap',
  65. 'cache_content',
  66. 'cache_filter',
  67. 'cache_calendar_ical',
  68. 'cache_location',
  69. 'cache_menu',
  70. 'cache_page',
  71. 'cache_reptag',
  72. 'cache_views',
  73. 'cache_views_data',
  74. 'cache_block',
  75. 'cache_update',
  76. 'cache_form',
  77. 'cache_bootstrap',
  78. 'cache_field',
  79. 'cache_image',
  80. 'cache_path',
  81. 'sessions',
  82. 'search_dataset',
  83. 'search_index',
  84. 'search_keywords_log',
  85. 'search_total',
  86. 'watchdog',
  87. 'accesslog',
  88. 'devel_queries',
  89. 'devel_times',
  90. );
  91. $nodata_tables = array_merge($core, module_invoke_all('devel_caches'));
  92. return array(
  93. 'nodata_tables' => $nodata_tables,
  94. 'exclude_tables' => array(),
  95. 'utils_lock_tables' => FALSE,
  96. );
  97. }
  98. /**
  99. * Get the form for the backup settings for this destination.
  100. */
  101. function backup_settings_form($settings) {
  102. $objects = $this->get_object_names();
  103. $form['#description'] = t("You may omit specific tables, or specific table data from the backup file. Only omit data that you know you will not need such as cache data, or tables from other applications. Excluding tables can break your Drupal install, so <strong>do not change these settings unless you know what you're doing</strong>.");
  104. $form['exclude_tables'] = array(
  105. "#type" => "select",
  106. "#multiple" => TRUE,
  107. "#title" => t("Exclude the following tables altogether"),
  108. "#options" => $objects,
  109. "#default_value" => $settings['exclude_tables'],
  110. "#description" => t("The selected tables will not be added to the backup file."),
  111. );
  112. $tables = $this->get_table_names();
  113. $form['nodata_tables'] = array(
  114. "#type" => "select",
  115. "#multiple" => TRUE,
  116. "#title" => t("Exclude the data from the following tables"),
  117. "#options" => $tables,
  118. "#default_value" => $settings['nodata_tables'],
  119. "#description" => t("The selected tables will have their structure backed up but not their contents. This is useful for excluding cache data to reduce file size."),
  120. );
  121. $form['utils_lock_tables'] = array(
  122. '#type' => 'checkbox',
  123. '#title' => t('Lock tables during backup'),
  124. '#default_value' => !empty($settings['utils_lock_tables']) ? $settings['utils_lock_tables'] : NULL,
  125. '#description' => t('This can help reduce data corruption, but will make your site unresponsive.'),
  126. );
  127. return $form;
  128. }
  129. /**
  130. * Backup from this source.
  131. */
  132. function backup_to_file($file, $settings) {
  133. $file->push_type($this->get_file_type_id());
  134. backup_migrate_filters_invoke_all('pre_backup', $this, $file, $settings);
  135. //$this->lock_tables($settings);
  136. // Switch to a different db if specified.
  137. $success = $this->_backup_db_to_file($file, $settings);
  138. //$this->unlock_tables($settings);
  139. backup_migrate_filters_invoke_all('post_backup', $this, $file, $settings, $success);
  140. return $success ? $file : FALSE;
  141. }
  142. /**
  143. * Restore to this source.
  144. */
  145. function restore_from_file($file, &$settings) {
  146. $num = 0;
  147. $type = $this->get_file_type_id();
  148. // Open the file using the file wrapper. Check that the dump is of the right type (allow .sql for legacy reasons).
  149. if ($file->type_id() !== $this->get_file_type_id() && $file->type_id() !== 'sql') {
  150. _backup_migrate_message("Unable to restore from file %file because a %type file can't be restored to this database.", array("%file" => $file->filepath(), '%type' => $file->type_id()), 'error');
  151. }
  152. else {
  153. backup_migrate_filters_invoke_all('pre_restore', $file, $settings);
  154. // Restore the database.
  155. $num = $this->_restore_db_from_file($file, $settings);
  156. $settings->performed_action = $num ? t('%num SQL commands executed.', array('%num' => $num)) : '';
  157. backup_migrate_filters_invoke_all('post_restore', $file, $settings, $num);
  158. }
  159. return $num;
  160. }
  161. /**
  162. * Get the db connection for the specified db.
  163. */
  164. function _get_db_connection() {
  165. if (!$this->connection) {
  166. $target = $key = '';
  167. $parts = explode(':', $this->get_id());
  168. // One of the predefined databases (set in settings.php)
  169. if ($parts[0] == 'db') {
  170. $key = empty($parts[1]) ? 'default' : $parts[1];
  171. $target = empty($parts[2]) ? 'default' : $parts[2];
  172. }
  173. // Another db url.
  174. else {
  175. // If the url is specified build it into a connection info array.
  176. if (!empty($this->dest_url)) {
  177. $info = array(
  178. 'driver' => empty($this->dest_url['scheme']) ? NULL : $this->dest_url['scheme'],
  179. 'host' => empty($this->dest_url['host']) ? NULL : $this->dest_url['host'],
  180. 'port' => empty($this->dest_url['port']) ? NULL : $this->dest_url['port'],
  181. 'username' => empty($this->dest_url['user']) ? NULL : $this->dest_url['user'],
  182. 'password' => empty($this->dest_url['pass']) ? NULL : $this->dest_url['pass'],
  183. 'database' => empty($this->dest_url['path']) ? NULL : $this->dest_url['path'],
  184. );
  185. $key = uniqid('backup_migrate_tmp_');
  186. $target = 'default';
  187. Database::addConnectionInfo($key, $target, $info);
  188. }
  189. // No database selected. Assume the default.
  190. else {
  191. $key = $target = 'default';
  192. }
  193. }
  194. if ($target && $key) {
  195. $this->connection = Database::getConnection($target, $key);
  196. }
  197. }
  198. return $this->connection;
  199. }
  200. /**
  201. * Backup the databases to a file.
  202. */
  203. function _backup_db_to_file($file, $settings) {
  204. // Must be overridden.
  205. }
  206. /**
  207. * Backup the databases to a file.
  208. */
  209. function _restore_db_from_file($file, $settings) {
  210. // Must be overridden.
  211. }
  212. /**
  213. * Get a list of objects in the database.
  214. */
  215. function get_object_names() {
  216. // Must be overridden.
  217. $out = $this->_get_table_names();
  218. if (method_exists($this, '_get_view_names')) {
  219. $out += $this->_get_view_names();
  220. }
  221. return $out;
  222. }
  223. /**
  224. * Get a list of tables in the database.
  225. */
  226. function get_table_names() {
  227. // Must be overridden.
  228. $out = $this->_get_table_names();
  229. return $out;
  230. }
  231. /**
  232. * Get a list of tables in the database.
  233. */
  234. function _get_table_names() {
  235. // Must be overridden.
  236. return array();
  237. }
  238. /**
  239. * Lock the database in anticipation of a backup.
  240. */
  241. function lock_tables($settings) {
  242. if ($settings->filters['utils_lock_tables']) {
  243. $tables = array();
  244. foreach ($this->get_table_names() as $table) {
  245. // There's no need to lock excluded or structure only tables because it doesn't matter if they change.
  246. if (empty($settings->filters['exclude_tables']) || !in_array($table, (array)$settings->filters['exclude_tables'])) {
  247. $tables[] = $table;
  248. }
  249. }
  250. $this->_lock_tables($tables);
  251. }
  252. }
  253. /**
  254. * Lock the list of given tables in the database.
  255. */
  256. function _lock_tables($tables) {
  257. // Must be overridden.
  258. }
  259. /**
  260. * Unlock any tables that have been locked.
  261. */
  262. function unlock_tables($settings) {
  263. if ($settings->filters['utils_lock_tables']) {
  264. $this->_unlock_tables();
  265. }
  266. }
  267. /**
  268. * Unlock the list of given tables in the database.
  269. */
  270. function _unlock_tables($tables) {
  271. // Must be overridden.
  272. }
  273. /**
  274. * Get the file type for to backup this destination to.
  275. */
  276. function get_file_type_id() {
  277. return 'sql';
  278. }
  279. }