[wp-hackers] Automatic multi-host WordPress upgrade

Alex Günsche ag.ml2007 at zirona.com
Thu Jan 11 10:48:45 GMT 2007


Hi list,

we all know how agonizing it can be to update web applications,
especially if it's many of them and they want to be updated frequently.
However: sh*t happens, and it's better to have frequent updates than
none at all. Still, if you have many installations to care for, you
might want to wish a (semi-)automatic solution to upgrade them for you.

As for WordPress, I have created such a solution, which does a fine job
on my server and I wanted to share. If you have a vitrual or decicated
root(!) server, you can use this little bash script in order to upgrade
a number of WordPress installations automatically. This of course
happens on the command line (bash).

If there are people who do have many WP blogs and a shell, but lack the
root access, it should be easy to adapt the script for non-privileged
usage.

ATTENTION: Use at your own risk and make sure you have backups.

I hope it saves somebody a couple of hours. (Usage hints below.)

--------------SNIP-----------------------------------------------
#!/bin/bash

# Copyright (c) 2007 Alex Guensche
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

# where are working data stored
WORKDIR="/root/wpupgrade/"

# a file with the absolute path to the WordPress install and the URL
# to the website, both separated only by an equation mark, no other
# characters(!!!). example lines in that file:
# /var/www/example.org/htdocs/blog/=http://example.org/blog/
# /var/www/localhost/htdocs/=http://127.0.0.1/
WPDIRS_FILE="${WORKDIR}installations"

# derive the affected WP installations from that file
WPDIRS=$(cat ${WPDIRS_FILE})

# WordPress website
WORDPRESS="http://www.wordpress.org/"

# path to latest version
LATEST="latest.tar.gz"

# script to call in order to finish the upgrade
UPGRADESCRIPT="wp-admin/upgrade.php?step=1"


# get it on
cd ${WORKDIR}

# remove old versions
[ -e "wordpress" ] && rm -r wordpress
[ -e "latest.tar.gz" ] && rm latest.tar.gz

# get latest version, unpack and prepare
wget ${WORDPRESS}${LATEST}
tar -xzf $LATEST
rm -r wordpress/wp-content
rm wordpress/wp-config-sample.php
[ -d "languages" ] && cp -r languages wordpress/wp-includes

# upgrade each webhost
for i in ${WPDIRS}; do

        DIR=$(echo $i | cut -f 1 -d "=")
        WEBSITE=$(echo $i | cut -f 2 -d "=")
        USER=$(ls -l ${DIR}/wp-config.php | cut -f 3 -d ' ')
        GROUP=$(ls -l ${DIR}/wp-config.php | cut -f 4 -d ' ')

        echo "Upgrading WP install in ${DIR} for host ${WEBSITE}..."
        echo -n "Do you have backups and want to continue? [y/N] "
        read DOITBABY
        [ "${DOITBABY}" == "y" ] || exit

        cd $DIR;
        echo "Upgrading..." >> index.html
        mv wp-content wordpress_content
        mv wp-config.php wordpress_config.php
        rm -r wp-* xmlrpc.php index.php

        cp -r ${WORKDIR}/wordpress/* .
        mv wordpress_content wp-content
        mv wordpress_config.php wp-config.php

        wget "${WEBSITE}${UPGRADESCRIPT}" -O -
        chown -R ${USER}:${GROUP} ${DIR}
        rm index.html

        echo "Upgrade for ${DIR} finished."
done
--------------SNIP-----------------------------------------------

Usage
* Log in as root and cd to /root/
* create a directory named "wpupgrade" (w/o quotes)
* cd to wpupgrade
* save the above script as wpupgrade or something
* make executable with chmod +x
* recommended: create a folder "languages" (w/o quotes) with all 
  required language files in it
* create a file named "installations" (w/o quotes) with the absolute
  path to the WordPress install and the URL to the website, both
  separated only by an equation mark, no other characters(!!!). Example
  lines in that file:
/var/www/example.org/htdocs/blog/=http://example.org/blog/
/var/www/localhost/htdocs/=http://127.0.0.1/

When you now execute the script, all affected installations will be
upgraded. Enjoy!


Best regards
Alex


-- 
Alex Günsche, Zirona OpenSource-Consulting
work: http://www.zirona.com/ | leisure: http://www.roggenrohl.net
PubKey for this address: http://www.zirona.com/misc/ag.ml2007.asc



More information about the wp-hackers mailing list