A beginner's guide to Drush
Drush is a command-line shell and scripting interface for Drupal. It makes my life a lot easier because I don't have to manage all the websites from Drupal's admin pages, but I can give commands to the server's command line. It helps you perform admin tasks using one or few commands in the terminal.
You need to SSH access to your server and write all commands to the terminal. PuTTY is very good tool for Windows and OS X / Linux got their own terminal + ssh software so you can just start by making a connection to your server. If you don't know how to use ssh then you probably don't want to play with drush.
Download latest stable release using the code below:
wget -O drush.phar https://github.com/drush-ops/drush-launcher/releases/download/0.6.0/drush.phar
No one likes to use long commands like
php drush.phar <drush_command> so let's make it work as normal commands. If you have
root access to your server then copy
chmod +x drush.phar sudo mv drush.phar /usr/local/bin/drush
If you don't have special rights to a server, you can add
export PATH=$PATH:</path/to/file> to your
~/.bashrc file (if your system use bash).
Download, enable and uninstall modules
Go to your Drupal installation folder.
You can download and enable any module from Drupal.org and you can enable multiple modules at once.
I use this "one liner" to activate all the necessary modules for new site (Drush download the modules automatically):
drush en pathauto ckeditor views jquery_update module_filter transliteration l10n_update admin_menu admin_menu_toolbar imce less html5_tools menu_block views_ui ctools token libraries elements field_ui overlay image contextual taxonomy
drush en enable module. You can also only download modules without activating them by using
drush dl (pm-download).
drush dis (pm-disable) is a command to uninstall. Drush can't delete modules so you have to do that manually (
rm -rf /<your_drupal_folder>/sites/all/modules/<module>).
Update Drupal with Drush
See main information and status of the site:
Before I found Drush it was a pain in the ass upgrading Drupal core and all enabled modules. Now you can just write one simple command and watch how Drush does all the work for you.
Use the command below to see all available updates. Before using the update command you should clear all the cache files from Drush and Drupal.
drush rf drush cc all drush up
Command update (
up) updates all modules pending, but you can also update only one or a few modules at the time. If you're updating a very important site, then you should first take a backup from SQL database and then update all modules one by one so if something goes wrong you know which module is the most problematic.
-y skips the confirmation question.
drush up drupal -y drush up module1 module2 -y
That command also updates your database if needed, but you can also do it without updating modules.
Drush always takes backups from modules and core before upgrading them, so if a new version is not good for you, you can always switch back to old ones.
See all the enabled modules
drush pm-list --type=Module --no-core --status=enabled
If you want to find certain module from the list:
drush pm-list --type=Module --no-core --status=enabled | grep module
Information about certain module:
drush pm-info <module_name>
Disable and uninstall modules
Command disable (
dis) only disables the module, but does not delete anything. Can also disable all modules depending on disabled module.
Command pm-uninstall disable module and delete all information from SQL. Module is still under all/modules after pm-uninstall but all data is gone.
drush dis module1 module2 -y drush pm-uninstall module -y
If Drupal is broken and you can't disable modules normally:
drush sql-query "UPDATE system SET status = '0' WHERE name = 'module_name'" drush sql-query "DELETE FROM cache_bootstrap WHERE cid = 'system_list'"
How to disable all non-core modules
drush pml --no-core --type=module --status=enabled --pipe | xargs drush -y dis
Drush Lock: How to freeze the version of a module during 'drush up'
Every so often I will find myself needing to update lots of Drupal modules but not one in particular.
drush up --lock=views_slideshow drush up --unlock=views_slideshow
Drush can backup your database, code and files into a single file.
archive-dump (ard) and
drush ard default --destination=/backups/mysite.tar
You can also backup only your database:
drush sql-dump > /path/to/backup_dir/database-backup.sql
Recovering the administrator password
drush upwd --password="password1234" admin
You can also use that command for any other user.
Unlock a locked user account
Drupal locks the user account after five failed attempts. That is very good if someone tries to guess your password, but very bad if you tried and failed multiple times yourself. This command will unlock the user account which was locked because of flood / spam.
drush sql-query "delete from flood"
Create and delete users
drush user-create username --mail="[email protected]" --password="letmein" drush user-cancel username
Log all users out:
drush sql-query 'TRUNCATE TABLE sessions;'
Log out a specific user
drush sql-query 'DELETE FROM sessions WHERE uid = 2;'
Update all Drupal websites
I made a script which I use to update all hosted Drupal websites at once. Very simple script, but still so powerful and saves a lot of time. You can find this script at Github.
#!/bin/sh # Author: Timo Anttila, [email protected] cd /www/drupal for d in ./* ; do (cd "$d/www/sites" && drush up -y && drush cc all); done chown -R www-data.www-data /www/drupal