Bash script for updating Drupal Core
Submitted by Rory Jaffe on
Updating Drupal core always makes me a bit nervous, as it involves ripping out the old version and inserting a new. Not having access to Drush, I decided to make a shell script that would automate the steps, eliminating the usual risk of human error. The following is the script. It’s heavily commented, so I’ll keep extra remarks to a minimum.
The script takes one argument, the full URL of the new Drupal version (e. g., http://ftp. Drupal.org/files/projects/Drupal-7.19.tar.gz). Line 20 in the script is the only one that needs editing to match your installation.
Be sure to read the warnings in the first few lines. You need to put your site into maintenance mode prior to running the script, run update.php after, then test and take out of maintenance mode. Also, remember to back up your site!
-
#!/bin/bash
-
# WARNING WARNING WARNING
-
# Before updating Drupal, BACK UP YOUR SITE. http://Drupal.org/node/129202 explains how. Also, set site into maintenance mode.
-
# After running script, run update.php, then take out of maintenance mode (if everything is working OK).
-
# END OF WARNING
-
-
# Check for proper number of command line args.
-
EXPECTED_ARGS=1
-
E_BADARGS=65
-
if [ $# -ne $EXPECTED_ARGS ]
-
then
-
echo “Usage: `basename $0` {arg}”
-
exit $E_BADARGS
-
fi
-
-
#for the following steps, stop immediately if something doesn't work
-
set -e
-
-
#this is a cd to your Drupal site's root directory, change as needed
-
cd ~
-
-
#get the update
-
wget $1
-
-
#remove path from the wget argument to get name of file
-
NEW_VERSION=${1##*/}
-
tar -zxvf $NEW_VERSION
-
-
#remove .tar.gz from NEW_VERSION to get untarred directory name
-
USE_DIRECTORY=${NEW_VERSION%.tar.gz}
-
-
#run through the rest even if there are errors
-
set +e
-
-
#just to be sure, check to see if we can see a file in the untarred directory—otherwise, don't delete the old site!
-
#if we can see the file, then delete old files (as used in Drupal 7), copy new distribution into site, compare old htaccess and robots to new, remove temporary files
-
if [ -f ”$USE_DIRECTORY/index.php” ]
-
then
-
rm -f robots. txt.old htaccess.old
-
cp robots.txt robots. txt.old
-
cp .htaccess htaccess.old
-
rm -R -f includes misc modules profiles scripts themes
-
rm -f .gitignore .htaccess authorize.php CHANGELOG.txt COPYRIGHT.txt cron.php index.php INSTALL.txt install.php INSTALL.mysql.txt INSTALL.pgsql.txt INSTALL.sqlite.txt LICENSE.txt MAINTAINERS.txt README.txt robots.txt update.php UPGRADE.txt web.config xmlrpc.php
-
cp -R $USE_DIRECTORY/ .
-
diff .htaccess htaccess.old
-
diff robots.txt robots. txt.old
-
rm -R $USE_DIRECTORY
-
rm $NEW_VERSION
-
rm INSTALL*txt install.php
-
else
-
echo ”$USE_DIRECTORY not found”
-
fi