Drupal File Upgrade Bash Script

Replacing files during a Drupal upgrade can be tedious, especially for those of us who have to worry about SVN files in every directory. The following script automates this part of the Drupal upgrade, making it much faster and less error prone. I've been using the script in its current form for almost a year and it has served me well. And though most of my other bash scripts have been replaced by Drush commands, this script has proven better suited to my workflow than its Drush equivalent.

Overview

The script does the following:

  1. Deletes the current Drupal distribution files while preserving:
    • directories
    • SVN files
    • all files in the /sites directory tree
    • all files in /files directory tree
      (I have a number of sites using the old file directory location)
    • the Komodo (.kpf) project file
  2. Downloads and expands the Drupal distribution tarball if it has not already been done
  3. Copies the new Drupal distribution files to the target directory
  4. Removes the unrequired text files. e.g., CHANGELOG.txt, INSTALL.mysql.txt

One thing this script doesn't do is remove unused directories. On the few occasions directories are removed from a release I remove them manually, and in my experience there's no harm done when I've missed one. The overall time savings is well worth this small manual detail.

Usage

Exact usage will depend on how you set up scripts on your system. I have a scripts directory in my path, so the script is treated like a command.

Once you placed the script on your system, you'll need to create a directory for the Drupal distributions and update the script's SOURCE_DIRECTORY variable with the location.

Usage is simple:

  1. cd to the directory of your Drupal installation
  2. Issue the command: drupalupgrade {version number}
    e.g.: $ drupalupgrade 6.22 or $ drupalupgrade 7.4

If you don't have a scripts directory you'll need to place the script file in an appropriate directory and reference the path. The script CAN NOT be in the same directory you are upgrading.

Drupal File Upgrade Script

(It's also available below as a textfile download, remove the .txt extension)

#!/bin/bash
#
# Remove all of the core files from the current directory and replace them the
# files from the specified version. Files in the /site directory are left alone
# as well as files in the /files directory for sites that use the old file
# standard.

CURRENT_DIRECTORY=`pwd`
SOURCE_DIRECTORY=~/DrupalSource

echo "Drupal Upgrade Script"
if [ -z $1 ] ; then
  echo Please provide a version
  exit
fi

echo ""
echo "**"
echo "*"
echo "* Removing old files"
echo "*"
echo "**"

rm -v `find . -type f | grep -v '/.svn' | grep -v './sites' | grep -v './files' | grep -v '.kpf'`

if [ ! -d $SOURCE_DIRECTORY/drupal-$1 ] ; then
  echo ""
  echo "**"
  echo "*"
  echo "* Don't have Drupal source for version $1"
  echo "* Fetching Drupal source"
  echo "*"
  echo "**"
  cd $SOURCE_DIRECTORY
  wget http://ftp.drupal.org/files/projects/drupal-$1.tar.gz
  echo "**"
  echo "*"
  echo "* Expanding tarball"
  echo "*"
  echo "**"
  tar xvzf drupal-$1.tar.gz
  cd $CURRENT_DIRECTORY
fi

echo ""
echo "**"
echo "*"
echo "* Copying files"
echo "*"
echo "**"
cp -vR $SOURCE_DIRECTORY/drupal-$1/* .
cp -v $SOURCE_DIRECTORY/drupal-$1/.htaccess .

echo ""
echo "**"
echo "*"
echo "* Removing unrequired .txt files"
echo "*"
echo "**"
rm -v CHANGELOG.txt COPYRIGHT.txt INSTALL.mysql.txt INSTALL.pgsql.txt INSTALL.txt LICENSE.txt MAINTAINERS.txt UPGRADE.txt INSTALL.sqlite.txt README.txt

echo ""
echo "**"
echo "* Done"
echo "**"

Although some of the ideas in this script are my own, many others came from fellow Drupal colleagues and bloggers whose names I have forgotten. My thanks to them for sharing.

AttachmentSize
Plain text icon drupalupgrade.txt1.46 KB

Comments

You may update core with a patch. For more information see: http://fuerstnet.de/en/drupal-upgrade-easier. This preserves your changes in robots.txt and .htaccess.

Drush has built in support for updates, and saves you from reinventing the wheel. Drush has the same functionalities as your script, but in addition it can also update modules. It also verifies the MD5 sums and backs up the old files so you can roll back if needed.

I routinely use the Drush module update commands. They're awesome.

When it comes to upgrading Drupal core I think you missed this statement in my introduction: "this script has proven better suited to my workflow than its Drush equivalent". If it's working for your workflow I'm a little envious and hope you post a description of how you use it. I'm curious to see if I've misunderstood or missed something.