Got a new web project? Request a quote

Drush pro for the lazy: Aliases

.Drush aliases allow us to execute commands on a remote site from the local console. It is the perfect tool for the lazy drupal developer. With drush aliases I rarely login to a remote server, I execute all the drush commands from my local console. It is also a great for workflow automation. Here is an example command using an alias: drush cc @dev.site This command will execute the cc (clear caches) on the @dev.site as it is defined in the alias. For this to work, you will have to:

  1. Have passwordless login in the target system
  2. Set up the site alias

Passwordless login

For passwordless login, we need to copy our public key to the remote machine: ssh-copy-id user@IP Then try to login to the ssh server without a password. Once this is set up, we can set up our aliases.

Set up a drush alias

A drush alias is an array that defines a site. It includes things like the remote site address, the db user etc. Drush reads this array so that it can login to the remote site and execute drush commands via ssh. Here is a sample alias array:

<?php
$aliases
['dev.site'] = array (
 
'remote-host' => 'dev.site.com',
 
'remote-user' => 'devsite',
 
'root' => '/home/devsite/public_html',
 
'uri' => 'http://dev.site.com',
);
?>

Or, use the following snippet if drush is not installed in the target server:

<?php
$aliases
['dev.site'] = array (
 
'remote-host' => 'dev.site.com',
 
'remote-user' => 'devsite',
 
'root' => '/home/devsite/public_html',
 
'uri' => 'http://dev.site.com',
 
'databases' =>
  array (
   
'default' =>
    array (
     
'default' =>
      array (
       
'driver' => 'mysql',
       
'database' => 'dev_db',
       
'username' => 'dev_db_user',
       
'password' => 'dev_db_pass',
       
'host' => 'localhost',
       
'prefix' => '',
       
'collation' => 'utf8_general_ci',
      ),
    ),
  ),
);
?>

This array must be placed in a file with the following name: example.aliases.drushrc.php Put this file in your ~/.drush or in the sites/all/drush folder. You can obtain the alias definition above by logging into the target site and executing the command: drush site-alias --with-db --show-passwords --alias-name=dev.site @self

Have fun

You can perform cool actions like copying the database from the live site to the dev from your local console: drush sql-sync @live.site @dev.site