10th June 2013

Drupal, Drush aliases and how to use them

John Ennew
Technical Director

drush aliases allow you to run a drush commands on your local server but actually execute the command on a remote server. This is a real time saver when working locally and then you want to do something on the remote server. Your specific workflows will depend on your particular setup but, as an example on Acquia dev cloud, how about pushing code up to your remote dev server and running updatedb in two commands.

 $ git push origin $ drush @site.dev updatedb -y 
Setting up your local computer to use an alias needs a little configuration. Alias configuration files can go into a .drush folder in your home directory or in the sites/all/drush directory of the site itself (putting them in the .drush in your home directory means they will be accessible from anywhere). You can add as many alias files as you like here with the naming convention:

Each file can contain any number of aliases descriptions which are just entries in a multi dimensional array, the key of which is the subitem for that alias. For example, an alias file called mysite.aliases.drushrc.php with one dev entry like this ...

 $aliases['dev'] = array( 'parent' => '@parent', 'site' => 'mysite', 'env' => 'dev', 'root' => '/var/www/html/mysite.dev/docroot', 'remote-host' => 'example.myserver.com', 'remote-user' => 'mysite', ); 
... gives one alias which is accessed via the @mysite.dev drush argument. For example,
 drush @mysite.dev status 
There is also a drush command to get you started on producing an alias drushrc file:
 drush sa [uri] 

Holy Cow! What else can I do with drush aliases?

I am glad you asked. How about download the remote db in one command ...

 drush @mysite.dev sql-dump > /tmp/dbdump.sql 
Note: don't use --result-file here as that will put the sql file on the remote server! How about bring all the remote files down to your local system? From the root of the site on your local install run ...
 drush rsync @mysite.dev:sites/default/files/ sites/default/files/ 
Note: Run drush help rsync for help with this one! And as an encore, how about applying your live db straight onto your local dev db in one command:
 drush @mysite.dev sql-dump --uri=mysite.com | drush sql-cli 
Note: This does replace your dev db without prompting so be careful! Now, having to put --uri in for the environment is annoying if there is only one uri, so lets add uri as an option in the alias ...
 $aliases['dev'] = array( 'parent' => '@parent', 'site' => 'mysite', 'env' => 'dev', 'root' => '/var/www/html/mysite.dev/docroot', 'remote-host' => 'example.myserver.com', 'remote-user' => 'mysite', 'uri' => 'dev.mysite.com', ); 
You can put defaults for most drush command options in the alias, setting specific defaults for each environment. For example, the excellent Master module needs the scope option to work out which scope to use. But typically scope is always the same for an environment, so adding
'scope' => 'dev',
to the alias means that:
drush @mysite.dev master-status --scope=dev
drush @mysite.dev master-status

Notes on Acquia and Drush aliases

We often use Acquia dev cloud to host our client's sites. On Acquia hosted sites setting up local drush aliases are made very simple. Under the cloud menu on the subscription tab there is a utility link which describes exactly how to add the alias files to your local computer. Acquia executes drush commands using drush 4 by default. To use drush 5, you'll need to edit your alias files in the .drush folder and for each environment add a path-aliases entry which describes which drush script to execute. The precise syntax is shown below.

 $aliases['dev'] = array( 'parent' => '@parent', 'site' => 'mysite', 'env' => 'dev', 'root' => '/var/www/html/mysite.dev/docroot', 'remote-host' => 'mysite.com', 'remote-user' => 'mysite', 'path-aliases' => array( '%drush-script' => 'drush5', ), ); 

Other reading

This link shows all the goodness that can be put into a sites drushrc file ... Educating Drush