Using Drush for your Drupal site
Drush helps you to perform many administrative tasks for Drupal including backups, restores & more. Learn how to use this powerful tool's features in this article.
This article describes how to use the Drush command-line tool to do common Drupal administrative tasks.
Important
Before you can use Drush, you must install it on your account. For information about how to do this, please see this article.
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.
Tip
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:
-
Log in to your account using SSH.
-
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.
Note
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.
-
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
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
Tip
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.
Important
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:
Create a custom php.ini file in the public_html directory that enables the required settings.
Add the following line to the /home/username/.bashrc file, where username represents your hosting.com account username:
export PHP_INI=~/public_html/php.ini
- 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:
-
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
-
If the module has dependencies, type
y
to install them, and then press Enter. -
When Drush successfully enables the module, it displays [ok].
Disabling modules
To disable a Drupal module, follow these steps:
-
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
-
At the Do you really want to continue? prompt, type
y
and then press Enter. -
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:
-
In the directory where Drupal is installed, type the following command:
drush up
-
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:
-
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.)
-
To restore the database backup, type the following command:
drush sql-cli < db-backup.sql
Warning
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:
-
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.
Important
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.
-
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.
Important
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.
Related Articles
Updated about 17 hours ago