A basic Drush tutorial
Drupal, Server // // Timo Anttila
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 connect to your server. If you don't know how to use ssh then you probably don't want to play with drush.
Install drush
Download latest stable release using the code below:
wget http://files.drush.org/drush.pharNo one likes to use long command like php drush.phar <drush_command> so let's make it work as normal commands. If you have sudo or root access to your server then copy drush.phar to /usr/local/bin/.
chmod +x drush.phar
sudo mv drush.phar /usr/local/bin/drushIf 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.
cd /<your_drupal_folder>/sitesYou 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 taxonomyCommand drush en enable module. You can also only download modules without activating them by using drush dl
drush emu is 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:
drush statusBefore I found Drush it was a pain in ass upgrading Drupal core and all enabled modules. Now you can just write one simple command and watch how Drush do all the work for you.
Use command below to see all available updates. Before using update command you should clear all the cache files from Drush and Drupal.
drush rf
drush cc all
drush upCommand update (up) updates all modules pending, but you can also update only one or a few modules at the time. If you're updating 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 -yThat command also updates your database if needed, but you can also do it without updating modules.
drush updbDrush always takes backups from modules and core before upgrading them, so if new version is not good for you, you can always switch back to old ones.
Disable and uninstall module
Command disable (dis) only disable 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 -yIf 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'"See all the enabled modules
drush pm-list --type=Module --no-core --status=enabledIf you want to find certain module from the list:
drush pm-list --type=Module --no-core --status=enabled | grep moduleBackup
Drush can backup your database, code and files into a single file.
Commands: archive-dump (ard) and archive-restore (arr).
drush ard default --destination=/backups/mysite.tarYou can also backup only your database:
drush sql-dump > /path/to/backup_dir/database-backup.sqlChange password for admin
drush upwd --password="password1234" adminYou can also use that command for any other user.
Force logout
Log all users out:
drush sql-query 'TRUNCATE TABLE sessions;'Log out a specific user
drush sql-query 'DELETE FROM sessions WHERE uid = 2;'