[SOLVED] How to use Drush Alias for website maintenance tasks from the command line?

July 20, 2017 - 13:17

Drush is a command-line shell for Drupal which enables you to perform many website maintenance tasks and administrative tasks from the terminal instead of using the Drupal administrative interface. Drush aliases allow you to run drush commands on your local server but actually the command executes on a remote server. This is a real time saver while working locally and you want to do something on the remote server.

Steps for downloading Drush aliases

  • Sign in to the Acquia Cloud, using your email id and password.
  • Click your name in the upper left corner, then click View profile.
  • Click Credentials from the Profile page.
  • Under Drush integration, click Download Drush aliases.
  • Extract the downloaded archive file into $HOME:
    $ tar -C $HOME -xf $HOME/Downloads/sitename.tar.gz

Setting up your local environment

Your local computer needs a little configuration settings to use an alias. Alias configuration files go into a .drush folder in your home directory or in the sites/all/drush directory of the site. Putting them in the .drush in your home directory means they will be accessible from anywhere. You can add multiple alias files here with the naming convention:

sitename.aliases.drushrc.php

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

$aliases['dev'] = array(
'parent' => '@parent',
'site' => 'sitename',
'env' => 'dev',
'root' => '/var/www/html/sitename.dev/docroot',
'remote-host' => 'example.myserver.com',
'remote-user' => 'sitename',
);

gives one alias which is accessed via the @sitename.dev drush argument. For example,

drush @sitename.dev status

Some tips for Drush Aliases usage

The powerful tasks that you can automate with drush aliases includes,

  • Sync the database from the staging environment to local environment:
    drush sql-sync @sitename.stage @sitename.dev 
  • Sync the files directory from production environment to development:
    drush sql-sync @sitename.prod @sitename.dev
  • Backup your entire production install (files & database):
    drush archive-dump @sitename.prod --destination=example.tgz
  • Restore the backup to your development environment:
    drush archive-restore example.tgz
  • Sync the sites/default directory on production environment to development environment using the created path-alias:
    drush rsync @sitename.prod:%default @sitename.dev:%files
  • Clear cache on the remote staging environment from development:
    drush @sitename.stage cc all

Post your comments / questions