sqlsync.drush.inc

  1. 8.0.x commands/sql/sqlsync.drush.inc
  2. 7.x commands/sql/sqlsync.drush.inc
  3. master commands/sql/sqlsync.drush.inc

Functions

File

commands/sql/sqlsync.drush.inc
View source
  1. <?php
  2. use Drush\Log\LogLevel;
  3. /*
  4. * Notes:
  5. * - Drush is required on source and dest (except with --no-dump)
  6. * - Source and Dest can both be remote. Convenient.
  7. * - No special handling for multiple targets. Use --no-dump.
  8. * - We could move the sanitize confirmations earlier if desired.
  9. * - A bit tricky to know where to put your drushrc/alias customizations. Can be local, $source, or $destination. Just add docs?
  10. * - No longer optimizing for efficient rsync. We could add this back if desired.
  11. * -- Always use gzip during sql-dump. Don't use --ordered-dump.
  12. * -- No more 24-hour freshness check for a team of devs.
  13. * - Can we now simplify anything in sitealias.inc or backend.inc?
  14. */
  15. /**
  16. * Implementation of hook_drush_command().
  17. */
  18. function sqlsync_drush_command() {
  19. $items['sql-sync'] = array(
  20. 'description' => 'Copies the database contents from a source site to a target site. Transfers the database dump via rsync.',
  21. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  22. 'drush dependencies' => array('sql', 'core'), // core-rsync.
  23. 'package' => 'sql',
  24. 'examples' => array(
  25. 'drush sql-sync @source @target' => 'Copy the database from the site with the alias "source" to the site with the alias "target".',
  26. 'drush sql-sync prod dev' => 'Copy the database from the site in /sites/prod to the site in /sites/dev (multisite installation).',
  27. ),
  28. 'arguments' => array(
  29. 'source' => 'A site-alias or the name of a subdirectory within /sites whose database you want to copy from.',
  30. 'target' => 'A site-alias or the name of a subdirectory within /sites whose database you want to replace.',
  31. ),
  32. 'required-arguments' => TRUE,
  33. 'options' => array(
  34. 'skip-tables-key' => 'A key in the $skip_tables array. See example.drushrc.php. Optional.',
  35. 'skip-tables-list' => 'A comma-separated list of tables to exclude completely. Optional.',
  36. 'structure-tables-key' => 'A key in the $structure_tables array. See example.drushrc.php. Optional.',
  37. 'structure-tables-list' => 'A comma-separated list of tables to include for structure, but not data. Optional.',
  38. 'tables-key' => 'A key in the $tables array. Optional.',
  39. 'tables-list' => 'A comma-separated list of tables to transfer. Optional.',
  40. // 'cache' => 'Skip dump if result file exists and is less than "cache" hours old. Optional; default is 24 hours.',
  41. // 'no-cache' => 'Do not cache the sql-dump file.',
  42. 'no-dump' => 'Do not dump the sql database; always use an existing dump file.',
  43. 'no-sync' => 'Do not rsync the database dump file from source to target.',
  44. 'source-db-url' => 'Database specification for source system to dump from.',
  45. 'source-remote-port' => 'Override sql database port number in source-db-url. Optional.',
  46. 'source-remote-host' => 'Remote machine to run sql-dump file on. Optional; default is local machine.',
  47. 'source-dump' => 'The destination for the dump file, or the path to the dump file when --no-dump is specified.',
  48. 'source-database' => 'A key in the $db_url (D6) or $databases (D7+) array which provides the data.',
  49. 'source-target' => array(
  50. 'description' => 'A key within the SOURCE database identifying a particular server in the database group.',
  51. 'example-value' => 'key',
  52. // Gets unhidden in help_alter(). We only want to show to D7+ users but have to
  53. // declare it here since this command does not bootstrap fully.
  54. 'hidden' => TRUE,
  55. ),
  56. 'target-db-url' => '',
  57. 'target-remote-port' => '',
  58. 'target-remote-host' => '',
  59. 'target-dump' => 'A path for saving the dump file on target. Mandatory when using --no-sync.',
  60. 'target-database' => 'A key in the $db_url (D6) or $databases (D7+) array which shall receive the data.',
  61. 'target-target' => array(
  62. 'description' => 'Oy. A key within the TARGET database identifying a particular server in the database group.',
  63. 'example-value' => 'key',
  64. // Gets unhidden in help_alter(). We only want to show to D7+ users but have to
  65. // declare it here since this command does not bootstrap fully.
  66. 'hidden' => TRUE,
  67. ),
  68. 'create-db' => 'Create a new database before importing the database dump on the target machine.',
  69. 'db-su' => array(
  70. 'description' => 'Account to use when creating a new database. Optional.',
  71. 'example-value' => 'root',
  72. ),
  73. 'db-su-pw' => array(
  74. 'description' => 'Password for the "db-su" account. Optional.',
  75. 'example-value' => 'pass',
  76. ),
  77. // 'no-ordered-dump' => 'Do not pass --ordered-dump to sql-dump. sql-sync orders the dumpfile by default in order to increase the efficiency of rsync.',
  78. 'sanitize' => 'Obscure email addresses and reset passwords in the user table post-sync.',
  79. ),
  80. 'sub-options' => array(
  81. 'sanitize' => drupal_sanitize_options() + array(
  82. 'confirm-sanitizations' => 'Prompt yes/no after importing the database, but before running the sanitizations',
  83. ),
  84. ),
  85. 'topics' => array('docs-aliases', 'docs-policy', 'docs-example-sync-via-http', 'docs-example-sync-extension'),
  86. );
  87. return $items;
  88. }
  89. /**
  90. * Implements hook_drush_help_alter().
  91. */
  92. function sqlsync_drush_help_alter(&$command) {
  93. // Drupal 7+ only options.
  94. if (drush_drupal_major_version() >= 7) {
  95. if ($command['command'] == 'sql-sync') {
  96. unset($command['options']['source-target']['hidden'], $command['options']['target-target']['hidden']);
  97. }
  98. }
  99. }
  100. /**
  101. * Command argument complete callback.
  102. *
  103. * @return
  104. * Array of available site aliases.
  105. */
  106. function sql_sql_sync_complete() {
  107. return array('values' => array_keys(_drush_sitealias_all_list()));
  108. }
  109. /*
  110. * Implements COMMAND hook init.
  111. */
  112. function drush_sql_sync_init($source, $destination) {
  113. // Try to get @self defined when --uri was not provided.
  114. drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_SITE);
  115. // Preflight destination in case it defines the alias used by the source
  116. _drush_sitealias_get_record($destination);
  117. // After preflight, get source and destination settings
  118. $source_settings = drush_sitealias_get_record($source);
  119. $destination_settings = drush_sitealias_get_record($destination);
  120. // Apply command-specific options.
  121. drush_sitealias_command_default_options($source_settings, 'source-');
  122. drush_sitealias_command_default_options($destination_settings, 'target-');
  123. }
  124. /*
  125. * A command validate callback.
  126. */
  127. function drush_sqlsync_sql_sync_validate($source, $destination) {
  128. // Get destination info for confirmation prompt.
  129. $source_settings = drush_sitealias_overlay_options(drush_sitealias_get_record($source), 'source-');
  130. $destination_settings = drush_sitealias_overlay_options(drush_sitealias_get_record($destination), 'target-');
  131. $source_db_url = drush_sitealias_get_db_spec($source_settings, FALSE, 'source-');
  132. $target_db_url = drush_sitealias_get_db_spec($destination_settings, FALSE, 'target-');
  133. $txt_source = (isset($source_db_url['remote-host']) ? $source_db_url['remote-host'] . '/' : '') . $source_db_url['database'];
  134. $txt_destination = (isset($target_db_url['remote-host']) ? $target_db_url['remote-host'] . '/' : '') . $target_db_url['database'];
  135. // Validate.
  136. if (empty($source_db_url)) {
  137. if (empty($source_settings)) {
  138. return drush_set_error('DRUSH_ALIAS_NOT_FOUND', dt('Error: no alias record could be found for source !source', array('!source' => $source)));
  139. }
  140. return drush_set_error('DRUSH_DATABASE_NOT_FOUND', dt('Error: no database record could be found for source !source', array('!source' => $source)));
  141. }
  142. if (empty($target_db_url)) {
  143. if (empty($destination_settings)) {
  144. return drush_set_error('DRUSH_ALIAS_NOT_FOUND', dt('Error: no alias record could be found for target !destination', array('!destination' => $destination)));
  145. }
  146. return drush_set_error('DRUSH_DATABASE_NOT_FOUND', dt('Error: no database record could be found for target !destination', array('!destination' => $destination)));
  147. }
  148. if (isset($source_db_url['remote-host']) && isset($target_db_url['remote-host']) && ($source_db_url['remote-host'] == $target_db_url['remote-host']) && ($source_db_url['database'] == $target_db_url['database']) && !drush_get_context('DRUSH_SIMULATE')) {
  149. return drush_set_error('DRUSH_SAME_DATABASE', dt('Source and target databases are the same; please sync to a different target.'));
  150. }
  151. if (drush_get_option('no-dump') && !drush_get_option('source-dump')) {
  152. return drush_set_error('DRUSH_SOURCE_DUMP_MISSING', dt('The --source-dump option must be supplied when --no-dump is specified.'));
  153. }
  154. if (drush_get_option('no-sync') && !drush_get_option('target-dump')) {
  155. return drush_set_error('DRUSH_TARGET_DUMP_MISSING', dt('The --target-dump option must be supplied when --no-sync is specified.'));
  156. }
  157. if (!drush_get_context('DRUSH_SIMULATE')) {
  158. drush_print(dt("You will destroy data in !target and replace with data from !source.", array('!source' => $txt_source, '!target' => $txt_destination)));
  159. // @todo Move sanitization prompts to here. They currently show much later.
  160. if (!drush_confirm(dt('Do you really want to continue?'))) {
  161. return drush_user_abort();
  162. }
  163. }
  164. }
  165. /*
  166. * A command callback.
  167. */
  168. function drush_sqlsync_sql_sync($source, $destination) {
  169. $source_settings = drush_sitealias_overlay_options(drush_sitealias_get_record($source), 'source-');
  170. $destination_settings = drush_sitealias_overlay_options(drush_sitealias_get_record($destination), 'target-');
  171. $source_is_local = !array_key_exists('remote-host', $source_settings) || drush_is_local_host($source_settings);
  172. $destination_is_local = !array_key_exists('remote-host', $destination_settings) || drush_is_local_host($destination_settings);
  173. // These options are passed along to subcommands like sql-create, sql-dump, sql-query, sql-sanitize, ...
  174. $source_options = drush_get_merged_prefixed_options('source-');
  175. $target_options = drush_get_merged_prefixed_options('target-');
  176. $backend_options = array();
  177. // @todo drush_redispatch_get_options() assumes you will execute same command. Not good.
  178. $global_options = drush_redispatch_get_options() + array(
  179. 'strict' => 0,
  180. );
  181. // We do not want to include root or uri here. If the user
  182. // provided -r or -l, their key has already been remapped to
  183. // 'root' or 'uri' by the time we get here.
  184. unset($global_options['root']);
  185. unset($global_options['uri']);
  186. if (drush_get_context('DRUSH_SIMULATE')) {
  187. $backend_options['backend-simulate'] = TRUE;
  188. }
  189. // Create destination DB if needed.
  190. if (drush_get_option('create-db')) {
  191. drush_log(dt('Starting to create database on Destination.'), LogLevel::OK);
  192. $return = drush_invoke_process($destination, 'sql-create', array(), $global_options + $target_options, $backend_options);
  193. if ($return['error_status']) {
  194. return drush_set_error('DRUSH_SQL_CREATE_FAILED', dt('sql-create failed.'));
  195. }
  196. }
  197. // Perform sql-dump on source unless told othrwise.
  198. $options = $global_options + $source_options + array(
  199. 'gzip' => TRUE,
  200. 'result-file' => drush_get_option('source-dump', TRUE),
  201. // 'structure-tables-list' => 'cache*', // Do we want to default to this?
  202. );
  203. if (!drush_get_option('no-dump')) {
  204. drush_log(dt('Starting to dump database on Source.'), LogLevel::OK);
  205. $return = drush_invoke_process($source, 'sql-dump', array(), $options, $backend_options);
  206. if ($return['error_status']) {
  207. return drush_set_error('DRUSH_SQL_DUMP_FAILED', dt('sql-dump failed.'));
  208. }
  209. else {
  210. $source_dump_path = $return['object'];
  211. if (!is_string($source_dump_path)) {
  212. return drush_set_error('DRUSH_SQL_DUMP_FILE_NOT_REPORTED', dt('The Drush sql-dump command did not report the path to the dump file produced. Try upgrading the version of Drush you are using on the source machine.'));
  213. }
  214. }
  215. }
  216. else {
  217. $source_dump_path = drush_get_option('source-dump');
  218. }
  219. $do_rsync = !drush_get_option('no-sync');
  220. // Determine path/to/dump on destination.
  221. if (drush_get_option('target-dump')) {
  222. $destination_dump_path = drush_get_option('target-dump');
  223. $rsync_options['yes'] = TRUE; // @temporary: See https://github.com/drush-ops/drush/pull/555
  224. }
  225. elseif ($source_is_local && $destination_is_local) {
  226. $destination_dump_path = $source_dump_path;
  227. $do_rsync = FALSE;
  228. }
  229. else {
  230. $tmp = '/tmp'; // Our fallback plan.
  231. drush_log(dt('Starting to discover temporary files directory on Destination.'), LogLevel::OK);
  232. $return = drush_invoke_process($destination, 'php-eval', array('return drush_find_tmp();'), array(), array('integrate' => FALSE, 'override-simulated' => TRUE));
  233. if (!$return['error_status']) {
  234. $tmp = $return['object'];
  235. }
  236. $destination_dump_path = $tmp . '/' . basename($source_dump_path);
  237. $rsync_options['yes'] = TRUE; // No need to prompt as destination is a tmp file.
  238. }
  239. if ($do_rsync) {
  240. if (!drush_get_option('no-dump')) {
  241. // Cleanup if this command created the dump file.
  242. $rsync_options['remove-source-files'] = TRUE;
  243. }
  244. $runner = drush_get_runner($source, $destination);
  245. $return = drush_invoke_process($runner, 'core-rsync', array("$source:$source_dump_path", "$destination:$destination_dump_path"), $global_options + $rsync_options);
  246. drush_log(dt('Copying dump file from Source to Destination.'), LogLevel::OK);
  247. if ($return['error_status']) {
  248. return drush_set_error('DRUSH_RSYNC_FAILED', dt('core-rsync failed.'));
  249. }
  250. }
  251. // Import file into destination.
  252. drush_log(dt('Starting to import dump file onto Destination database.'), LogLevel::OK);
  253. $options = $global_options + $target_options + array(
  254. 'file' => $destination_dump_path,
  255. 'file-delete' => TRUE,
  256. );
  257. $return = drush_invoke_process($destination, 'sql-query', array(), $options, $backend_options);
  258. if ($return['error_status']) {
  259. // An error was already logged.
  260. return FALSE;
  261. }
  262. // Run Sanitize if needed.
  263. $options = $global_options + $target_options;
  264. if (drush_get_option('sanitize')) {
  265. drush_log(dt('Starting to sanitize target database on Destination.'), LogLevel::OK);
  266. $return = drush_invoke_process($destination, 'sql-sanitize', array(), $options, $backend_options);
  267. if ($return['error_status']) {
  268. return drush_set_error('DRUSH_SQL_SANITIZE_FAILED', dt('sql-sanitize failed.'));
  269. }
  270. }
  271. }