Blog Hero Image

Drupal and Drush

Oct 22, 2010
Eric Aguayo
Eric Aguayo

Easy Drupal Management

Drupal is an open source PHP based CMS that allows easily creating web applications, plug-in themes and creating custom functionality. One of the main advantages of Drupal is that there is a large list of contributed modules available at the Drupal.org site that almost any feature needed is already developed or being developed by the community. Most importantly, is that when any bug is found on any module, developers can report the issue and contribute patches so the module can be updated by the maintainers. When a new release of a module becomes available it is reported on the administration interface so developers can update the site. However, there are tasks that are not so trivial that can be performed with the out-of-the-box functionality of the Drupal core, like the task of updating Drupal modules. Fortunately there is a Drupal tool called Drush (a short name for Drupal Shell), which is a command line application tool used to enable, disable, uninstall, update many aspects of Drupal such as modules, themes, profiles or translations.

Besides the package management features of Drush, there are other contributed modules that can be used to provide additional commands. There is a Drush module that provides tools for applying and rolling patches which can be useful when extending functionalities of modules. Several other modules can be found for providing commands for setting and managing different environments. There is a huge set of Drush related modules on the Drupal.org site which allows controlling almost any component in Drupal such as blocks, users, nodes, modules, permissions, etc. In addition, there is a contributed module called Drupal Terminal which allows executing commands within the web interface.

Drush allows controlling almost every aspect of the Drupal administration interface from the command line, so one might wonder how it can be useful if it can be done from the web interface. A command line interface is not as user-friendly as a web interface but it is one of the most flexible and economical ways for developers and site maintainers to query for information and execute commands. Drush is very useful, not only for development, but also for routine maintenance tasks. A theme developer will usually need to flush the caches to see some changes reflected which can be done from the command line instead of going to the web interface to clear the caches every time it is needed. Further, Drush allows specifying the cache tables that need to be flushed. Drush is very useful as well for performing remote calls to the commands to get information or perform some actions over a remote server.

Drush has proved to be a very useful tool when working with Drupal projects, especially when working in a Linux environment; bash scripts can be written to automatically perform several complex tasks such as module updates or run the cron.php batch job under specific conditions such as when the number of nodes to be processed on some queue. In general, Drush has opened a huge set of possibilities to build and maintain web applications in Drupal.

Keeping Drupal Up to Date

One of the main reasons for the increasing popularity of Drupal is its extensibility and modularity which allow developers to implement new features and contribute them to the community so it can be maintained and reviewed by other developers. There is a huge volume of contributed modules on the community site so almost any feature needed is very likely to be already implemented. Bug fixes and feature requests are continuously being added to the contributed modules so it is important to keep modules up to date all the time, this task is even more important when security updates are released.

Drush allows executing commands from the command line and it can be used on Windows, Mac or Linux platforms. This tool gives you an advantage when creating a script to automatically execute several tasks for maintenance purposes. In this particular case, I have created a simple bash script that automatically search for updates and install them. The following code does the trick:

#!/bin/bash
drush -r ~/workspace/drupal/trunk/ vset site_offline 1
drush –r ~/workspace/drupaltest sql-dump --result-file=db_backup.sql
drush –r ~/workspace/drupaltest –-yes up
drush -r ~/workspace/drupal/trunk/ vset site_offline 0

The vset command allows assigning some value to a Drupal system variable, in this case we are indicating to put the site in maintenance mode before processing the updates. In most of the cases, it will not be necessary to put the system in maintenance mode when updating the modules by using Drush since the modules get updated instantly, but if you are a bit worried then include the first and last lines in your script. The second line executes the sql-dump command which allows getting a database backup so it can be reverted in case anything goes wrong which is not likely to happen. Next, the up command is executed to download and install updates for contributed modules whenever they become available. The --yes option bypasses any yes/no question by answering yes automatically so the script can be executed automatically. The up command is a compound command that not only downloads the updated files but also backups the previous files under folder named backups on the root folder of the Drupal installation, the location of the backups folder can be customized as well. Also, the up command automatically calls the update.php script so any database query needed can be called. Finally, we set the site to online mode by unsetting the site_offline variable. This simple script can be run manually or scheduled through the crontab command to be executed regularly.

Latest insights