Blog

Updating all of your Drupal Sites at Once - aka Lazy Person's Aegir

Aegir is an excellent way to manage multi site drupal instances, but sometimes it can be a bit too heavy. For example if you have a handful of sites, it can be overkill to deploy aegir. If there is an urgent security fix and you have a lot of sites (I am talking 100s if not 1000s) to patch, waiting for aegir to migrate and verify all of your sites can be a little too slow.

For these situations I have a little script which I use to do the heavy lifting. I keep in ~/bin/update-all-sites and it has a single purpose, to update all of my drupal instances with a single command. Just like aegir, my script leverages drush, but unlike aegir there is no parachute, so if something breaks during the upgrade you get to keep all of the pieces. If you use this script, I would recommend always backing up all of your databases first - just in case.

I keep my “platforms” in svn, so before running the script I run a svn switch or svn update depending on how major the update is. If you are using git or bzr, you would do something similar first. If you aren’t using any form of version control - I feel sorry for your clients.

So here is the code, it should be pretty self explanatory - if not ask questions via the comments.

#!/bin/sh
# Update all drupal sites at once using drush - aka lazy person's aegir
#
# Written by Dave Hall
# Copyright (c) 2009 Dave Hall Consulting http://davehall.com.au
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# Alternatively you may use and/or distribute it under the terms
# of the CC-BY-SA license http://creativecommons.org/licenses/by-sa/3.0/
# Change this to point to your instance of drush isn't in your path
DRUSH_CMD="drush"
if [ $# != 1 ]; then
    SCRIPT="`basename $0`"
    echo "Usage: $SCRIPT path-to-drupal-install"
    exit 1;
fi
SITES_PATH="$1"
PWD=$(pwd)
cd "$SITES_PATH/sites";
for site in `find ./ -maxdepth 1 -type d | cut -d/ -f2 | egrep -v '(.git|.bzr|.svn|all|^$)'`; do
    if [ -f "${site}/settings.php" ]; then
        echo updating $site
        $DRUSH_CMD updatedb -y -l $site
    fi
done
# Lets go back to where we started
cd "$PWD"

OK, so my script isn’t any where as awesome as aegir, but if you are lazy (or in a hurry) it can come in handy. Most of the time you will probably still want to use aegir.

Notes:

Make sure you make the script executable (hint run chmod +x /path/to/update-all-sites)

If you don’t have drush in your path, I would recommend you add it, but if you can’t then change DRUSH_CMD="drush" to point to your instance of drush - such as DRUSH_CMD="/opt/drush/drush".

Thanks to Peter Lieverdink (aka cafuego) for suggesting the improved regex.