Blog Hero Image

Drupal and Drush Part 2: Keeping Drupal Up to Date

Oct 22, 2010
Eric Aguayo
Eric Aguayo

Drupal is an open source PHP based Content Management System which allows you to easily build and customize web applications. Currently, there are innumerable websites on the internet built on Drupal. 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.

There is a tool in the Drupal community called Drush (a short name for Drupal Shell) which simplifies the process of updating modules and backing up the database and files of previous versions. The Drush tool 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:

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.

Now Check out: Drupal and Drush Part 1: Easy Drupal Management

Latest insights