sources.db.mysql.inc 15 KB

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