Quick and Dirty Script to Upgrade Drupal

I run several Drupal-powered websites, and I recently had to upgrade Drupal in order to fix a vulnerability that had been reported. I hate doing the same task over and over so I looked for a way to automate that from the command line. I came up with a solution that worked for me, and I wanted to share it.

First, head to /admin/reports/updates on your website and verify that your Drupal installation needs to be updated. Next, run these commands from the UNIX shell:

#
# Change accordingly based on the version of Drupal you are upgrading to
#
VERSION="6.31" 

mkdir -p ~/tmp/drupal/OLD && cd ~/tmp/drupal/
wget http://ftp.drupal.org/files/projects/drupal-${VERSION}.tar.gz \
   && tar xfvz ./drupal-${VERSION}.tar.gz

#
# Replace with whatever the root directory of your website is
#
cd webserver_root 

#
# Move each file to the "OLD" directory and move in the upgraded version
#
for FILE in `echo index.php includes misc modules profiles scripts themes update.php`
do
     NEW=~/tmp/drupal/drupal-${VERSION}/${FILE}
     OLD=~/tmp/drupal/OLD
     echo "NEW: ${NEW} OLD: ${OLD}"
     mv ${FILE} ${OLD} && mv ${NEW} .
     echo "Done with ${FILE}"
done

At this point, your Drupal installation has been updated, and the files that you have replaced are in $HOME/tmp/drupal/OLD/ in case you need to get them back. Finally, you should run /update.php through your web browser, just in case there are any database updates involved.

For bonus points, why not check your entire Drupal installation into a Git repository? That can be done on a quick and dirty basis as follows:

#
# Replace with your website's document root
#
cd webserver_root 

#
# Create a new Git repo
#
git init

#
# Add everything form the current directory on down
#
git add .

#
# Commit the change
#
git commit -m "Initial checkin"

Having your website in Git gives you the added benefit of being able to perform multiple Drupal upgrades over the life of your website and track those changes.

Even better, push a copy of your Git repository offsite onto a service like BitBucket. Should your website ever become hacked, you'll have what amounts to an offsite backup of your website contents. (The database needs to be backed up separately, of course)

If you read this far why not follow me on Twitter or Facebook?

5
Average: 5 (1 vote)
Your rating: None