Drush (Drupal Shell)

Drush (Drupal Shell) esembrat3
Drupal Version
Tags

Drush (Drupal Shell) is a great Drupal "module" to accomplish many administrative tasks via the command-line. It is not a module in the normal sense, because it's not enabled/disabled from the Drupal admin. Instead, it actually lives anywhere on your local system and is run like any other local executable. Here are some of its key features from the Drush Drupal.org page:


A few core drush features:

  • The Drush Package Manager allows you to download, enable, disable, uninstall, update modules/themes/profiles/translations from the command line in a very simple way (apt-get style) - just type e.g. drush dl views and drush enable views in a Drupal directory to install the Views project!
  • Additionally, the Drush Package Manager also allows you to update all your modules with just one command - drush update.
  • Drush Core: several useful utilities for site administrators and developers (e.g. drush cron or drush cache clear)
  • Drupal SQL Commands help you issue queries to any of your databases (i.e. is multi-site aware) and even helps you migrate databases between your environments.
  • Drush simpletest commands make it easy to run unit tests from the command line and email results to your dev team.
  • Drush now supports aliases which let's you perform actions on separate Drupal installations.  This is very useful for multi-site configurations.

Drush Guides and Resources

Updating Modules via Drush

Updating Modules via Drush esembrat3
Drupal Version

If you have to perform module updates on your Drupal site, then Drush is the way to go. Drush will help you to update all modules on your site(s) quickly and painlessly.

Prerequisites

  • SSH/shell access
  • Somewhat higher than basic Unix/Linux command-line experience

Process

Drush has many commands and options. These commands need to be run from the directory where the settings.php file for your site is, i.e.

/path/to/drupal/sites/default

/path/to/drupal/sites/gpburdell.gatech.edu

For the purpose of updating your modules you will basically use it like below:

drush update

This is interactive and will give you a listing of all the modules that need to be updated for a given site. You can then choose to update them all or continue.  Unless you are certain you want to do all of your updates at once, I suggest you use this command to see what updates are needed, then use the below command to update a few of the modules separately at one time. For short you can also use use "drush up".

drush update modulename0 modulename1

This let's you specify a list of modules you want to update.

drush updatedb

This command will perform any updates to the database schema for your modules.  This is run automatically after every Drush update. For short you can also use "drush updb".

If you have multi-sites then after you do a Drush update you may have to move to your other multi-site folder to do a drush updatedb to update any changes to the database schema.

Best Practices

  • Limit the number of modules you use on your Drupal sites as much as possible.
  • Try to keep modules that are shared by more than one multi-site in the sites/all/modules folder.
  • Always check the release notes for any new modules to see if installing them will require any other changes or dependencies.

Gotchas

  • Drush expects a backup directory to be present in the root of your Drupal installation like /path/to/drupal/backup where it backs up the previous version of the module to.  Make sure you have write access to this directory.
  • If you had any modified files in the previous module directory you will need to copy those back to the correct place after the module is updated.  Luckily everything will be in the backup directory.
  • Depending on permissions you may need to run Drush as the super user.  Ask your system administrator for sudo access if needed.

Using Drush on OIT Web Hosting

Using Drush on OIT Web Hosting aa17
Drupal Version

If you are hosting a Drupal website on OIT Web Hosting, you can SSH into your host, but you cannot run the command-line tool Drush from your SSH session because OIT doesn't allow PHP to be run directly from the SSH command line of Web Hosting accounts.  However, this doesn't mean that you cannot use Drush at all with OIT Web Hosting – you just have to work around this limitation.

Disclaimer

THESE INSTRUCTIONS ARE COMPLETELY, 100%, NO, EVEN 1000%, NOT SUPPORTED IN ANY OFFICIAL WAY BY OIT WEB HOSTING.  No warranty expressed or implied.  Always back up your site and database(s) before trying these instructions.  These instructions, though tested on several sites, may break your site, cause data loss, or even make your dog not like you any more.  Be careful!

Process

Essentially, you will run Drush locally on your computer, but have it operate on an OIT Web Hosting site.  To do this, you will need to:

  1. Establish an SSH tunnel for the database connection, and
  2. Mount your site's files over ssh via SSHFS.

Note: The instructions presented below are for using Drush on a Mac. If you want to try this from a Windows computer, both SSH (<) and SSHFS (WebDrive) clients are available for Windows.

Prerequisites

  • Drush, installed locally – Installation instructions are available at the Drush Docs site.  If you have the Acquia Dev Deskstop installed, you already have Drush.  You can also install it via homebrew.

  • An SSH client – Suggested products are:

    • On a macOS / Mac OS X system: Terminal or iTerm
    • On a Windows system: SecureCRT
  • An SSHFS client to let you mount a remote filesystem over SSH – Suggested products are:

  • Access to your site via SSH – You must know the password for your site's (unfortunately named) FTP Login account, which you can set/reset via your web hosting administration panel in the Web Hosting Settings screen.  (Note as of the 2015/16 Plesk upgrades, you can now set up and use an SSH key instead of needing the SSH/FTP password.)

  • Be on campus or have access to the campus VPN Service, as OIT Web Hosting does not allow SSH directly from off-campus.

Steps to Drush Bliss

In the instructions below, foo.gatech.edu is a placeholder for your site's actual hostname, and foo is a placeholder for your site's SSH/FTP account. Replace these with your site's hostname and SSH/FTP account where appropriate.  Also, these instructions assume you are using Transmit as your SSHFS client.  Configuration screens for WebDrive will be similar.

  1. Install Drupal, if you haven't already... via source, Installatron, etc.
  2. Edit your httpdocs/sites/default/settings.php file, changing the databases array's host and port entries to read:
     'host' => php_sapi_name() == 'cli' ? '127.0.0.1' : 'mysql.localhost',
     'port' => php_sapi_name() == 'cli' ? '3307' : '3306',
  3. Create a file named dbip.php in your sites httpdocs directory: 
    <?php 
    if (array_key_exists('REMOTE_ADDR', $_SERVER) 
       && ((substr($_SERVER['REMOTE_ADDR'], 0,7) == '128.61.') 
       || (substr($_SERVER['REMOTE_ADDR'], 0,8) == '130.207.') 
       || (substr($_SERVER['REMOTE_ADDR'], 0,8) == '143.215.') 
       || (substr($_SERVER['REMOTE_ADDR'], 0,9) == '192.93.8'))) 
       { 
        echo gethostbyname('mysql.localhost'); 
       } 
    ?>
    
  4. On your local machine, in a terminal window, open up an SSH tunnel through your Web Hosting server to your Web Hosting account's database host:
    ssh -N foo@foo.gatech.edu -L 3307/`curl -s http://foo.gatech.edu/dbip.php`/3306
  5. Use an SSHFS client to mount your site's httpdocs directory. For Transmit, use the SFTP connection and choose Mount as Disk:


     
  6. In a terminal, navigate to the mount point ( /Volumes/foo.gatech.edu/ ) and run drush status to test your configuration. You should see the message "Database : Connected and Drupal bootstrap: Successful."
  7. When you are finished using Drush on your site, remember to close (control-C) the SSH command doing the tunneling and unmount your site's filesystem (drag it to the Trash).