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_rsync Entrypoint for drush rsync.
_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 destination in case it defines aliases used by the source
  18. _drush_sitealias_preflight_path($destination);
  19. // After preflight, evaluate file paths
  20. $source_settings = drush_sitealias_evaluate_path($source, $additional_options);
  21. $destination_settings = drush_sitealias_evaluate_path($destination, $additional_options);
  22. $source_path = $source_settings['evaluated-path'];
  23. $destination_path = $destination_settings['evaluated-path'];
  24. if (!isset($source_settings)) {
  25. return drush_set_error('DRUSH_BAD_PATH', dt('Could not evaluate source path !path.', array('!path' => $source)));
  26. }
  27. if (!isset($destination_settings)) {
  28. return drush_set_error('DRUSH_BAD_PATH', dt('Could not evaluate destination path !path.', array('!path' => $destination)));
  29. }
  30. // Check to see if this is an rsync multiple command (multiple sources and multiple destinations)
  31. $is_multiple = drush_do_multiple_command('rsync', $source_settings, $destination_settings, TRUE);
  32. if ($is_multiple === FALSE) {
  33. // If the user path is the same for the source and the destination, then
  34. // always add a slash to the end of the source. If the user path is not
  35. // the same in the source and the destinaiton, then you need to know how
  36. // rsync paths work, and put on the trailing '/' if you want it.
  37. if ($source_settings['user-path'] == $destination_settings['user-path']) {
  38. $source_path .= '/';
  39. }
  40. // Prompt for confirmation. This is destructive.
  41. if (!drush_get_context('DRUSH_SIMULATE')) {
  42. drush_print(dt("You will destroy data from !target and replace with data from !source", array('!source' => $source_path, '!target' => $destination_path)));
  43. if (!drush_confirm(dt('Do you really want to continue?'))) {
  44. // was: return drush_set_error('CORE_SYNC_ABORT', 'Aborting.');
  45. drush_die('Aborting.');
  46. }
  47. }
  48. // Exclude settings is the default only when both the source and
  49. // the destination are aliases or site names. Therefore, include
  50. // settings will be the default whenever either the source or the
  51. // destination contains a : or a /.
  52. $include_settings_is_default = (strpos($source . $destination, ':') !== FALSE) || (strpos($source . $destination, '/') !== FALSE);
  53. // Go ahead and call rsync with the paths we determined
  54. drush_core_call_rsync($source_path, $destination_path, $additional_options, $include_settings_is_default);
  55. }
  56. }
  57. /**
  58. * Make a direct call to rsync after the source and destination paths
  59. * have been evaluated.
  60. *
  61. * @param $source
  62. * Any path that can be passed to rsync.
  63. * @param $destination
  64. * Any path that can be passed to rsync.
  65. * @param $additional_options
  66. * An array of options that overrides whatever was passed in on the command
  67. * line (like the 'process' context, but only for the scope of this one
  68. * call).
  69. * @param $include_settings_is_default
  70. * If TRUE, then settings.php will be transferred as part of the rsync unless
  71. * --exclude-conf is specified. If FALSE, then settings.php will be excluded
  72. * from the transfer unless --include-conf is specified.
  73. * @param $live_output
  74. * If TRUE, output goes directly to the terminal using system(). If FALSE,
  75. * rsync is executed with drush_shell_exec() with output in
  76. * drush_shell_exec_output().
  77. *
  78. * @return
  79. * TRUE on success, FALSE on failure.
  80. */
  81. function drush_core_call_rsync($source, $destination, $additional_options = array(), $include_settings_is_default = TRUE, $live_output = TRUE) {
  82. $options = ' --exclude="*.svn*"';
  83. $mode = '-az';
  84. // Process --include-path and --exclude-path options the same way
  85. foreach (array('include', 'exclude') as $include_exclude) {
  86. // Get the option --include-path or --exclude path and explode to an array of paths
  87. // that we will translate into an --include or --exclude option to pass to rsync
  88. $inc_ex_path = explode(',', drush_get_option($include_exclude . '-path', ''));
  89. foreach ($inc_ex_path as $one_path_to_inc_ex) {
  90. if (!empty($one_path_to_inc_ex)) {
  91. $options .= ' --' . $include_exclude . '="' . $one_path_to_inc_ex . '"';
  92. }
  93. }
  94. }
  95. // drush_core_rsync passes in $include_settings_is_default such that
  96. // 'exclude-conf' is the default when syncing from one alias to
  97. // another, and 'include-conf' is the default when a path component
  98. // is included.
  99. if ($include_settings_is_default ? _drush_rsync_option_exists('exclude-conf', $additional_options) : !_drush_rsync_option_exists('include-conf', $additional_options)) {
  100. $options .= ' --exclude="settings.php"';
  101. }
  102. if (_drush_rsync_option_exists('exclude-sites', $additional_options)) {
  103. $options .= ' --include="sites/all" --exclude="sites/*"';
  104. }
  105. if (_drush_rsync_option_exists('mode', $additional_options)) {
  106. $mode = "-" . drush_get_option_override($additional_options, 'mode');
  107. }
  108. if (drush_get_context('DRUSH_VERBOSE')) {
  109. // the drush_op() will be verbose about the command that gets executed.
  110. $mode .= 'v';
  111. $options .= ' --stats --progress';
  112. }
  113. $rsync_available_options = array(
  114. // unary options
  115. 'archive', // -a
  116. 'recursive', // -r
  117. 'relative', // -R
  118. 'backup', // -b
  119. 'update', // -u
  120. 'checksum', // -c
  121. 'dirs', // -d
  122. 'links', // -l
  123. 'copy-links', // -L
  124. 'copy-dirlinks', // -k
  125. 'keep-dirlinks', // -K
  126. 'hard-links', // -H
  127. 'perms', // -p
  128. 'executability', // -E
  129. 'acls', // -A
  130. 'xattrs', // -X
  131. 'owner', // -o
  132. 'group', // -g
  133. 'times', // -t
  134. 'omit-dir-times', // -O
  135. 'sparse', // -S
  136. 'dry-run', // -n
  137. 'whole-file', // -W
  138. 'one-file-system', // -x
  139. 'prune-empty-dirs', // -m
  140. 'ignore-times', // -I
  141. 'fuzzy', // -y
  142. 'cvs-exclude', // -C
  143. 'compress', // -Z
  144. 'protect-args', // -s
  145. '8-bit-output', // -8
  146. 'human-readable', // -h
  147. 'itemize-changes', // -i
  148. 'copy-unsafe-links',
  149. 'safe-links',
  150. 'no-implied-dirs',
  151. 'inplace',
  152. 'append',
  153. 'append-verify',
  154. 'existing',
  155. 'remove-source-files',
  156. 'delete',
  157. 'delete-before',
  158. 'delete-during',
  159. 'delete-delay',
  160. 'delete-after',
  161. 'delete-excluded',
  162. 'ignore-errors',
  163. 'force',
  164. 'ignore-existing',
  165. 'partial',
  166. 'delay-updates',
  167. 'numeric-ids',
  168. 'size-only',
  169. 'blocking-io',
  170. 'stats',
  171. 'progress',
  172. 'list-only',
  173. // options with values
  174. 'block-size',
  175. 'backup-dir',
  176. 'suffix',
  177. 'chmod',
  178. 'rsync-path',
  179. 'modify-window',
  180. 'compare-dest',
  181. 'copy-dest',
  182. 'link-dest',
  183. 'skip-compress',
  184. 'filter',
  185. 'exclude',
  186. 'include',
  187. 'address',
  188. 'port',
  189. 'sockopts',
  190. 'out-format',
  191. 'bwlimit',
  192. 'iconv',
  193. 'checksum-seed',
  194. 'max-delete',
  195. 'max-size',
  196. 'min-size',
  197. 'partial-dir',
  198. 'timeout',
  199. 'temp-dir',
  200. 'compress-level',
  201. 'out-format',
  202. 'protocol',
  203. );
  204. foreach ($rsync_available_options as $test_option) {
  205. $value = drush_get_option_override($additional_options, $test_option);
  206. if (isset($value)) {
  207. if ($value === TRUE) {
  208. $options .= " --$test_option";
  209. }
  210. else {
  211. $options .= " --$test_option=" . escapeshellarg($value);
  212. }
  213. }
  214. }
  215. $ssh_options = drush_get_option_override($additional_options, 'ssh-options', '');
  216. $exec = "rsync -e 'ssh $ssh_options' $mode$options $source $destination";
  217. if ($live_output) {
  218. $exec_result = drush_op('system', $exec) !== FALSE;
  219. }
  220. else {
  221. $exec_result = drush_shell_exec($exec);
  222. }
  223. return $exec_result;
  224. }
  225. function _drush_rsync_option_exists($option, $additional_options) {
  226. if (array_key_exists($option, $additional_options)) {
  227. return TRUE;
  228. }
  229. else {
  230. return drush_get_option($option, FALSE);
  231. }
  232. }