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