sources.db.inc 9.6 KB

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