Drush Aliases Setup

Aliases simplify specifing a site in Drush.
Instead of this: drush username@myserver.com/path/to/drupal#mysite.com status 
we get drush @dev status A nice shortcut.

Setup an Alias for Drush

Create a directory called .drush in your home directory, if it doesn't exist already. Add the drush aliases file here.

mkdir ~/.drush
vi ~/.drush/aliases.drushrc.php

Add an entry like this for a dev site: 

<?php
$aliases['dev'] = array(
  'uri' => 'localhost/drupal_dev', // Replace localhost with the uri of your site
  'root' => '/www/sites/drupal/dev', // This is the Drupal root directory.
);
?>

That's it! Now if I want to run a drush command on my local dev site, I can type (from any directory) drush @dev status

Now set up aliases for some remote sites:

<?php
$aliases['dev'] = array(
  'uri' => 'localhost', // Replace localhost with the uri of your site
  'root' => '/Users/jsansbury/Sites/dev', // This is the Drupal root directory.
);
$aliases['stg'] = array(
  'uri' => 'stage.example.com',
  'root' => '/var/www/example.com',
  'remote-host' => 'stage.example.com',
  'remote-user' => 'username',
);
$aliases['live'] = array(
  'uri' => 'example.com',
  'root' => '/var/www/example.com',
  'remote-host' => 'example.com',
  'remote-user' => 'username'
,
);

?>

Next, set up a passwordless login to a remote server by typing the following commands on your local machine. 

ssh-keygen -t rsa  //this will generate a pair of private/public keys needed for passwordless login
ssh-copy-id remoteuser@remote.example.com 
after typing the password, you should log out from remote machine this time
ssh remoteuser@remote.example.com will not prompt for password

Great! Now I can run commands on a remote site without having to log in to that server.

 

Drush site alias groups

The 'site-list' option in the alias configuration file defines a group of aliases. Create a@remote alias that points to all remote sites. Open up that all.aliases.drushrc.php file again, andadd this to the bottom of it.

<?php
$aliases['remote'] = array(
  'site-list' => array('@stg', '@live'),
);
?>

Now you can do the following:

drush @remote status

To see a list of all site aliases available:

  drush sa --full 

Some alias-enhanced Drush commands

Let's run a few commands to verify that the aliases are working correctly and to get the hang of things.

See the status of the site. This should print a dozen+ rows of information:

drush @dev status

Print where the files directory is

drush dd @dev:%files

Switch to the files directory (note the backticks to express the output as part of the command):

cd `drush dd @dev:%files`

Dump the database, and then load the same database. Keep these handy especially when you're experimenting.

drush @dev sql-dump > `drush dd @dev:%dump`
`drush @dev sql-connect` < `drush dd @dev:%dump`

To see an example of a full Drush alias with the db_url specifing the database type:

# drush sa @backup --with-db --show-passwords drush sa @backup --with-db --show-passwords
$aliases['backup'] = array (
  'root' => '/var/www/sites/mysite',
  'uri' => 'sever/drupalsite',
  'remote-host' => 'localhost',
  'remote-user' => 'user',
  'databases' => 
  array (
    'default' => 
    array (
      'default' => 
      array (
        'driver' => 'mysql',
        'username' => 'user name',
        'password' => 'pass word',
        'port' => '',
        'host' => 'localhost',
        'database' => 'database name',
      ),
    ),
  ),
);