Bash script for updating Drupal Core

Bash script for updating Drupal Core

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!

  1. #!/bin/bash
  2. # WARNING WARNING WARNING
  3. # Before updating Drupal, BACK UP YOUR SITE. http://Drupal.org/node/129202 explains how. Also, set site into maintenance mode.
  4. # After running script, run update.php, then take out of maintenance mode (if everything is working OK).
  5. # END OF WARNING
  6.  
  7. # Check for proper number of command line args.
  8. EXPECTED_ARGS=1
  9. E_BADARGS=65
  10. if [ $# -ne $EXPECTED_ARGS ]
  11. then
  12.         echo “Usage: `basename $0` {arg}”
  13.         exit $E_BADARGS
  14. fi
  15.  
  16. #for the following steps, stop immediately if something doesn't work
  17. set -e
  18.  
  19. #this is a cd to your Drupal site's root directory, change as needed
  20. cd ~
  21.  
  22. #get the update
  23. wget $1
  24.  
  25. #remove path from the wget argument to get name of file
  26. NEW_VERSION=${1##*/}
  27. tar -zxvf $NEW_VERSION
  28.  
  29. #remove .tar.gz from NEW_VERSION to get untarred directory name
  30. USE_DIRECTORY=${NEW_VERSION%.tar.gz}
  31.  
  32. #run through the rest even if there are errors
  33. set +e
  34.  
  35. #just to be sure, check to see if we can see a file in the untarred directory—otherwise, don't delete the old site!
  36. #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
  37. if [ -f $USE_DIRECTORY/index.php” ]
  38. then
  39.         rm -f robots. txt.old htaccess.old
  40.         cp robots.txt robots. txt.old
  41.         cp .htaccess htaccess.old
  42.         rm -R -f includes misc modules profiles scripts themes
  43.         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
  44.         cp -R $USE_DIRECTORY/ .
  45.         diff .htaccess htaccess.old
  46.         diff robots.txt robots. txt.old
  47.         rm -R $USE_DIRECTORY
  48.         rm $NEW_VERSION
  49.         rm INSTALL*txt install.php
  50. else
  51.         echo $USE_DIRECTORY not found”
  52. fi

Tags: