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 A command callback.
drush_core_rsync_init
_drush_build_rsync_options
_drush_core_rsync_both_remote Handle an rsync operation from a remote site to a remote site by first rsync'ing to a local location, and then copying that location to its final destination.
_drush_rsync_option_exists

File

commands/core/rsync.core.inc
View source
  1. <?php
  2. use Drush\Log\LogLevel;
  3. /*
  4. * Implements COMMAND hook init.
  5. */
  6. function drush_core_rsync_init() {
  7. // Try to get @self defined when --uri was not provided.
  8. drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_SITE);
  9. }
  10. /**
  11. * A command callback.
  12. *
  13. * @param source
  14. * A site alias ("@dev") or site specification ("/path/to/drupal#mysite.com")
  15. * followed by an optional path (":path/to/sync"), or any path
  16. * that could be passed to rsync ("user@server.com:/path/to/dir/").
  17. * @param destination
  18. * Same format as source.
  19. * @param additional_options
  20. * An array of options that overrides whatever was passed in on
  21. * the command line (like the 'process' context, but only for
  22. * the scope of this one call).
  23. */
  24. function drush_core_rsync($source, $destination, $additional_options = array()) {
  25. // Preflight source in case it defines aliases used by the destination
  26. _drush_sitealias_preflight_path($source);
  27. // After preflight, evaluate file paths. We evaluate destination paths first, because
  28. // there is a first-one-wins policy with --exclude-paths, and we want --target-command-specific
  29. // to take precedence over --source-command-specific.
  30. $destination_settings = drush_sitealias_evaluate_path($destination, $additional_options, FALSE, "rsync", 'target-');
  31. $source_settings = drush_sitealias_evaluate_path($source, $additional_options, FALSE, "rsync", 'source-');
  32. $source_path = $source_settings['evaluated-path'];
  33. $destination_path = $destination_settings['evaluated-path'];
  34. if (!isset($source_settings)) {
  35. return drush_set_error('DRUSH_BAD_PATH', dt('Could not evaluate source path !path.', array('!path' => $source)));
  36. }
  37. if (!isset($destination_settings)) {
  38. return drush_set_error('DRUSH_BAD_PATH', dt('Could not evaluate destination path !path.', array('!path' => $destination)));
  39. }
  40. // If the user path is the same for the source and the destination, then
  41. // always add a slash to the end of the source. If the user path is not
  42. // the same in the source and the destination, then you need to know how
  43. // rsync paths work, and put on the trailing '/' if you want it.
  44. if ($source_settings['user-path'] == $destination_settings['user-path']) {
  45. $source_path .= '/';
  46. }
  47. // Prompt for confirmation. This is destructive.
  48. if (!drush_get_context('DRUSH_SIMULATE')) {
  49. drush_print(dt("You will delete files in !target and replace with data from !source", array('!source' => $source_path, '!target' => $destination_path)));
  50. if (!drush_confirm(dt('Do you really want to continue?'))) {
  51. return drush_user_abort();
  52. }
  53. }
  54. // Next, check to see if both the source and the destination are remote.
  55. // If so, then we'll process this as an rsync from source to local,
  56. // followed by an rsync from local to the destination.
  57. if (drush_sitealias_is_remote_site($source_settings) && drush_sitealias_is_remote_site($destination_settings)) {
  58. return _drush_core_rsync_both_remote($source, $destination, $additional_options, $source_path);
  59. }
  60. // Exclude settings is the default only when both the source and
  61. // the destination are aliases or site names. Therefore, include
  62. // settings will be the default whenever either the source or the
  63. // destination contains a : or a /.
  64. $include_settings_is_default = (strpos($source . $destination, ':') !== FALSE) || (strpos($source . $destination, '/') !== FALSE);
  65. $options = _drush_build_rsync_options($additional_options, $include_settings_is_default);
  66. // Get all of the args and options that appear after the command name.
  67. $original_args = drush_get_original_cli_args_and_options();
  68. foreach ($original_args as $original_option) {
  69. if ($original_option{0} == '-') {
  70. $options .= ' ' . $original_option;
  71. }
  72. }
  73. drush_backend_set_result($destination_path);
  74. // Go ahead and call rsync with the paths we determined
  75. return drush_core_exec_rsync($source_path, $destination_path, $options);
  76. }
  77. /**
  78. * Make a direct call to rsync after the source and destination paths
  79. * have been evaluated.
  80. *
  81. * @param $source
  82. * Any path that can be passed to rsync.
  83. * @param $destination
  84. * Any path that can be passed to rsync.
  85. * @param $additional_options
  86. * An array of options that overrides whatever was passed in on the command
  87. * line (like the 'process' context, but only for the scope of this one
  88. * call).
  89. * @param $include_settings_is_default
  90. * If TRUE, then settings.php will be transferred as part of the rsync unless
  91. * --exclude-conf is specified. If FALSE, then settings.php will be excluded
  92. * from the transfer unless --include-conf is specified.
  93. * @param $live_output
  94. * If TRUE, output goes directly to the terminal using system(). If FALSE,
  95. * rsync is executed with drush_shell_exec() with output in
  96. * drush_shell_exec_output().
  97. *
  98. * @return
  99. * TRUE on success, FALSE on failure.
  100. */
  101. function drush_core_call_rsync($source, $destination, $additional_options = array(), $include_settings_is_default = TRUE, $live_output = TRUE) {
  102. $options = _drush_build_rsync_options($additional_options, $include_settings_is_default);
  103. return drush_core_exec_rsync($source, $destination, $options, $additional_options, $live_output);
  104. }
  105. function drush_core_exec_rsync($source, $destination, $options, $additional_options = array(), $live_output = TRUE) {
  106. $ssh_options = drush_get_option_override($additional_options, 'ssh-options', '');
  107. $exec = "rsync -e 'ssh $ssh_options' $options $source $destination";
  108. if ($live_output) {
  109. $exec_result = drush_op_system($exec);
  110. $result = ($exec_result == 0);
  111. }
  112. else {
  113. $result = drush_shell_exec($exec);
  114. }
  115. if (!$result) {
  116. drush_set_error('DRUSH_RSYNC_FAILED', dt("Could not rsync from !source to !dest", array('!source' => $source, '!dest' => $destination)));
  117. }
  118. return $result;
  119. }
  120. function _drush_build_rsync_options($additional_options, $include_settings_is_default = TRUE) {
  121. $options = '';
  122. // Exclude vcs reserved files.
  123. if (!_drush_rsync_option_exists('include-vcs', $additional_options)) {
  124. $vcs_files = drush_version_control_reserved_files();
  125. foreach ($vcs_files as $file) {
  126. $options .= ' --exclude="'.$file.'"';
  127. }
  128. }
  129. else {
  130. unset($additional_options['include-vcs']);
  131. }
  132. $mode = '-akz';
  133. // Process --include-paths and --exclude-paths options the same way
  134. foreach (array('include', 'exclude') as $include_exclude) {
  135. // Get the option --include-paths or --exclude-paths and explode to an array of paths
  136. // that we will translate into an --include or --exclude option to pass to rsync
  137. $inc_ex_path = explode(PATH_SEPARATOR, drush_get_option($include_exclude . '-paths', ''));
  138. foreach ($inc_ex_path as $one_path_to_inc_ex) {
  139. if (!empty($one_path_to_inc_ex)) {
  140. $options .= ' --' . $include_exclude . '="' . $one_path_to_inc_ex . '"';
  141. }
  142. }
  143. // Remove stuff inserted by evaluate path
  144. unset($additional_options[$include_exclude . '-paths']);
  145. unset($additional_options[$include_exclude . '-files-processed']);
  146. }
  147. // drush_core_rsync passes in $include_settings_is_default such that
  148. // 'exclude-conf' is the default when syncing from one alias to
  149. // another, and 'include-conf' is the default when a path component
  150. // is included.
  151. if ($include_settings_is_default ? _drush_rsync_option_exists('exclude-conf', $additional_options) : !_drush_rsync_option_exists('include-conf', $additional_options)) {
  152. $options .= ' --exclude="settings.php"';
  153. unset($additional_options['exclude-conf']);
  154. }
  155. if (_drush_rsync_option_exists('exclude-sites', $additional_options)) {
  156. $options .= ' --include="sites/all" --exclude="sites/*"';
  157. unset($additional_options['exclude-sites']);
  158. }
  159. if (_drush_rsync_option_exists('mode', $additional_options)) {
  160. $mode = "-" . drush_get_option_override($additional_options, 'mode');
  161. unset($additional_options['mode']);
  162. }
  163. if (drush_get_context('DRUSH_VERBOSE')) {
  164. // the drush_op() will be verbose about the command that gets executed.
  165. $mode .= 'v';
  166. $options .= ' --stats --progress';
  167. }
  168. // Check if the user has set $options['rsync-version'] to enable rsync legacy version support.
  169. // Drush was written for rsync 2.6.9 or later, so assume that version if nothing was explicitly set.
  170. $rsync_version = drush_get_option(array('rsync-version','source-rsync-version','target-rsync-version'), '2.6.9');
  171. $options_to_exclude = array('ssh-options');
  172. foreach ($additional_options as $test_option => $value) {
  173. // Downgrade some options for older versions of rsync
  174. if ($test_option == 'remove-source-files') {
  175. if (version_compare($rsync_version, '2.6.4', '<')) {
  176. $test_option = NULL;
  177. drush_log('Rsync does not support --remove-sent-files prior to version 2.6.4; some temporary files may remain undeleted.', LogLevel::WARNING);
  178. }
  179. elseif (version_compare($rsync_version, '2.6.9', '<')) {
  180. $test_option = 'remove-sent-files';
  181. }
  182. }
  183. if ((isset($test_option)) && !in_array($test_option, $options_to_exclude) && (isset($value) && !is_array($value))) {
  184. if (($value === TRUE) || (!isset($value))) {
  185. $options .= " --$test_option";
  186. }
  187. else {
  188. $options .= " --$test_option=" . escapeshellarg($value);
  189. }
  190. }
  191. }
  192. return $mode . $options;
  193. }
  194. function _drush_rsync_option_exists($option, $additional_options) {
  195. if (array_key_exists($option, $additional_options)) {
  196. return TRUE;
  197. }
  198. else {
  199. return drush_get_option($option, FALSE);
  200. }
  201. }
  202. /**
  203. * Handle an rsync operation from a remote site to a remote
  204. * site by first rsync'ing to a local location, and then
  205. * copying that location to its final destination.
  206. */
  207. function _drush_core_rsync_both_remote($source, $destination, $additional_options, $source_path) {
  208. $options = $additional_options + drush_redispatch_get_options();
  209. // Make a temporary directory to copy to. There are three
  210. // cases to consider:
  211. //
  212. // 1. rsync @src:file.txt @dest:location
  213. // 2. rsync @src:dir @dest:location
  214. // 3. rsync @src:dir/ @dest:location
  215. //
  216. // We will explain each of these in turn.
  217. //
  218. // 1. Copy a single file. We'll split this up like so:
  219. //
  220. // rsync @src:file.txt /tmp/tmpdir
  221. // rsync /tmp/tmpdir/file.txt @dest:location
  222. //
  223. // Since /tmp/tmpdir is empty, we could also rsync from
  224. // '/tmp/tmpdir/' if we wished.
  225. //
  226. // 2. Copy a directory. A directory with the same name
  227. // is copied to the destination. We'll split this up like so:
  228. //
  229. // rsync @src:dir /tmp/tmpdir
  230. // rsync /tmp/tmpdir/dir @dest:location
  231. //
  232. // The result is that everything in 'dir' is copied to @dest,
  233. // and ends up in 'location/dir'.
  234. //
  235. // 3. Copy the contents of a directory. We will split this
  236. // up as follows:
  237. //
  238. // rsync @src:dir/ /tmp/tmpdir
  239. // rsync /tmp/tmpdir/ @dest:location
  240. //
  241. // After the first rsync, everything in 'dir' will end up in
  242. // tmpdir. The second rsync copies everything in tmpdir to
  243. // @dest:location without creating an encapsulating folder
  244. // in the destination (i.e. there is no 'tmpdir' in the destination).
  245. //
  246. // All three of these cases need to be handled correctly in order
  247. // to ensure the correct results. In all cases the first
  248. // rsync always copies to $tmpDir, however the second rsync has
  249. // two cases that depend on the source path. If the source path ends
  250. // in /, the contents of a directory have been copied to $tmpDir, and
  251. // the contents of $tmpDir must be copied to the destination. Otherwise,
  252. // a specific file or directory has been copied to $tmpDir and that
  253. // specific item, identified by basename($source_path) must be copied to
  254. // the destination.
  255. $putInTmpPath = drush_tempdir();
  256. $getFromTmpPath = "$putInTmpPath/";
  257. if (substr($source_path, -1) !== '/') {
  258. $getFromTmpPath .= basename($source_path);
  259. }
  260. // Copy from the source to the temporary location. Exit on failure.
  261. $values = drush_invoke_process('@self', 'core-rsync', array($source, $putInTmpPath), $options);
  262. if ($values['error'] != 0) {
  263. return FALSE;
  264. }
  265. // Copy from the temporary location to the final destination.
  266. $values = drush_invoke_process('@self', 'core-rsync', array($getFromTmpPath, $destination), $options);
  267. return $values['error'] == 0;
  268. }