destinations.db.inc

  1. nittany7 modules/contrib/backup_migrate/includes/destinations.db.inc
  2. mooc7 modules/contrib/backup_migrate/includes/destinations.db.inc

Functions to handle the direct to database destination.

Classes

Namesort descending Description
backup_migrate_destination_db A destination type for saving to a database server.

File

modules/contrib/backup_migrate/includes/destinations.db.inc
View source
  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. $tables = $this->get_table_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" => $tables,
  109. "#default_value" => $settings['exclude_tables'],
  110. "#description" => t("The selected tables will not be added to the backup file."),
  111. );
  112. $form['nodata_tables'] = array(
  113. "#type" => "select",
  114. "#multiple" => TRUE,
  115. "#title" => t("Exclude the data from the following tables"),
  116. "#options" => $tables,
  117. "#default_value" => $settings['nodata_tables'],
  118. "#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."),
  119. );
  120. $form['utils_lock_tables'] = array(
  121. '#type' => 'checkbox',
  122. '#title' => t('Lock tables during backup'),
  123. '#default_value' => !empty($settings['utils_lock_tables']) ? $settings['utils_lock_tables'] : NULL,
  124. '#description' => t('This can help reduce data corruption, but will make your site unresponsive.'),
  125. );
  126. return $form;
  127. }
  128. /**
  129. * Backup from this source.
  130. */
  131. function backup_to_file($file, $settings) {
  132. $file->push_type($this->get_file_type_id());
  133. backup_migrate_filters_invoke_all('pre_backup', $this, $file, $settings);
  134. //$this->lock_tables($settings);
  135. // Switch to a different db if specified.
  136. $success = $this->_backup_db_to_file($file, $settings);
  137. //$this->unlock_tables($settings);
  138. backup_migrate_filters_invoke_all('post_backup', $this, $file, $settings, $success);
  139. return $success ? $file : FALSE;
  140. }
  141. /**
  142. * Restore to this source.
  143. */
  144. function restore_from_file($file, &$settings) {
  145. $num = 0;
  146. $type = $this->get_file_type_id();
  147. // Open the file using the file wrapper. Check that the dump is of the right type (allow .sql for legacy reasons).
  148. if ($file->type_id() !== $this->get_file_type_id() && $file->type_id() !== 'sql') {
  149. _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');
  150. }
  151. else {
  152. backup_migrate_filters_invoke_all('pre_restore', $file, $settings);
  153. // Restore the database.
  154. $num = $this->_restore_db_from_file($file, $settings);
  155. $settings->performed_action = $num ? t('%num SQL commands executed.', array('%num' => $num)) : '';
  156. backup_migrate_filters_invoke_all('post_restore', $file, $settings, $num);
  157. }
  158. return $num;
  159. }
  160. /**
  161. * Get the db connection for the specified db.
  162. */
  163. function _get_db_connection() {
  164. if (!$this->connection) {
  165. $target = $key = '';
  166. $parts = explode(':', $this->get_id());
  167. // One of the predefined databases (set in settings.php)
  168. if ($parts[0] == 'db') {
  169. $key = empty($parts[1]) ? 'default' : $parts[1];
  170. $target = empty($parts[2]) ? 'default' : $parts[2];
  171. }
  172. // Another db url.
  173. else {
  174. // If the url is specified build it into a connection info array.
  175. if (!empty($this->dest_url)) {
  176. $info = array(
  177. 'driver' => empty($this->dest_url['scheme']) ? NULL : $this->dest_url['scheme'],
  178. 'host' => empty($this->dest_url['host']) ? NULL : $this->dest_url['host'],
  179. 'port' => empty($this->dest_url['port']) ? NULL : $this->dest_url['port'],
  180. 'username' => empty($this->dest_url['user']) ? NULL : $this->dest_url['user'],
  181. 'password' => empty($this->dest_url['pass']) ? NULL : $this->dest_url['pass'],
  182. 'database' => empty($this->dest_url['path']) ? NULL : $this->dest_url['path'],
  183. );
  184. $key = uniqid('backup_migrate_tmp_');
  185. $target = 'default';
  186. Database::addConnectionInfo($key, $target, $info);
  187. }
  188. // No database selected. Assume the default.
  189. else {
  190. $key = $target = 'default';
  191. }
  192. }
  193. if ($target && $key) {
  194. try {
  195. $this->connection = Database::getConnection($target, $key);
  196. }
  197. catch (PDOException $e) {
  198. return NULL;
  199. }
  200. }
  201. }
  202. return $this->connection;
  203. }
  204. /**
  205. * Backup the databases to a file.
  206. */
  207. function _backup_db_to_file($file, $settings) {
  208. // Must be overridden.
  209. }
  210. /**
  211. * Backup the databases to a file.
  212. */
  213. function _restore_db_from_file($file, $settings) {
  214. // Must be overridden.
  215. }
  216. /**
  217. * Get a list of tables in the database.
  218. */
  219. function get_table_names() {
  220. // Must be overridden.
  221. $out = $this->_get_table_names();
  222. return $out;
  223. }
  224. /**
  225. * Get a list of tables in the database.
  226. */
  227. function _get_table_names() {
  228. // Must be overridden.
  229. return array();
  230. }
  231. /**
  232. * Lock the database in anticipation of a backup.
  233. */
  234. function lock_tables($settings) {
  235. if ($settings->filters['utils_lock_tables']) {
  236. $tables = array();
  237. foreach ($this->get_table_names() as $table) {
  238. // There's no need to lock excluded or structure only tables because it doesn't matter if they change.
  239. if (empty($settings->filters['exclude_tables']) || !in_array($table, (array)$settings->filters['exclude_tables'])) {
  240. $tables[] = $table;
  241. }
  242. }
  243. $this->_lock_tables($tables);
  244. }
  245. }
  246. /**
  247. * Lock the list of given tables in the database.
  248. */
  249. function _lock_tables($tables) {
  250. // Must be overridden.
  251. }
  252. /**
  253. * Unlock any tables that have been locked.
  254. */
  255. function unlock_tables($settings) {
  256. if ($settings->filters['utils_lock_tables']) {
  257. $this->_unlock_tables();
  258. }
  259. }
  260. /**
  261. * Unlock the list of given tables in the database.
  262. */
  263. function _unlock_tables($tables) {
  264. // Must be overridden.
  265. }
  266. /**
  267. * Get the file type for to backup this destination to.
  268. */
  269. function get_file_type_id() {
  270. return 'sql';
  271. }
  272. }