sync.sql.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. require_once DRUSH_BASE_PATH . '/commands/core/rsync.core.inc';
  3. /**
  4. * Sql sync init function. Bootstrap either the source or the
  5. * destination site. At least one of the sites
  6. * must be local for this to work; if both sites are remote,
  7. * then it clearly will not be possible to bootstrap to
  8. * either of them. If both are local, the source site is preferred.
  9. */
  10. function drush_sql_sync_init($source = NULL, $destination = NULL) {
  11. // Preflight destination in case it defines the alias used by the source
  12. _drush_sitealias_get_record($destination);
  13. // After preflight, get source and destination settings
  14. $source_settings = drush_sitealias_get_record($source);
  15. $destination_settings = drush_sitealias_get_record($destination);
  16. // Insure that we have database records for the source and destination
  17. // alias records. sitealias_get_databases_from_record will cache the
  18. // database info inside the alias records, and drush_sitealias_set_alias_context
  19. // will copy the database record into the 'alias' context. We do not
  20. // actually use the databases record at this time.
  21. sitealias_get_databases_from_record($source_settings);
  22. sitealias_get_databases_from_record($destination_settings);
  23. // Bootstrap to the source sites being sync'ed if it is local.
  24. // This allows modules enabled in the site to participate in the
  25. // sql-sync hook functions (e.g. to add sanitization operations, etc.).
  26. // If the source is remote and the destination is local, then we
  27. // will determine the sanitization operations after the database
  28. // has been copied.
  29. if (!drush_get_option('deferred-sanitization', FALSE) && drush_get_option(array('sanitize', 'destination-sanitize'), FALSE)) {
  30. $bootstrapped = drush_bootstrap_max_to_sitealias($source_settings);
  31. if ($bootstrapped) {
  32. drush_command_invoke_all('drush_sql_sync_sanitize', $source);
  33. }
  34. else {
  35. drush_set_option('deferred-sanitization', TRUE);
  36. }
  37. }
  38. // By default, sql-sync will do an ordered dump.
  39. // Set --no-ordered-dump to override.
  40. if (!drush_get_option('no-ordered-dump', FALSE)) {
  41. drush_set_option('ordered-dump', TRUE);
  42. }
  43. return TRUE;
  44. }
  45. /**
  46. * Sql sync sanitization function. This hook function will sanitize usernames and
  47. * passwords in the user table when the --sanitize option is used. It is
  48. * also an example of how to write a database sanitizer for sql sync.
  49. *
  50. * To write your own sync hook function, define mymodule_drush_sql_sync_sanitize()
  51. * and follow the form of this function to add your own database
  52. * sanitization operations via the register post-sync op function;
  53. * @see drush_sql_register_post_sync_op(). This is the only thing that the
  54. * sync hook function needs to do; sql-sync takes care of the rest.
  55. *
  56. * The function below has a lot of logic to process user preferences and
  57. * generate the correct SQL regardless of whether Postgres, Mysql,
  58. * Drupal 6 or Drupal 7 is in use. A simpler sanitize function that
  59. * always used default values and only worked with Drupal 6 + mysql
  60. * appears in the drush.api.php. @see hook_drush_sql_sync_sanitize().
  61. */
  62. function sql_drush_sql_sync_sanitize($site) {
  63. $site_settings = drush_sitealias_get_record($site);
  64. $user_table_updates = array();
  65. $message_list = array();
  66. // Sanitize passwords.
  67. $newpassword = drush_get_option(array('sanitize-password', 'destination-sanitize-password'), 'password');
  68. if ($newpassword != 'no') {
  69. $major_version = drush_drupal_major_version();
  70. $pw_op = "";
  71. // In Drupal 6, passwords are hashed via the MD5 algorithm.
  72. if ($major_version == 6) {
  73. $pw_op = "MD5('$newpassword')";
  74. }
  75. // In Drupal 7, passwords are hashed via a more complex algorithm,
  76. // available via the user_hash_password function.
  77. elseif ($major_version >= 7) {
  78. include_once DRUPAL_ROOT . '/includes/password.inc';
  79. include_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  80. $hash = user_hash_password($newpassword);
  81. $pw_op = "'$hash'";
  82. }
  83. if (!empty($pw_op)) {
  84. $user_table_updates[] = "pass = $pw_op";
  85. $message_list[] = "passwords";
  86. }
  87. }
  88. // Sanitize email addresses.
  89. $newemail = drush_get_option(array('sanitize-email', 'destination-sanitize-email'), 'user+%uid@localhost');
  90. if ($newemail != 'no') {
  91. if (strpos($newemail, '%') !== FALSE) {
  92. // We need a different sanitization query for Postgres and Mysql.
  93. $db_driver = $site_settings['databases']['default']['default']['driver'];
  94. if ($db_driver == 'pgsql') {
  95. $email_map = array('%uid' => "' || uid || '", '%mail' => "' || replace(mail, '@', '_') || '", '%name' => "' || replace(name, ' ', '_') || '");
  96. $newmail = "'" . str_replace(array_keys($email_map), array_values($email_map), $newemail) . "'";
  97. }
  98. else {
  99. $email_map = array('%uid' => "', uid, '", '%mail' => "', replace(mail, '@', '_'), '", '%name' => "', replace(name, ' ', '_'), '");
  100. $newmail = "concat('" . str_replace(array_keys($email_map), array_values($email_map), $newemail) . "')";
  101. }
  102. }
  103. $user_table_updates[] = "mail = $newmail";
  104. $message_list[] = 'email addresses';
  105. }
  106. if (!empty($user_table_updates)) {
  107. $sanitize_query = "update users set " . implode(', ', $user_table_updates) . " where uid > 0;";
  108. drush_sql_register_post_sync_op('user-email', dt('Reset !message in user table', array('!message' => implode(' and ', $message_list))), $sanitize_query);
  109. }
  110. }
  111. function drush_sql_sync($source = NULL, $destination = NULL) {
  112. $source_settings = drush_sitealias_get_record($source);
  113. $destination_settings = drush_sitealias_get_record($destination);
  114. // Check to see if this is an sql-sync multiple command (multiple sources and multiple destinations)
  115. $is_multiple = drush_do_multiple_command('sql-sync', $source_settings, $destination_settings);
  116. if ($is_multiple === FALSE) {
  117. // Evaluate the source and destination specifications into options.
  118. // The options from the 'source-*' and 'target-*' aliases are set
  119. // in a drush context that has a lower priority than the command-line
  120. // options; this allows command-line options to override the default
  121. // values specified in a site-alias.
  122. drush_sitealias_set_alias_context($source_settings, 'source-');
  123. drush_sitealias_set_alias_context($destination_settings, 'target-');
  124. // Get the options for the source and target databases
  125. $source_db_url = _drush_sql_get_spec_from_options('source-', FALSE);
  126. // The host may have special ssh requirements
  127. $source_remote_ssh_options = drush_get_option('source-ssh-options');
  128. // rsync later will also have to know this option
  129. $source_rsync_options = array('ssh-options' => $source_remote_ssh_options);
  130. $target_db_url = _drush_sql_get_spec_from_options('target-', FALSE);
  131. // The host may have special ssh requirements
  132. $target_remote_ssh_options = drush_get_option('target-ssh-options');
  133. // rsync later will also have to know this option
  134. $target_rsync_options = array('ssh-options' => $target_remote_ssh_options);
  135. if (empty($source_db_url)) {
  136. return drush_set_error('DRUSH_DATABASE_NOT_FOUND', dt('Error: no database record could be found for !source', array('!source' => $source)));
  137. }
  138. if (empty($target_db_url)) {
  139. return drush_set_error('DRUSH_DATABASE_NOT_FOUND', dt('Error: no database record could be found for !destination', array('!destination' => $destination)));
  140. }
  141. // Set up the result file and the remote file.
  142. // If the result file is not set, then create a temporary file.
  143. // If the remote file is not set, use the same name for the remote
  144. // and local files and hope for the best.
  145. $source_dump = drush_sql_dump_file($source_db_url, 'source-');
  146. $target_dump = drush_sql_dump_file($target_db_url, 'target-');
  147. $use_temp_files = drush_get_option('temp');
  148. // Only use one dump file if both the source and the target are on the local machine
  149. if (!isset($source_db_url['remote-host']) && !isset($target_db_url['remote-host'])) {
  150. if ((!$target_db_url['dump-is-temp']) && ($source_db_url['dump-is-temp'])) {
  151. $source_dump = $target_dump;
  152. $source_db_url['dump-is-temp'] = FALSE;
  153. }
  154. else {
  155. $target_dump = $source_dump;
  156. $target_db_url['dump-is-temp'] = $source_db_url['dump-is-temp'];
  157. }
  158. $local_file = $source_dump;
  159. }
  160. else {
  161. // If one of the systems is remote, then set the --remove-source-files
  162. // rsync option if the source dump file is temporary. This will get
  163. // rsync to clean up after us automatically; useful if the source is remote.
  164. if ($source_db_url['dump-is-temp']) {
  165. $source_rsync_options['remove-source-files'] = TRUE;
  166. }
  167. // Set $local_file to whichever side of the operation is local, or make
  168. // a temporary file if both source and target are remote.
  169. if (!isset($source_db_url['remote-host'])) {
  170. $local_file = $source_dump;
  171. }
  172. elseif (!isset($target_db_url['remote-host'])) {
  173. $local_file = $target_dump;
  174. }
  175. else {
  176. $local_file = drush_tempnam($source_db_url['database'] . ($source_db_url['database'] == $target_db_url['database'] ? '' : '-to-' . $target_db_url['database']) . '.sql.');
  177. }
  178. }
  179. // If source is remote, then use ssh to dump the database and then rsync to local machine
  180. // If source is local, call drush_sql_dump to dump the database to local machine
  181. // In either case, the '--no-dump' option will cause the sql-dump step to be skipped, and
  182. // we will import from the existing local file (first using rsync to fetch it if it does not exist)
  183. //
  184. // No dump affects both local and remote sql-dumps; it prevents drush sql-sync
  185. // from calling sql-dump when the local cache file is newer than the cache threshhold
  186. // No sync affects the remote sql-dump; it will prevent drush sql-sync from
  187. // rsyncing the local sql-dump file with the remote sql-dump file.
  188. $no_sync = drush_get_option(array('no-sync', 'source-no-sync'));
  189. $no_dump = drush_get_option(array('no-dump', 'source-no-dump'));
  190. $no_cache = drush_get_option(array('no-cache', 'source-no-cache'));
  191. if (!isset($no_cache)) {
  192. $cache = drush_get_option(array('cache', 'source-cache'));
  193. if (!isset($cache)) {
  194. $cache = 24; // Default cache is 24 hours if nothing else is specified.
  195. }
  196. }
  197. // If the 'cache' option is set, then we will set the no-dump option iff the
  198. // target file exists and its modification date is less than "cache" hours.
  199. if (isset($cache)) {
  200. if (file_exists($local_file) && (filesize($local_file) > 0)) {
  201. if ((time() - filemtime($local_file)) < ($cache * 60 * 60)) {
  202. drush_log(dt('Modification time of local dump file !file is less than !cache hours old. Use the --no-cache option to force a refresh.', array('!file' => $local_file, '!cache' => $cache)), 'warning');
  203. $no_dump = TRUE;
  204. $no_sync = TRUE;
  205. }
  206. else {
  207. drush_log(dt('Local sql cache file exists but is greater than !cache hours old.', array('!cache' => $cache)));
  208. }
  209. }
  210. else {
  211. drush_log('Local sql cache file does not exist.');
  212. }
  213. }
  214. $table_selection = array();
  215. if (!isset($no_dump)) {
  216. $table_selection = drush_sql_get_table_selection();
  217. }
  218. // Prompt for confirmation. This is destructive.
  219. if (!drush_get_context('DRUSH_SIMULATE')) {
  220. // Check to see if we are using a temporary file in a situation
  221. // where the user did not specify "--temp".
  222. if (($source_db_url['dump-is-temp'] || $target_db_url['dump-is-temp']) && (!isset($use_temp_files)) && (isset($source_db_url['remote-host']) || isset($target_db_url['remote-host']))) {
  223. drush_print(dt('WARNING: Using temporary files to store and transfer sql-dump. It is recommended that you specify --source-dump and --target-dump options on the command line, or set \'%dump\' or \'%dump-dir\' in the path-aliases section of your site alias records. This facilitates fast file transfer via rsync.'));
  224. }
  225. if (array_key_exists('tables', $table_selection) && (count($table_selection['tables']) > 0)) {
  226. drush_print();
  227. drush_print(dt(' Only the following tables will be transferred: !list', array('!list' => implode(',', $table_selection['tables']))));
  228. }
  229. elseif (!empty($table_selection)) {
  230. $skip_tables_list = implode(',', $table_selection['skip'] + $table_selection['structure']);
  231. if(!empty($skip_tables_list)) {
  232. drush_print();
  233. drush_print(dt(' The following tables will be skipped: !list', array('!list' => $skip_tables_list)));
  234. }
  235. }
  236. // If there are multiple destinations, then
  237. // prompt once here and suppress the warning message
  238. // and the normal confirmation below.
  239. if (array_key_exists('site-list', $destination_settings)) {
  240. drush_print();
  241. drush_print(dt('You are about to sync the database from !source, overwriting all of the following targets:', array('!source' => $source)));
  242. foreach ($destination_settings['site-list'] as $one_destination) {
  243. drush_print(dt(' !target', array('!target' => $one_destination)));
  244. }
  245. }
  246. else {
  247. drush_print();
  248. $txt_source = (isset($source_db_url['remote-host']) ? $source_db_url['remote-host'] . '/' : '') . $source_db_url['database'];
  249. $txt_destination = (isset($target_db_url['remote-host']) ? $target_db_url['remote-host'] . '/' : '') . $target_db_url['database'];
  250. drush_print(dt("You will destroy data from !target and replace with data from !source.", array('!source' => $txt_source, '!target' => $txt_destination)));
  251. }
  252. // If any sanitization operations are to be done, then get the
  253. // sanitization messages and print them as part of the confirmation.
  254. // If --sanitize was specified but there were no sanitize messages,
  255. // then warn that sanitization operations will be accumulated and
  256. // processed after the sync completes.
  257. $messages = _drush_sql_get_post_sync_messages();
  258. if ($messages) {
  259. drush_print();
  260. drush_print($messages);
  261. }
  262. else if (drush_get_option('deferred-sanitization', FALSE) && !drush_get_option('confirm-sanitizations', FALSE)) {
  263. drush_print();
  264. drush_print("WARNING: --sanitize was specified, but deferred (e.g. the source site is remote). The sanitization operations will be determined after the database is copied to the local system and will be run without further confirmation. Run with --confirm-sanitizations to force confirmation after the sync.");
  265. }
  266. // TODO: actually make the backup if desired.
  267. drush_print();
  268. drush_print(dt("You might want to make a backup first, using the sql-dump command.\n"));
  269. if (!drush_confirm(dt('Do you really want to continue?'))) {
  270. return drush_user_abort();
  271. }
  272. }
  273. if (isset($source_db_url['remote-host'])) {
  274. $source_remote_user = drush_get_option('source-remote-user');
  275. $source_at = '';
  276. if (isset($source_remote_user)) {
  277. $source_at ='@';
  278. $source_remote_pass = drush_get_option('source-remote-pass') ? ':' . drush_get_option('source-remote-pass') : '';
  279. }
  280. if (!isset($no_dump)) {
  281. $source_intermediate = $source_dump;
  282. $mv_intermediate = '';
  283. // If we are doing a remote dump and the source is not a temporary file,
  284. // then first dump to a temporary file and move it to the specified file after
  285. // the dump is complete. This will reduce contention during simultaneous dumps
  286. // from different users sharing the same dump file.
  287. if (!$source_db_url['dump-is-temp']) {
  288. $source_intermediate = $source_dump . '-' . date("U");
  289. $mv_intermediate = '; mv -f ' . $source_intermediate . ' ' . $source_dump;
  290. }
  291. drush_set_option('result-file', $source_intermediate);
  292. list($dump_exec, $dump_file) = drush_sql_build_dump_command($table_selection, $source_db_url);
  293. $dump_exec .= $mv_intermediate;
  294. if (isset($cache) && !$source_db_url['dump-is-temp']) {
  295. // Inject some bash commands to remotely test the modification date of the target file
  296. // if the cache option is set.
  297. $dump_exec = 'if [ ! -s ' . $source_dump . ' ] || [ $((`date "+%s"`-`stat --format="%Y" ' . $source_dump . '`)) -gt ' . ($cache * 60 * 60) . ' ] ; then ' . $dump_exec . '; fi';
  298. }
  299. $dump_exec = "ssh $source_remote_ssh_options $source_remote_user$source_at" . $source_db_url['remote-host'] . " " . escapeshellarg($dump_exec);
  300. }
  301. }
  302. else {
  303. if (!isset($no_dump)) {
  304. drush_set_option('result-file', $local_file);
  305. list($dump_exec, $dump_file) = drush_sql_build_dump_command($table_selection, $source_db_url);
  306. }
  307. $no_sync = TRUE;
  308. }
  309. // Call sql-dump, either on the local machine or remotely via ssh, as appropriate.
  310. if (!empty($dump_exec)) {
  311. drush_op_system($dump_exec);
  312. // TODO: IF FAILURE THEN ABORT
  313. }
  314. // If the sql-dump was remote, then rsync the file over to the local machine.
  315. if (!isset($no_sync)) {
  316. // If the source file is a temporary file, then we will have rsync
  317. // delete it for us (remove-source-files option set above).
  318. if (!drush_core_call_rsync($source_remote_user . $source_at . $source_db_url['remote-host'] . ':' . $source_dump, $local_file, $source_rsync_options)) {
  319. return FALSE;
  320. }
  321. }
  322. // We will handle lists of destination sites differently from
  323. // single source-to-destination syncs.
  324. if (array_key_exists('site-list', $destination_settings)) {
  325. // Insure that we will not dump the source sql database
  326. // repeatedly, but will instead re-use it each time through
  327. // the redispatch loop.
  328. drush_set_option('no-dump', TRUE);
  329. drush_set_option('no-sync', TRUE);
  330. drush_set_option('source-dump', $source_dump);
  331. // Call sql-sync for each destination to push the $source_dump
  332. // to each target in turn.
  333. foreach ($destination_settings['site-list'] as $one_destination) {
  334. drush_do_command_redispatch('sql-sync', array($source, $one_destination));
  335. }
  336. }
  337. else {
  338. // Prior to database import, we will generate a "create database" command
  339. // if the '--create-db' option was specified. Note that typically the
  340. // web server user will not have permissions to create a database; to specify
  341. // a different user to use with the create db command, the '--db-su' option
  342. // may be used.
  343. // Under postgres, "alter role username with createdb;" will give create database
  344. // permissions to the specified user if said user was not created with this right.
  345. $pre_import_commands = '';
  346. $create_db = drush_get_option('create-db');
  347. if (isset($create_db)) {
  348. $create_db_su = drush_sql_su($target_db_url);
  349. $db_su_connect = _drush_sql_connect($create_db_su);
  350. $pre_import_sql = drush_sql_build_createdb_sql($target_db_url);
  351. $pre_import_commands = sprintf('echo "%s" | %s; ', $pre_import_sql, $db_su_connect);
  352. }
  353. // Generate the import command
  354. $import_command = _drush_sql_connect($target_db_url);
  355. switch (_drush_sql_get_scheme($target_db_url)) {
  356. case 'mysql':
  357. $import_command .= ' --silent';
  358. break;
  359. case 'pgsql':
  360. $import_command .= ' -q';
  361. break;
  362. }
  363. // If destination is remote, then use rsync to push the database, then use ssh to import the database
  364. // If destination is local, then just import the database locally
  365. if (isset($target_db_url['remote-host'])) {
  366. $target_remote_user = drush_get_option('target-remote-user');
  367. $target_at = '';
  368. if (isset($target_remote_user)) {
  369. $target_at ='@';
  370. $target_remote_pass = drush_get_option('target-remote-pass') ? ':' . drush_get_option('target-remote-pass') : '';
  371. }
  372. if (!drush_core_call_rsync($local_file, $target_remote_user . $target_at . $target_db_url['remote-host'] . ':' . $target_dump, $target_rsync_options)) {
  373. return FALSE;
  374. }
  375. $connect_exec = $pre_import_commands . $import_command . ' < ' . $target_dump;
  376. $import_exec = "ssh $target_remote_ssh_options $target_remote_user$target_at" . $target_db_url['remote-host'] . ' ' . escapeshellarg($connect_exec);
  377. // delete the remote target file if it is a temporary file
  378. if ($target_db_url['dump-is-temp']) {
  379. $import_exec .= '; rm -f ' . escapeshellarg($target_dump);
  380. }
  381. }
  382. else {
  383. $import_exec = $pre_import_commands . $import_command . ' < ' . $local_file;
  384. }
  385. drush_op_system($import_exec);
  386. // After the database is imported into the destination, we
  387. // will check and see if we did not collect sanitization
  388. // operations in drush_sql_sync_init (i.e. because the source
  389. // site was remote), and if the destination site is local,
  390. // then we will call the sanitization hooks now.
  391. // This presumes an important precondition, that the code
  392. // files were sync'ed before the database was sync'ed.
  393. if (drush_get_option('deferred-sanitization', FALSE) && (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_SITE) == FALSE)) {
  394. $bootstrapped = drush_bootstrap_max_to_sitealias($destination_settings);
  395. if ($bootstrapped) {
  396. drush_command_invoke_all('drush_sql_sync_sanitize', $destination);
  397. }
  398. }
  399. }
  400. }
  401. }
  402. /**
  403. * Apply all post-sync operations that were registered in any pre-sync hook.
  404. * Follow the pattern of this function to make your own post-sync hook.
  405. * If changing the database, be sure to also include a pre-sync hook to
  406. * notify the user of the change that will be made. @see drush_sql_pre_sql_sync().
  407. */
  408. function drush_sql_post_sql_sync($source = NULL, $destination = NULL) {
  409. $options = drush_get_context('post-sync-ops');
  410. if (!empty($options)) {
  411. // If 'deferred-sanitization' is set, then we collected the
  412. // sanitization operations -after- the database sync, which
  413. // means they were not confirmed up-front. We will show the
  414. // operations here, but we will not offer an opportunity to
  415. // confirm unless --confirm-sanitizations is specified.
  416. if (drush_get_option('deferred-sanitization', FALSE) || drush_get_option('confirm-sanitizations', FALSE)) {
  417. if (!drush_get_context('DRUSH_SIMULATE')) {
  418. $messages = _drush_sql_get_post_sync_messages();
  419. if ($messages) {
  420. drush_print();
  421. drush_print($messages);
  422. if (drush_get_option('confirm-sanitizations', FALSE)) {
  423. if (!drush_confirm(dt('Do you really want to sanitize?'))) {
  424. // Do not abort or return FALSE; that would trigger a rollback.
  425. // Just skip the sanitizations and signal that all is ok.
  426. drush_log(dt('Sanitizations skipped.'), 'ok');
  427. return TRUE;
  428. }
  429. }
  430. }
  431. }
  432. }
  433. $destination_settings = drush_sitealias_get_record($destination);
  434. $sanitize_query = '';
  435. foreach($options as $id => $data) {
  436. $sanitize_query .= $data['query'] . " ";
  437. }
  438. if ($sanitize_query) {
  439. if (!drush_get_context('DRUSH_SIMULATE')) {
  440. $result = drush_invoke_sitealias_args($destination_settings, "sql-query", array($sanitize_query));
  441. }
  442. else {
  443. drush_print("Executing on $destination: $sanitize_query");
  444. }
  445. }
  446. }
  447. }