Main Content
Drush logo on laptop with 0s and 1s in the background

Using SQL-sync & rsync in Drush

One of the main draws to Drush is the library's ability to make developer lives easier. There are two simple commands that work using Drush aliases that can help sync database and files between multiple Drupal instances.  First, we'll go over setting up an alias file for Drush. After that, we'll document the usage of Drush's SQL-sync and rsync commands.

 

Create a file to store your aliases that is readable by Drush.

If it does not exist already, create a .drush folder in your home directory.

$ mkdir ~/.drush

Create an aliases file for the current project that you are setting up.

$ vim ~/.drush/aliases.drushrc.php

You can call this file WHATEVER.aliases.drushrc.php or even just put this code in your drushrc.php file. One tactic might be to divide *.aliases.drushrc.php into different files per-project or per-environment Shown below is a template for setting up a local and remote alias for a given site. The 'local' alias is configured for OSX while the 'remote' aliases are configured for Ubuntu.

Your actual configuration will vary, but this should suffice as an example:

$local_sites = '/users/username/Sites/'; $aliases['prometsource.local'] = array( 'root' => $local_sites . 'prometsource.prometdev.com', 'path-aliases' => array( '%dump-dir' => $local_sites . 'drush.dbdumps', '%files' => $local_sites . 'prometsource/sites/default/files' ) ); $remote_sites = '/var/www/sites/'; $aliases['remote'] = array ( 'remote-host' => '10.0.0.83', 'remote-user' => 'username', 'ssh-options' => "-p 22", 'path-aliases' => array( '%dump-dir' => '/home/username/drush.dbdumps' ) ); $aliases['prometsource.dev'] = array( 'parent' => '@remote', 'root' => $remote_sites . 'prometsource.developmentbranch.com', 'path-aliases' => array( '%files' => $remote_sites . 'prometsource.developmentbranch.com/sites/default/files' ) );

Using the $aliases array, one can define as many aliases as they need. There are three aliases listed above. A 'parent' alias can be created and child aliases can inherit their parent's attributes. In the example, 'remote' is a parent to the 'prometsource.dev' alias. This can be very useful if you want to create aliases for multiple sites that are hosted by the same environment. Now that we have aliases defined for our remote and local Drupal sites, we can sync database and files between them.

 

Import the remote database to your local database

$ drush SQL-sync @prometsource.dev @prometsource.local

There are many --flags for drush SQL-sync. One of the most important ones to note is '--no-cache' - you will find this flag useful if you are syncing your database more than once every 24 hrs and you want to be using a new db dump rather than a cached version of the db dump. Note that we defined a '%dump-dir' in our aliases settings. You can find recent db dumps in this folder. Drush must have permissions to write to the folder in order to successfully SQL-sync your db dump.

 

Import the remote files to your local files folder

$ drush rsync @prometsource.dev:%files/ @prometsource.local:%files

This command will pull down the entire files directory from the remote site to the local site. This may take quite some time. Drush does not provide any indication that it is working outside of holding up your command line interface from accepting a new command until it is done.

You can use a network monitor, such as the program that comes with OSX utilities, to actually see the files being downloaded and track progress. There are many --flags for drush rsync as well.

 

Conclusion

Generally, we do not condone a workflow that calls for consistent SQL-sync back and forth between development to staging and production sites. However, these commands can be very useful for setting up a new local testing environment from an existing project. The value of being able to automatically sync files and database should be apparent at this point!

Want to learn more about tinkering with code?

Sign up for training.