Knowledge Base

How to use the Drush tool for Drupal

This article describes how to use the Drush command-line tool to do common Drupal administrative tasks.

About Drush

Drush is a tool for Drupal that enables you to do common administrative tasks and basic scripting from the command line. For example, you can back up and restore Drupal sites, install modules, and more.

Drush is a command-line tool, so it helps if you already have some basic familiarity with the Linux command-line environment. If you have never worked in the Linux command-line environment before, you can learn the basics by reading this article.

Verifying the Drush installation

To verify that Drush is installed and functioning correctly on your account, follow these steps:

  1. Log in to your account using SSH.
  2. Change to the directory where Drupal is installed. For example, if you installed Drupal in the document root directory, type the following command:
    cd ~/public_html

    Alternatively, if you installed Drupal in a subdirectory, change to that directory instead.

    All of the following commands in this article assume that you are running Drush in the directory where Drupal is installed. Most of these commands will not work if you run Drush from a different directory.
  3. Type the following command:

    drush status

    Drush should display information about your Drupal installation that resembles the following output (specific values will vary):

    Drupal version                  :  7.26                             
    Site URI                        :  http://default                   
    Database driver                 :  mysql                            
    Database hostname               :  localhost                        
    Database username               :  user_drup502                 
    Database name                   :  user_drup502                 
    Database                        :  Connected                        
    Drupal bootstrap                :  Successful                       
    Drupal user                     :  Anonymous                        
    Default theme                   :  bartik                           
    Administration theme            :  seven                            
    PHP configuration               :  /usr/local/lib/php.ini           
    Drush version                   :  5.8                              
    Drush configuration             :                                   
    Drupal root                     :  /home/username/public_html/drupal
    Site path                       :  sites/default                    
    File directory path             :  sites/default/files              
    Temporary file directory path   :  /tmp
    Drush is installed on all shared hosting servers by default. If Drush does not work on your account, please open a support ticket on the A2 Hosting Customer Portal at https://my.a2hosting.com.

Configuring Drupal modules

A common Drupal administration task is module configuration. This is often done through Drupal's web administration interface. However, you can also quickly and easily work with Drupal modules on the command line.

For example, if you need to download and enable several modules, it is much quicker to do this on the command line with Drush than it is to click through the various Drupal web administration pages.

Downloading modules

To download a Drupal module, type the following command in the directory where Drupal is installed. Replace MODULE with the name of the module that you want to download:

drush dl MODULE
  • To download several modules at once, just add them to the end of the command. For example, to download the views and token modules at the same time, type drush dl views token.
  • To view the Drupal module project page and search for modules, please visit http://drupal.org/project/project_module.

Some Drupal modules, such as composer_manager, have additional PHP configuration requirements (for example, allow_url_fopen must be enabled). On some servers, you may receive an error message when you try to download or enable these modules. To resolve this problem, follow these steps:

  1. Create a custom php.ini file in the public_html directory that enables the required settings.
  2. Add the following line to the /home/USERNAME/.bashrc file, where USERNAME represents your A2 Hosting account username:
    export PHP_INI=~/public_html/php.ini
  3. To make the new settings take effect immediately, type the following command:

    source ~/.bashrc
Enabling modules

To enable a Drupal module that you have downloaded, follow these steps:

  1. In the directory where Drupal is installed, type the following command. Replace MODULE with the name of the module that you want to enable:
    drush en MODULE
  2. If the module has dependencies, type y to install them, and then press Enter.
  3. When Drush successfully enables the module, it displays [ok].
Disabling modules

To disable a Drupal module, follow these steps:

  1. In the directory where Drupal is installed, type the following command. Replace MODULE with the name of the module that you want to disable:
    drush dis MODULE
  2. At the Do you really want to continue? prompt, type y and then press Enter.
  3. When Drush successfully disables the module, it displays [ok].

Checking for Drupal updates

To check for available updates for your Drupal site, follow these steps:

  1. In the directory where Drupal is installed, type the following command:
    drush up
  2. Drush checks if there are updates available:

    • If there are no updates available, Drush displays No code updates available and exits.
    • If there are updates available, at the Do you really want to continue? prompt, type y and then press Enter. Drush updates the Drupal installation.

Backing up and restoring a Drupal database

Drush makes it easy to quickly back up and restore Drupal databases. To do this, follow these steps:

  1. In the directory where Drupal is installed, type the following command:
    drush sql-dump --result-file=db-backup.sql

    Drush saves the Drupal database to the db-backup.sql file. (You can name the file anything you want.)

  2. To restore the database backup, type the following command:

    drush sql-cli < db-backup.sql
    Be careful! This command completely overwrites the existing Drupal database with the database contained in the backup file.

Backing up and restoring a Drupal site

Backing up and restoring entire Drupal sites is easy with Drush. A backup includes all Drupal site files, databases, and code in one compressed file.

To do this, follow these steps:

  1. In the directory where Drupal is installed, type the following command:
    drush archive-dump
    

    Drush backs up and compresses the Drupal site. When Drush completes the backup, it indicates the path and filename where the backup file is located.

    We strongly recommend you copy the backup file to a location that is not on the server. It is a sound administrative practice to maintain multiple backups in multiple locations.
  2. To restore the site backup, type the following command in the directory where Drupal is installed. Replace /path/filename.tar.gz with the complete path and name of the Drupal backup file:

    drush archive-restore /path/filename.tar.gz

Clearing Drupal caches

There are times, particularly during site development, when you may want to clear out Drupal's caches. To do this, type the following command in the directory where Drupal is installed:

drush cache-clear all

Rebuilding the Drupal search index

Drupal rebuilds its search index at fixed times according to a cron job schedule. However, you can instruct Drupal to immediately rebuild the search index. To do this, type the following command:

drush search-reindex

When you are prompted, type y to continue.

Depending on the size of your site, rebuilding the search index may take some time. You should try to limit index rebuilding to off-peak times.

More Information

For more information about Drush, please visit http://drush.org.