How to automatically replace files when updating WordPress

2011-04-17 » shell, Wordpress

WordPress is quick and easy to install and update, but the quicker you can make these operations the better, right? If you have shell access to the server where your WordPress copy is installed, it is possible to perform all the operations in Step 1 of the Manual WordPress Update Procedure with the shell script below. It will save you a few minutes, which may seem too little, but is great if you maintain more than one copy of WordPress. That, however, is not the main reason to use a script like this. Its bigger advantage is reducing the possibility of human error by doing things by hand, at the prompt or with the mouse is the same.

The scripts takes two parameters: the first is the complete URL of the zipped WordPress files that you need in order to upgrade. The second is the directory, called WP_ROOT, where the WordPress installation you need to upgrade lives. You will note that in my script $WP_ROOT is only the name of that directory, not the complete path to it, which in the script is $HTML_ROOT/$WP_ROOT. I have do this because I have several, independent copies of WordPress running on the same server, all living in different subdirectories of my $HTML_ROOT, named with the name of each blog. Therefore, this choice of variables sounds more natural to me. When I want to update http://stop.zona-m.net, I will type `upgrade_wordpress.sh http://wordpress.org/wordpress-3.1.1.zip stop`. When I want to update http://strider.zona-m.net, instead, I will type “strider” instead of “stop”, and so on. Here is the script. Enjoy it, but remember to read the warnings at the end of this page first!


  #! /bin/bash
  # upgrade_wordpress.sh
  # This script performs automatically the operations described in the section
  # "Manual Update - Step 1" of http://codex.wordpress.org/Updating_WordPress
  # Copyright 2011 M. Fioretti http://stop.zona-m.net
  # Released under GPL V2 license
  #
  # $1 URL of latest WordPress version, zipped
  # $2 Root directory of the WordPress installation that must be upgraded

  WP_ROOT=$2
  HTML_ROOT=/var/www/html/wp
  TEMP_DIR=/tmp/temp_wp_update

  # Step 1.1 and 1.2: get latest WordPress, unpack it in temporary directory
  mkdir $TEMP_DIR
  cd    $TEMP_DIR
  wget  $1
  unzip -q wordpress*.zip

  # Step 1.3: Delete the old wp-includes and wp-admin directories
  rm -rf $HTML_ROOT/$WP_ROOT/wp-includes $HTML_ROOT/$WP_ROOT/wp-admin

  # Step 1.4: "upload" the new wp-includes and wp-admin directories
  cp -r -p $TEMP_DIR/wordpress/wp-includes $TEMP_DIR/wordpress/wp-admin $HTML_ROOT/$WP_ROOT

  # Step 1.5: "Upload" the individual files from the new wp-content folder
  # to your existing wp-content folder, overwriting existing files.

  cd $TEMP_DIR/wordpress/wp-content/
  find . -type f | sort  > $TEMP_DIR/new_wp_content_files_list
  tar cf $TEMP_DIR/wp-content-files-to-replace.tar -T $TEMP_DIR/new_wp_content_files_list
  cd  $HTML_ROOT/$WP_ROOT/wp-content
  tar xf $TEMP_DIR/wp-content-files-to-replace.tar

  # Step 6: "Upload" all new loose files from the root directory of the new
  # version to your existing wordpress root directory

  cd     $TEMP_DIR/wordpress
  tar cf $TEMP_DIR/loose_files_in_wp_rootdir.tar license.txt readme.html *php
  cd     $HTML_ROOT/$WP_ROOT
  tar xf $TEMP_DIR/loose_files_in_wp_rootdir.tar

  # Final step: remove temporary files, remember to check the configuration

  rm -rf $TEMP_DIR
  echo "you should take a look at the wp-config-sample.php file, to see if any new settings have been introduced that you might want to add to your own wp-config.php"



Warnings

If you reload the WordPress admin page just after running the script you’ll get the “let’s upgrade the database” button, and everything should be OK after that. I have tested this script myself to update both http://tips.zona-m.net/ and this website from WordPress 2.8.1 on a Centos VPS and as far as I can see it worked just fine.

As everything done with shell scripts, howeverm this thing is powerful enough to both save you time and to hurt your blog if there is some bug or if something changes in the official WordPress instructions before I notice it and update this page. Of course, the script is provided as is, without warranties, and you should (because you should do it anyway!!!) back everything up right before running it.

The script could have been even shorter. I deliberately wrote it in that way to have the same steps as the procedure described at WordPress.org, and make it easier to update if/when something changes in WordPress. If you find errors or ways to improve it, please write them as a comment and I will update the page as soon as possible. Thanks.