destinations.db.mysql.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. backup_migrate_include('destinations.db');
  3. /**
  4. * @file
  5. * Functions to handle the direct to database destination.
  6. */
  7. /**
  8. * A destination type for saving to a database server.
  9. *
  10. * @ingroup backup_migrate_destinations
  11. */
  12. class backup_migrate_destination_db_mysql extends backup_migrate_destination_db {
  13. function type_name() {
  14. return t("MySQL Database");
  15. }
  16. /**
  17. * Return a list of backup filetypes.
  18. */
  19. function file_types() {
  20. return array(
  21. "sql" => array(
  22. "extension" => "sql",
  23. "filemime" => "text/x-sql",
  24. "backup" => TRUE,
  25. "restore" => TRUE,
  26. ),
  27. "mysql" => array(
  28. "extension" => "mysql",
  29. "filemime" => "text/x-sql",
  30. "backup" => TRUE,
  31. "restore" => TRUE,
  32. ),
  33. );
  34. }
  35. /**
  36. * Declare any mysql databases defined in the settings.php file as a possible destination.
  37. */
  38. function destinations() {
  39. $out = array();
  40. global $databases;
  41. foreach ((array)$databases as $db_key => $target) {
  42. foreach ((array)$target as $tgt_key => $info) {
  43. // Only mysql/mysqli supported by this destination.
  44. $key = $db_key . ':' . $tgt_key;
  45. if ($info['driver'] === 'mysql') {
  46. $url = $info['driver'] . '://' . $info['username'] . ':' . $info['password'] . '@' . $info['host'] . (isset($info['port']) ? ':' . $info['port'] : '') . '/' . $info['database'];
  47. if ($destination = backup_migrate_create_destination('mysql', array('url' => $url))) {
  48. // Treat the default database differently because it is probably the only one available.
  49. if ($key == 'default:default') {
  50. $destination->set_id('db');
  51. $destination->set_name(t('Default Database'));
  52. // Dissalow backing up to the default database because that's confusing and potentially dangerous.
  53. $destination->remove_op('scheduled backup');
  54. $destination->remove_op('manual backup');
  55. }
  56. else {
  57. $destination->set_id('db:'. $key);
  58. $destination->set_name($key .": ". $destination->get_display_location());
  59. }
  60. $out[$destination->get_id()] = $destination;
  61. }
  62. }
  63. }
  64. }
  65. return $out;
  66. }
  67. /**
  68. * Get the file type for to backup this destination to.
  69. */
  70. function get_file_type_id() {
  71. return 'mysql';
  72. }
  73. /**
  74. * Get the form for the backup settings for this destination.
  75. */
  76. function backup_settings_form($settings) {
  77. $form = parent::backup_settings_form($settings);
  78. $form['use_mysqldump'] = array(
  79. "#type" => "checkbox",
  80. "#title" => t("Use mysqldump command"),
  81. "#default_value" => !empty($settings['use_mysqldump']),
  82. "#description" => t("Use the mysqldump command line tool if available. This can be faster for large databases but will not work on all servers. EXPERIMENTAL"),
  83. );
  84. return $form;
  85. }
  86. /**
  87. * Backup the databases to a file.
  88. *
  89. * Returns a list of sql commands, one command per line.
  90. * That makes it easier to import without loading the whole file into memory.
  91. * The files are a little harder to read, but human-readability is not a priority
  92. */
  93. function _backup_db_to_file($file, $settings) {
  94. if (!empty($settings->filters['use_mysqldump']) && $this->_backup_db_to_file_mysqldump($file, $settings)) {
  95. return TRUE;
  96. }
  97. $lines = 0;
  98. $exclude = !empty($settings->filters['exclude_tables']) ? $settings->filters['exclude_tables'] : array();
  99. $nodata = !empty($settings->filters['nodata_tables']) ? $settings->filters['nodata_tables'] : array();
  100. if ($file->open(TRUE)) {
  101. $file->write($this->_get_sql_file_header());
  102. $alltables = $this->_get_tables();
  103. foreach ($alltables as $table) {
  104. if (_backup_migrate_check_timeout()) {
  105. return FALSE;
  106. }
  107. if ($table['name'] && !isset($exclude[$table['name']])) {
  108. $file->write($this->_get_table_structure_sql($table));
  109. $lines++;
  110. if (!in_array($table['name'], $nodata)) {
  111. $lines += $this->_dump_table_data_sql_to_file($file, $table);
  112. }
  113. }
  114. }
  115. $file->write($this->_get_sql_file_footer());
  116. $file->close();
  117. return $lines;
  118. }
  119. else {
  120. return FALSE;
  121. }
  122. }
  123. /**
  124. * Backup the databases to a file using the mysqldump command.
  125. */
  126. function _backup_db_to_file_mysqldump($file, $settings) {
  127. $success = FALSE;
  128. $nodata_tables = array();
  129. $alltables = $this->_get_tables();
  130. $command = 'mysqldump --result-file=%file --opt -Q --host=%host --port=%port --user=%user --password=%pass %db';
  131. $args = array(
  132. '%file' => $file->filepath(),
  133. '%host' => $this->dest_url['host'],
  134. '%port' => !empty($this->dest_url['port']) ? $this->dest_url['port'] : '3306',
  135. '%user' => $this->dest_url['user'],
  136. '%pass' => $this->dest_url['pass'],
  137. '%db' => $this->dest_url['path'],
  138. );
  139. // Ignore the excluded and no-data tables.
  140. if (!empty($settings->filters['exclude_tables'])) {
  141. $db = $this->dest_url['path'];
  142. foreach ((array)$settings->filters['exclude_tables'] as $table) {
  143. if (isset($alltables[$table])) {
  144. $command .= ' --ignore-table='. $db .'.'. $table;
  145. }
  146. }
  147. foreach ((array)$settings->filters['nodata_tables'] as $table) {
  148. if (isset($alltables[$table])) {
  149. $nodata_tables[] = $table;
  150. $command .= ' --ignore-table='. $db .'.'. $table;
  151. }
  152. }
  153. }
  154. $success = backup_migrate_exec($command, $args);
  155. // Get the nodata tables.
  156. if ($success && !empty($nodata_tables)) {
  157. $tables = implode(' ', array_unique($nodata_tables));
  158. $command = "mysqldump --no-data --opt -Q --host=%host --port=%port --user=%user --password=%pass %db $tables >> %file";
  159. $success = backup_migrate_exec($command, $args);
  160. }
  161. return $success;
  162. }
  163. /**
  164. * Backup the databases to a file.
  165. */
  166. function _restore_db_from_file($file, $settings) {
  167. $num = 0;
  168. if ($file->open() && $conn = $this->_get_db_connection()) {
  169. // Read one line at a time and run the query.
  170. while ($line = $this->_read_sql_command_from_file($file)) {
  171. if (_backup_migrate_check_timeout()) {
  172. return FALSE;
  173. }
  174. if ($line) {
  175. // Prepeare and exexute the statement instead of the api function to avoid substitution of '{' etc.
  176. $stmt = $conn->prepare($line);
  177. $stmt->execute();
  178. $num++;
  179. }
  180. }
  181. // Close the file with fclose/gzclose.
  182. $file->close();
  183. }
  184. else {
  185. drupal_set_message(t("Unable to open file %file to restore database", array("%file" => $file->filepath())), 'error');
  186. $num = FALSE;
  187. }
  188. return $num;
  189. }
  190. /**
  191. * Read a multiline sql command from a file.
  192. *
  193. * Supports the formatting created by mysqldump, but won't handle multiline comments.
  194. */
  195. function _read_sql_command_from_file($file) {
  196. $out = '';
  197. while ($line = $file->read()) {
  198. $first2 = substr($line, 0, 2);
  199. $first3 = substr($line, 0, 2);
  200. // Ignore single line comments. This function doesn't support multiline comments or inline comments.
  201. if ($first2 != '--' && ($first2 != '/*' || $first3 == '/*!')) {
  202. $out .= ' ' . trim($line);
  203. // If a line ends in ; or */ it is a sql command.
  204. if (substr($out, strlen($out) - 1, 1) == ';') {
  205. return trim($out);
  206. }
  207. }
  208. }
  209. return trim($out);
  210. }
  211. /**
  212. * Get a list of tables in the database.
  213. */
  214. function _get_table_names() {
  215. $out = array();
  216. foreach ($this->_get_tables() as $table) {
  217. $out[$table['name']] = $table['name'];
  218. }
  219. return $out;
  220. }
  221. /**
  222. * Lock the list of given tables in the database.
  223. */
  224. function _lock_tables($tables) {
  225. if ($tables) {
  226. $tables_escaped = array();
  227. foreach ($tables as $table) {
  228. $tables_escaped[] = '`'. db_escape_table($table) .'` WRITE';
  229. }
  230. $this->query('LOCK TABLES '. implode(', ', $tables_escaped));
  231. }
  232. }
  233. /**
  234. * Unlock all tables in the database.
  235. */
  236. function _unlock_tables($settings) {
  237. $this->query('UNLOCK TABLES');
  238. }
  239. /**
  240. * Get a list of tables in the db.
  241. */
  242. function _get_tables() {
  243. $out = array();
  244. // get auto_increment values and names of all tables
  245. $tables = $this->query("show table status", array(), array('fetch' => PDO::FETCH_ASSOC));
  246. foreach ($tables as $table) {
  247. // Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed.
  248. // See: http://drupal.org/node/1171866
  249. $table = array_change_key_case($table);
  250. $out[$table['name']] = $table;
  251. }
  252. return $out;
  253. }
  254. /**
  255. * Get the sql for the structure of the given table.
  256. */
  257. function _get_table_structure_sql($table) {
  258. $out = "";
  259. $result = $this->query("SHOW CREATE TABLE `". $table['name'] ."`", array(), array('fetch' => PDO::FETCH_ASSOC));
  260. foreach ($result as $create) {
  261. // Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed.
  262. // See: http://drupal.org/node/1171866
  263. $create = array_change_key_case($create);
  264. $out .= "DROP TABLE IF EXISTS `". $table['name'] ."`;\n";
  265. // Remove newlines and convert " to ` because PDO seems to convert those for some reason.
  266. $out .= strtr($create['create table'], array("\n" => ' ', '"' => '`'));
  267. if ($table['auto_increment']) {
  268. $out .= " AUTO_INCREMENT=". $table['auto_increment'];
  269. }
  270. $out .= ";\n";
  271. }
  272. return $out;
  273. }
  274. /**
  275. * Get the sql to insert the data for a given table
  276. */
  277. function _dump_table_data_sql_to_file($file, $table) {
  278. $rows_per_line = variable_get('backup_migrate_data_rows_per_line', 30);
  279. $bytes_per_line = variable_get('backup_migrate_data_bytes_per_line', 2000);
  280. $lines = 0;
  281. $data = $this->query("SELECT * FROM `". $table['name'] ."`", array(), array('fetch' => PDO::FETCH_ASSOC));
  282. $rows = $bytes = 0;
  283. // Escape backslashes, PHP code, special chars
  284. $search = array('\\', "'", "\x00", "\x0a", "\x0d", "\x1a");
  285. $replace = array('\\\\', "''", '\0', '\n', '\r', '\Z');
  286. $line = array();
  287. foreach ($data as $row) {
  288. // DB Escape the values.
  289. $items = array();
  290. foreach ($row as $key => $value) {
  291. $items[] = is_null($value) ? "null" : "'". str_replace($search, $replace, $value) ."'";
  292. }
  293. // If there is a row to be added.
  294. if ($items) {
  295. // Start a new line if we need to.
  296. if ($rows == 0) {
  297. $file->write("INSERT INTO `". $table['name'] ."` VALUES ");
  298. $bytes = $rows = 0;
  299. }
  300. // Otherwise add a comma to end the previous entry.
  301. else {
  302. $file->write(",");
  303. }
  304. // Write the data itself.
  305. $sql = implode(',', $items);
  306. $file->write('('. $sql .')');
  307. $bytes += strlen($sql);
  308. $rows++;
  309. // Finish the last line if we've added enough items
  310. if ($rows >= $rows_per_line || $bytes >= $bytes_per_line) {
  311. $file->write(";\n");
  312. $lines++;
  313. $bytes = $rows = 0;
  314. }
  315. }
  316. }
  317. // Finish any unfinished insert statements.
  318. if ($rows > 0) {
  319. $file->write(";\n");
  320. $lines++;
  321. }
  322. return $lines;
  323. }
  324. /**
  325. * Get the db connection for the specified db.
  326. */
  327. function _get_db_connection() {
  328. if (!$this->connection) {
  329. $this->connection = parent::_get_db_connection();
  330. // Set the sql mode because the default is ANSI,TRADITIONAL which is not aware of collation or storage engine.
  331. $this->connection->exec("SET sql_mode=''");
  332. }
  333. return $this->connection;
  334. }
  335. /**
  336. * Run a db query on this destination's db.
  337. */
  338. function query($query, $args = array(), $options = array()) {
  339. if ($conn = $this->_get_db_connection()) {
  340. return $conn->query($query, $args, $options);
  341. }
  342. }
  343. /**
  344. * The header for the top of the sql dump file. These commands set the connection
  345. * character encoding to help prevent encoding conversion issues.
  346. */
  347. function _get_sql_file_header() {
  348. return "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
  349. /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
  350. /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
  351. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
  352. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
  353. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=NO_AUTO_VALUE_ON_ZERO */;
  354. SET NAMES utf8;
  355. ";
  356. }
  357. /**
  358. * The footer of the sql dump file.
  359. */
  360. function _get_sql_file_footer() {
  361. return "
  362. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
  363. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
  364. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
  365. /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
  366. /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
  367. /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
  368. ";
  369. }
  370. }