computer code

Drush aliases - streamline your web development workflow

For Drupal website developers, drush is an indispensable line command toolkit. Once installed, type 'drush help' to see the many wondrous drush commands you have at your disposal when building your website. One of the most useful ways to extend drush and streamline your web design workflow is to create aliases. They enable you to target web projects and tailor options, just by using an ampersands!

Quite a bit has changed with is drush 8 which the latest stable release. Drush 8 is needed for Drupal 8 and is 100% compatible with Drupal 7. I found most of the tutorials on drush aliases to either be out of date or incomplete. Hopefully this article fills in a few holes.

Two drush sql commands I find very useful in conjunction with an alias are drush sql-sync and drush sql-dump. With an alias you can download a copy of your production database from a remote server and import it into your local development instance. In a later tutorial I will look more closely at drush sql-dump. You can even enable your development modules and Ui's on the fly (I will provide a tutorial on that later - see below).

Firstly let us create a drush alias file. There is a decent example file at example.aliases.drushrc.php that explains the required directory structure and naming convention so drush can find your file. I personally like to situate my alias file in $HOME/.drush/site-aliases (for mac users that is /Users/YOUR_USERNAME/.drush/site-aliases). Name the file SITE_NAME.aliases.drushrc.php i.e. example.aliases.drushrc.php. You will target your alias in a command by @SITE_NAME.ALIAS_ARRAY_NAME. The example below hopefully should explain this concept in more depth.

Now take a look at the code below. The aliases are the commented by //local alias and //production alias. These tell drush all it needs to alias your project.


<?php
/**
* @file
* Example of valid statements for an alias file.
*
* Aliases are commonly used to define short names for
* local or remote Drupal installations; however, an alias
* is really nothing more than a collection of options.
* 
*/
//local alias - details of you local server / machine
$aliases['local'] = array(
'uri' => 'http://example.local',
'root' => '/Users/YOUR_USERNAME/Sites/example.local',
'path-aliases' => array(
'%dump-dir' => '/tmp',
'%files' => '/Users/YOUR_USERNAME/Sites/example.local/sites/default/files',
'%drush-script' => 'PATH/TO/drush.php',  //example -> /Users/YOUR_USERNAME/.composer/vendor/bin/drush.php
)
);
//production alias - details of you remote server 
$aliases['com'] = array(
'remote-host' => 'SERVER_IP_ADDRESS',
'remote-user' => 'USERNAME_ON_SERVER', // example -> www-data
'root' => 'SITE ROOT', // example -> /var/www/html/example.com/public_html'
'uri' => 'https://example.com',
'os' => 'Linux',
//If you use keys for ssh, add path to you ssh private key (on your local machine) 
'ssh-options' => '-o PasswordAuthentication=no -i /home/YOUR_USERNAME/.ssh/id_rsa',
'path-aliases' => array(
'%dump-dir' => '/tmp',
'%files' => '/var/www/html/example.com/public_html/sites/default/files',
'%drush-script' => 'PATH/TO/drush.php', //example -> /home/YOUR_USERNAME/.config/composer/vendor/bin/drush.php
)
'php' => '/usr/bin/php', // PATH/TO/YOUR/PHP
'php-options' => '-d error_reporting="E_ALL^E_DEPRECATED"',
);
?>

The alias file explains to drush which project you want to work with. To clear the cache on your production site just run 'drush @example.com cc all'.

The next tutorial will look at how to sync your local database with your production database Drush aliases Part 2 - working with drush sql-sync.