Keeping web software up to date is a pain, but failure to do it can result in significant ramifications. Some bits of software are easier to keep up to date than others, but one thing I never like doing is using web-based upgraders. They may be convenient, but for a start they require that you give the web server far more file permissions than any sane person would want to during the upgrade process, plus the fact that any kind of ‘black box’ upgrade makes me nervous.
WordPress is fairly easy one to update, but even so requires some manual fiddling if like me you shy away from fully web-based upgraders. So, I wrote a simple script to automate it, including backing up previous files and the database in case anything goes horribly wrong. I’ve tested this on 2 sites so far (in 3 environments, my local test server and 2 separate live sites) and it upgraded WordPress installs running both 2.5.x and 2.7.x to the latest version without any problems. Certainly saved me time once I’d written it and removed the element of random human error (replacing it with predictable automated error 😉 ). I figured someone else might find it useful.
Disclaimers: this script is presented AS-IS and I take no responsibility for any effects of using it, use entirely at your own risk. In particular, it only backs up the core WordPress tables and assumes that you used the default ‘wp_’ table prefix, so if you have plugins which need extra tables, or used a different prefix, you will need to alter the script if you want a valid database backup. It’s also only applicable to Linux servers, although that’s the most common setup for WordPress anyway.
#!/bin/bash # WordPress upgrader, run from the root of your WordPress install like this: # /path/to/wpupgrade.sh /path/to/wordpress-x-x-x.zip # (tar.bz2 / tar.gz archives also supported) if [[ "$1" == "" ]] then echo Required: WordPress archive parameter exit 1 fi if [ ! -e $1 ] then echo Archive $1 does not exist exit 1 fi if [ ! -e wp-config.php ] then echo This script must be run from the WordPress root directory exit 1 fi # Back up old files echo Backing up DB... rm wordpress_db_backup.sql.bz2 read -pDatabase: db read -pusername: user mysqldump -u$user -p $db wp_comments wp_links wp_options wp_postmeta wp_posts wp_term_relationships wp_term_taxonomy wp_terms wp_usermeta wp_users > wordpress_db_backup.sql || exit 1 bzip2 wordpress_db_backup.sql echo Backing up files... rm wordpress_file_backup.tar.bz2 tar -cjf wordpress_file_backup.tar.bz2 wp-includes wp-admin wp-content wp-*.php index.php xmlrpc.php || exit 1 rm -rf tmpwordpress mkdir tmpwordpress case $1 in *.zip) unzip $1 -d tmpwordpress/ ;; *.tar.bz2) tar -xvjf $1 -C tmpwordpress/ ;; *.tar.gz) tar -xvzf $1 -C tmpwordpress/ esac rm -rf wp-includes cp -R tmpwordpress/wordpress/wp-includes ./ rm -rf wp-admin cp -R tmpwordpress/wordpress/wp-admin ./ cp -f tmpwordpress/wordpress/*.php ./ cp -f tmpwordpress/wordpress/*.html ./ cp -f tmpwordpress/wordpress/*.txt ./ rm -rf tmpwordpress echo Files updated, now go into WordPress admin to finish the upgrade
As an aside, WordPress 2.7.1 has a bug in it that can cause saving posts to fail with a message about htmlspecialcharacters_decode sometimes (such as when using the syntax highlighter in the above code). If you hit it too, the fix is in this bug report.