rsync.core.inc

  1. 8.0.x commands/core/rsync.core.inc
  2. 6.x commands/core/rsync.core.inc
  3. 7.x commands/core/rsync.core.inc
  4. 3.x commands/core/rsync.core.inc
  5. 4.x commands/core/rsync.core.inc
  6. 5.x commands/core/rsync.core.inc
  7. master commands/core/rsync.core.inc

Functions

Namesort descending Description
drush_core_call_rsync Make a direct call to rsync after the source and destination paths have been evaluated.
drush_core_exec_rsync
drush_core_rsync Entrypoint for drush rsync.
_drush_build_rsync_options
_drush_rsync_option_exists

File

commands/core/rsync.core.inc
View source
  1. <?php
  2. /**
  3. * Entrypoint for drush rsync.
  4. *
  5. * @param source
  6. * A site alias ("@dev") or site specification ("/path/to/drupal#mysite.com")
  7. * followed by an optional path (":path/to/sync"), or any path
  8. * that could be passed to rsync ("user@server.com:/path/to/dir/").
  9. * @param destination
  10. * Same format as source.
  11. * @param additional_options
  12. * An array of options that overrides whatever was passed in on
  13. * the command line (like the 'process' context, but only for
  14. * the scope of this one call).
  15. */
  16. function drush_core_rsync($source, $destination, $additional_options = array()) {
  17. // Preflight source in case it defines aliases used by the destination
  18. _drush_sitealias_preflight_path($source);
  19. // After preflight, evaluate file paths. We evaluate destination paths first, because
  20. // there is a first-one-wins policy with --exclude-paths, and we want --target-command-specific
  21. // to take precedence over --source-command-specific.
  22. $destination_settings = drush_sitealias_evaluate_path($destination, $additional_options, FALSE, "rsync", 'target-');
  23. $source_settings = drush_sitealias_evaluate_path($source, $additional_options, FALSE, "rsync", 'source-');
  24. $source_path = $source_settings['evaluated-path'];
  25. $destination_path = $destination_settings['evaluated-path'];
  26. if (!isset($source_settings)) {
  27. return drush_set_error('DRUSH_BAD_PATH', dt('Could not evaluate source path !path.', array('!path' => $source)));
  28. }
  29. if (!isset($destination_settings)) {
  30. return drush_set_error('DRUSH_BAD_PATH', dt('Could not evaluate destination path !path.', array('!path' => $destination)));
  31. }
  32. // Check to see if this is an rsync multiple command (multiple sources and multiple destinations)
  33. $is_multiple = drush_do_multiple_command('rsync', $source_settings, $destination_settings, TRUE);
  34. if ($is_multiple === FALSE) {
  35. // If the user path is the same for the source and the destination, then
  36. // always add a slash to the end of the source. If the user path is not
  37. // the same in the source and the destination, then you need to know how
  38. // rsync paths work, and put on the trailing '/' if you want it.
  39. if ($source_settings['user-path'] == $destination_settings['user-path']) {
  40. $source_path .= '/';
  41. }
  42. // Prompt for confirmation. This is destructive.
  43. if (!drush_get_context('DRUSH_SIMULATE')) {
  44. drush_print(dt("You will destroy data from !target and replace with data from !source", array('!source' => $source_path, '!target' => $destination_path)));
  45. if (!drush_confirm(dt('Do you really want to continue?'))) {
  46. // was: return drush_set_error('CORE_SYNC_ABORT', 'Aborting.');
  47. return drush_user_abort();
  48. }
  49. }
  50. // Exclude settings is the default only when both the source and
  51. // the destination are aliases or site names. Therefore, include
  52. // settings will be the default whenever either the source or the
  53. // destination contains a : or a /.
  54. $include_settings_is_default = (strpos($source . $destination, ':') !== FALSE) || (strpos($source . $destination, '/') !== FALSE);
  55. $options = _drush_build_rsync_options($additional_options, $include_settings_is_default);
  56. // Get all of the args and options that appear after the command name.
  57. $original_args = drush_get_original_cli_args_and_options();
  58. foreach ($original_args as $original_option) {
  59. if ($original_option{0} == '-') {
  60. $options .= ' ' . $original_option;
  61. }
  62. }
  63. // Go ahead and call rsync with the paths we determined
  64. return drush_core_exec_rsync($source_path, $destination_path, $options);
  65. }
  66. }
  67. /**
  68. * Make a direct call to rsync after the source and destination paths
  69. * have been evaluated.
  70. *
  71. * @param $source
  72. * Any path that can be passed to rsync.
  73. * @param $destination
  74. * Any path that can be passed to rsync.
  75. * @param $additional_options
  76. * An array of options that overrides whatever was passed in on the command
  77. * line (like the 'process' context, but only for the scope of this one
  78. * call).
  79. * @param $include_settings_is_default
  80. * If TRUE, then settings.php will be transferred as part of the rsync unless
  81. * --exclude-conf is specified. If FALSE, then settings.php will be excluded
  82. * from the transfer unless --include-conf is specified.
  83. * @param $live_output
  84. * If TRUE, output goes directly to the terminal using system(). If FALSE,
  85. * rsync is executed with drush_shell_exec() with output in
  86. * drush_shell_exec_output().
  87. *
  88. * @return
  89. * TRUE on success, FALSE on failure.
  90. */
  91. function drush_core_call_rsync($source, $destination, $additional_options = array(), $include_settings_is_default = TRUE, $live_output = TRUE) {
  92. $options = _drush_build_rsync_options($additional_options, $include_settings_is_default);
  93. return drush_core_exec_rsync($source, $destination, $options, $additional_options, $live_output);
  94. }
  95. function drush_core_exec_rsync($source, $destination, $options, $additional_options = array(), $live_output = TRUE) {
  96. $ssh_options = drush_get_option_override($additional_options, 'ssh-options', '');
  97. $exec = "rsync -e 'ssh $ssh_options' $options $source $destination";
  98. if ($live_output) {
  99. $exec_result = drush_op_system($exec);
  100. $result = ($exec_result == 0);
  101. }
  102. else {
  103. $result = drush_shell_exec($exec);
  104. }
  105. if (!$result) {
  106. drush_set_error('DRUSH_RSYNC_FAILED', dt("Could not rsync from !source to !dest", array('!source' => $source, '!dest' => $destination)));
  107. }
  108. return $result;
  109. }
  110. function _drush_build_rsync_options($additional_options, $include_settings_is_default = TRUE) {
  111. $options = '';
  112. // Exclude vcs reserved files.
  113. if (!_drush_rsync_option_exists('include-vcs', $additional_options)) {
  114. $vcs_files = drush_version_control_reserved_files();
  115. foreach ($vcs_files as $file) {
  116. $options .= ' --exclude="'.$file.'"';
  117. }
  118. }
  119. else {
  120. unset($additional_options['include-vcs']);
  121. }
  122. $mode = '-akz';
  123. // Process --include-paths and --exclude-paths options the same way
  124. foreach (array('include', 'exclude') as $include_exclude) {
  125. // Get the option --include-paths or --exclude-paths and explode to an array of paths
  126. // that we will translate into an --include or --exclude option to pass to rsync
  127. $inc_ex_path = explode(PATH_SEPARATOR, drush_get_option($include_exclude . '-paths', ''));
  128. foreach ($inc_ex_path as $one_path_to_inc_ex) {
  129. if (!empty($one_path_to_inc_ex)) {
  130. $options .= ' --' . $include_exclude . '="' . $one_path_to_inc_ex . '"';
  131. }
  132. }
  133. // Remove stuff inserted by evaluate path
  134. unset($additional_options[$include_exclude . '-paths']);
  135. unset($additional_options[$include_exclude . '-files-processed']);
  136. }
  137. // drush_core_rsync passes in $include_settings_is_default such that
  138. // 'exclude-conf' is the default when syncing from one alias to
  139. // another, and 'include-conf' is the default when a path component
  140. // is included.
  141. if ($include_settings_is_default ? _drush_rsync_option_exists('exclude-conf', $additional_options) : !_drush_rsync_option_exists('include-conf', $additional_options)) {
  142. $options .= ' --exclude="settings.php"';
  143. unset($additional_options['exclude-conf']);
  144. }
  145. if (_drush_rsync_option_exists('exclude-sites', $additional_options)) {
  146. $options .= ' --include="sites/all" --exclude="sites/*"';
  147. unset($additional_options['exclude-sites']);
  148. }
  149. if (_drush_rsync_option_exists('mode', $additional_options)) {
  150. $mode = "-" . drush_get_option_override($additional_options, 'mode');
  151. unset($additional_options['mode']);
  152. }
  153. if (drush_get_context('DRUSH_VERBOSE')) {
  154. // the drush_op() will be verbose about the command that gets executed.
  155. $mode .= 'v';
  156. $options .= ' --stats --progress';
  157. }
  158. // Check if the user has set $options['rsync-version'] to enable rsync legacy version support.
  159. // Drush was written for rsync 2.6.9 or later, so assume that version if nothing was explicitly set.
  160. $rsync_version = drush_get_option(array('rsync-version','source-rsync-version','target-rsync-version'), '2.6.9');
  161. $options_to_exclude = array('ssh-options');
  162. foreach ($additional_options as $test_option => $value) {
  163. // Downgrade some options for older versions of rsync
  164. if ($test_option == 'remove-source-files') {
  165. if (version_compare($rsync_version, '2.6.4', '<')) {
  166. $test_option = NULL;
  167. drush_log('Rsync does not support --remove-sent-files prior to version 2.6.4; some temporary files may remain undeleted.', 'warning');
  168. }
  169. elseif (version_compare($rsync_version, '2.6.9', '<')) {
  170. $test_option = 'remove-sent-files';
  171. }
  172. }
  173. if ((isset($test_option)) && !in_array($test_option, $options_to_exclude) && (isset($value) && !is_array($value))) {
  174. if (($value === TRUE) || (!isset($value))) {
  175. $options .= " --$test_option";
  176. }
  177. else {
  178. $options .= " --$test_option=" . escapeshellarg($value);
  179. }
  180. }
  181. }
  182. return $mode . $options;
  183. }
  184. function _drush_rsync_option_exists($option, $additional_options) {
  185. if (array_key_exists($option, $additional_options)) {
  186. return TRUE;
  187. }
  188. else {
  189. return drush_get_option($option, FALSE);
  190. }
  191. }