destinations.db.inc 10 KB

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