Dealing with Drush

As a longtime Drupal site builder, developer and maintainer, I can't imagine doing my work without Drush. Often called a "Swiss Army Knife" for Drupal, Drush is a command line shell and scripting interface for doing all sorts of handy administrative tasks without touching the Drupal web interface. Features like site aliases, shell aliases, and quick ways to execute common site building and development tasks like syncing databases between environments, running cron, clearing caches, and enabling or disabling modules make Drush an indispensable tool that can greatly streamline and speed up your workflow.

Therefore, as folks familiar with Drupal are starting to explore Backdrop, it's not infrequent for their first question to be, "But can I use Drush?" Luckily, the answer is yes!--well, kinda. Because the Backdrop community is largely made up of folks who work with Drupal, the importance of Drush is well understood. As a result, Drush integration is one of the fastest moving contributed projects for Backdrop; it feels like every day, a new Drush command is working with Backdrop.

At my cooperative we're currently creating or supporting sites built on Drupal 6, 7, and 8 and Backdrop. Some of our clients' servers don't support anything newer than Drush 6, so if we're working with those sites remotely, we need Drush 6 installed locally, too. And finally, we've been bitten a few times by a weird gotcha where Drush destroys your git repository and other non-Drupal files when you run drush up drupal. Since we've primarily experienced that disaster when using Drush 8, we're inclined to stick with Drush 7 for most work, only using Drush 8 where it's required or known to work best (as with Backdrop), with Drush 6 as a backup when we need it for remote servers.

It's possible to run multiple versions of Drush in the same environment; however, one of my Drush pet peeves is that it has never been easy to figure out the right way to install it and keep it up to date. If you search the web for Drush installation instructions, you'll find tons of outdated documentation and a seemingly endless array of choices for how to get Drush installed: wget, composer, pear, git clone, Debian package, HALP! It's not uncommon to find a Drupal web server with multiple outdated versions of Drush installed in different ways and abandoned once someone gave a newly favored installation method a shot.

Therefore, we've put some time in figuring out a reliable, easily replicable process for installing all three versions of Drush and switching between them with ease. We've documented it in our public wiki both for internal use and for public reference; we'll keep it up to date there, but as of this writing, this solution for installing and configuring Drush is working nicely for us.

Many thanks to Lullabot's Karen Stevenson and Triquanta's Marc van Gend, whose work made mine much easier!

Install Drush 6, 7 and 8 for all users

I've followed the "To install for all users on the server" instructions under "Composer - One Drush for all Projects" in the Drush 7 installation documentation, adapting those instructions below for different versions of Drush. If you don't have Composer installed, you'll have to install it globally first. Run the following commands as root or using sudo.

Drush 6

git clone https://github.com/drush-ops/drush.git -b 6.6.0 /usr/local/src/drush6
cd /usr/local/src/drush6
composer install
ln -s /usr/local/src/drush6/drush /usr/bin/drush6

Drush 7

git clone https://github.com/drush-ops/drush.git -b 7.2.0 /usr/local/src/drush7
cd /usr/local/src/drush7
composer install
ln -s /usr/local/src/drush7/drush /usr/bin/drush7

Replace "7.2.0" with the tag of the most recent stable 7.x release (see https://github.com/drush-ops/drush/releases).

Drush 8

git clone https://github.com/drush-ops/drush.git -b 8.0.5 /usr/local/src/drush8
cd /usr/local/src/drush8
composer install
ln -s /usr/local/src/drush8/drush /usr/bin/drush8

Replace "8.0.5" with the tag of the most recent stable 8.x release (see https://github.com/drush-ops/drush/releases).

You should now be able use the drush6, drush7 or drush8 commands to run each specific version of Drush.

EXTRA TIP: To update a Drush installation installed in this manner, run the following commands within the /usr/local/src/ directory:

git pull origin master
git checkout tags/VERSION -b VERSION
composer install

Replace VERSION with the most recent stable release of the Drush version you're updating.

Install Backdrop Drush commands

In order for Drush to work with Backdrop, you must install the Drush Backdrop commands within the Drush 8 installation's commands directory.

Assuming that Drush 8 is installed in /usr/local/src/drush8, as specified above:

git clone https://github.com/backdrop-contrib/drush.git /usr/local/src/drush8/commands/backdrop

Create script to automatically switch Drush versions based on git configuration setting

As per this excellent guide from Marc van Gend of Triquanta, this script lets you switch between versions of Drush on a project-by-project basis using git configuration settings.

The script, which lives at /usr/bin/drush and is therefore called whenever someone runs the drush command:

#!/bin/bash
version=$(git config --get drush.version)
if
  [ "$version" = '8' ];
then
  drush8 "$@"
elif
  [ "$version" = '6' ];
  drush6 "$@"
else
  drush7 "$@" fi

Set drush.version git variable on a per-project basis

In this configuration, Drush 7 is the default version of Drush. For the script above to automatically switch to a non-default version of Drush for a given project, set the drush.version value in the git repository for the project by running git config drush.version . For example:

$ drush --version

    Drush Version : 7.2.0

$ git config drush.version 8
$ drush --version

    Drush Version : 8.0.5

Note that as of this writing, Backdrop doesn't support drush --version; instead, use drush status to find out what version of Drush you're using on a Backdrop site.

Update Drush remote site aliases to use correct Drush scripts

If you've got Drush site aliases set up for remote sites on servers where multiple versions of Drush are available, update your aliases.drushrc.php file to include the specific drush script that should be used on the remote site. See the example.aliases.drushrc.php file if you need help creating your aliases.drushrc.php file or learning about %drush-script.

An example aliases.drushrc.php file with %drush-script specified:

$aliases['backdrop'] = array(
  'remote-host' => 'fake-dev-server.palantetech.coop',
  'remote-user' => 'jack',
  'root' => '/var/www/dev/backdrop',
  'uri' => 'backdrop.palantetech.coop',
  'path-aliases' => array(
    '%drush-script' => '/usr/bin/drush8',
   )
);
$aliases['drupal7'] = array(
  'remote-host' => 'fake-dev-server.palantetech.coop',
  'remote-user' => 'jack',
  'root' => '/var/www/dev/drupal7',
  'uri' => 'drupal7.palantetech.coop',
  'path-aliases' => array(
    '%drush-script' => '/usr/bin/drush7',
  )
);