WordPress Update Script

I like all the practice I’m getting with command-line scripts. I had to update WordPress today, which meant…

  1. Downloading the latest version
  2. Extracting the latest version
  3. Backing up older version
  4. Uploading newer version
  5. /ol>

    So there’s a few ways this could be done through a bash script – wget the latest URL, and then un-zipping or un-tar’ing that into the WP directory. This is a little more involving, since the download URL will change based on the version.

    This is similar to the upgrade script I have for phpVMS, and also the upgrade script I’m offering to users to work with for upgrading. So it was good timing that it coincided with the WordPress update. Two birds with one stone.

    So looking through the WordPress site for a viable upgrade path, I saw they use SVN for their version control – both dev and stable versions. Woohoo! Subversion to the rescue. Since this URL won’t change anytime soon, it’s a reliable source to use for upgrades. They house all of the “final” versions in the /tags directory, one directory per version, which made it even easier. They have a guide on Updating with Subversion, which was helpful on the procedure for the files which need to stay, so I came up with a small script:

    [bash]
    #!/bin/bash
    #
    # Nabeel Shahzad
    # http://www.nsslive.net
    #
    # This follows the general flow on the following page:
    # http://codex.wordpress.org/Installing/Updating_WordPress_with_Subversion

    #
    # Variables
    #

    # Where wordpress lives on our server
    WP_DIR="/path/to/wordpress"
    # WordPress stores latest versions under tags directory
    SVN_ROOT="http://svn.automattic.com/wordpress/tags/"

    # Go!
    echo "Retreiving latest version"

    LASTVERSION=$(svn list $SVN_ROOT | tail -1)
    echo "Found $LASTVERSION"

    # Clean out our temp directory
    rm -drf wp-temp

    # Check it out
    svn checkout $SVN_ROOT/$LASTVERSION wp-temp
    echo "Copying files"

    # Copy our files over
    cp -p $WP_DIR/wp-config.php $WP_DIR/.htaccess wp-temp
    echo "Copying everything into $WP_DIR"

    # Now copy everything backwards into where our live WordPress lives
    cp -rpf wp-temp/* $WP_DIR
    echo "Removing temporary directory"

    # Clean up the temp directory and delete it
    rm -drf wp-temp
    echo "Done!"
    [/bash]

    What I do is list the versions (svn list) in the /tags directory. The last line is the latest version available (ie, 2.5.0/), so I extract this (using tail –1), and then checkout the latest version into a temporary directory. Then according to the WordPress guide, I copy over some of the essential files (config file, htaccess) into that temporary directory, and then copy all those files into where WordPress lives.

    The only problem is… any files which you have modified that are part of the WordPress package will be overwritten and not copied over. You can add those files into the “Copy our files over” section, but it’s meant to be “non-destructive”, so it will only touch the WordPress files. I have it in my public_html root, where there are tons of other files, I don’t have to worry about those getting messed up or overwritten as part of the upgrade.

    All that’s left after this is running the updater, which just involves logging into the admin panel and running their upgrade script. 

    Cheers!

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • HackerNews
  • Netvibes
  • Reddit
  • StumbleUpon
  • Twitter
  • Yahoo! Buzz

Leave a Reply