Drush Tip: Quickly Sync Files Between Your Environments With Rsync
If you’re using Drupal best practices, you probably maintain different copies of your website (e.g. some combination of production, staging, development, and local development environments). But what happens when your local copy needs to be synchronized with the files you uploaded to your production server? If you need to do this a lot, automating the process can save you a lot of time and reduce the possibility of human error.
Here's a quick way to automate this file syncronization process using the rsync option within drush. Besides a basic drush installation/configuration, there are only two main items you'll need to place in a drush site alias file in order to set this up:
- A path alias for each environment.
- A shell alias for each combination of environments you wish to sync files (i.e. source1 to destination1, etc).
Adding a Path Alias for Each Environment
The path alias variable is a way to reference which folder you want to sync to and from on each environment. In this example, we are using the standard sites/default/files location and referencing it by %files variable.
// Site mysite.com, environment prod $aliases['mysite.production'] = array( 'parent' 'root' => '/my/file/path/drupal', 'uri' => 'mysite.com', 'remote-host' => 'myhosting.com', 'remote-user' => 'myusername', 'path-aliases' => array( '%files' => 'sites/default/files', ) ); // Site mysite.com, environment local $aliases['mysite.local'] = array( 'root' => '/my/local/file/path/drupal', 'uri' => 'mylocalsite', 'path-aliases' => array( '%files' => 'sites/default/files', ) );
If you have any questions on what the over variables mean, simply checkout the examples/example.aliases.drushrc.php file contained in the drush module. Also be sure to add in entries for your additional environments if you want the option to sync to/from them.
Adding a Rsync Shell Alias
Once you have the path aliases set, the last part is simple one line addition at the end of the site alias file.
$options['shell-aliases']['pull-files'] = '!drush rsync @mysite.production:%files/ @mysite.local:%files';
Here I've created a simple drush shell alias called "pull-files." All I need to do is run drush @mysite pull-files and drush will pull down my files from production to my local environment using the settings in my site alias file. And if you'd prefer to sync from the development or staging environments, simply create seperate aliases for each possible combination you'd like (e.g. pull-files-staging, pull-files-dev, etc).
Ideally you've properly configured/installed your ssh keys such that you don't have to authenticate with the external servers every time you run this command. And if you'd like to keep files in sync without having to think about it, you could also add this to your local crontab. Using this in combination with drush's sql-sync could be useful if you want to start each day or each new feature from a clean slate!comments powered by Disqus