fromSeptember 2013
Article:

Drush: The Swiss Army Knife for Drupal

Part Two: The Long Blade and Saw
0

Hello again, young MacGyver!

Swiss Army KniveIn the previous issue you learned how to install Drush, Drupal, and contributed modules. If you missed it, make sure you go back and read Part One from the previous issue.

Updates

Now that you've successfully installed Drupal and extended it with some awesome contributed modules, it's time to apply a few updates. With Drush, it is easier by far than any method you might currently be using.

Let's get started: Make sure you are working from the root directory of your website. That would be the directory where you find index.php, and I'm going to assume that location for the remainder of this article.

Issue the following command:

drush pm-update

That command will check for new versions of core, themes, and all the contributed modules that are enabled on your site. A list of all available updates will be shown on the screen. Review the list and then press “y” at the prompt if you wish to proceed with the updates.

If you proceed with the updates, Drush will make a backup copy of all the out-of-date packages, download the new ones, and then run database updates, if any are required. It's all very quick and you don't even have to open an FTP client.

Alas, sometimes things go awry; often, very awry. That's why Drush stores a backup copy of the updated packages for you. Should an update fail, it will restore the previous versions and notify you there was a problem. Or, if you need to restore manually, you can find the backups in your user's home directory under “drush-backups”.

Now let's say you only want to update Drupal, but none of the contributed projects. Easy enough: this time only check for Drupal core. Let’s use the shorter version of the command, which I prefer:

drush up drupal

The command “up” is short for “pm-update”. As in the first example, Drush will backup the installed version, replace it with the latest, and then run database updates, if any are required. In this case, we specified “drupal”, so Drush will only check for updates for Drupal core.

This works the same way for any package. Specify one or more projects in the same command and Drush will update only them. For example, you can update just Views and Ctools with one command:

drush up views ctools

In some situations you might not want to update Drupal core, but you do want to update all of the contributed packages. Simply add the “—no-core” parameter.

drush up —no-core

Another useful parameter is “—security-only” which, as it says, only updates packages that have a security release available. Knowing that little trick will save you time and, of course, keep your Drupal site healthy with secure code.

Archiving and Backups

Beyond keeping your site up-to-date, there might be an occasion where you need to archive your site in order to send it to another developer or client, or just create an impromptu backup.

Whatever the reason, you could package it up with only two commands. Start by dumping the database into a file by issuing the command “sql-dump”.

drush sql-dump —result-file=sites/default

The —result-file parameter is important; without it, your database will just print to screen and not save at all. Using this parameter, you can save the file anywhere, if you provide it a path. If you don't include a path it will assume you want to write it to a drush-backups directory within your user's home directory.

Assuming your user account has permission to write to the default site directory, the path provided in the command above will place your database dump in a logical place for the next person to find. If you can’t write it there, or prefer an alternative location, feel free to proceed with whatever works best for you.

Security note: The sites/default directory is a web-accessible location. The dump file should be removed immediately after you are done with the next step.

Next, use the archive-dump command to package up the entire codebase into a tar file.

drush archive-dump

If in the previous step you saved your database dump inside the default site directory, the archive generated with archive-dump will contain everything you need to restore it elsewhere. You can find this archive inside the drush-backups directory when you are ready to retrieve it. If you prefer to save it elsewhere, you can pass along a destination parameter:

drush archive-dump —destination=/var/www/backups

There are many more commands to be learned. Yes, you can always use drush help at the command line, but I insist you swing by http://drush.ws. This is a good go-to reference list that shows you what Drush can do “out of the box.”

Remember; the more you practice, the better you will become. Use Drush often enough and it will become instinct. Until next time fellow Drupalers!

Image: ©Istockphoto.com/malerapaso