WordPress Auto-Update Script For A Linux Server
I wrote a little update script for my server to auto update my wordpress installation and figured it might help some other people as well. To use it you simply supply as arguments to the script three things:
1. the directory where your wordpress install lives
2. the name of the backups you’d like to create for that directory
3. the version of the wordpress install you’d like to upgrade to
For example, on my server I use this command to update my wp version:
sudo ./updateWordpress.sh /path/to/wordpress/installation efnx 2.7.1
[enter password]
[watch output]
done!
Here is the code to my script:
#!/bin/bash
DIR2UPDATE=$1
NAME=$2
VERSION=$3
echo "Beginning update of $DIR2UPDATE to version $VERSION..."
if [ -d wordpress_svn ]
then
day=`date | cut -d" " -f3`
tme=`date | cut -d" " -f4`
hour=`echo ${tme} | cut -d":" -f1`
fileday=`ls -lh | grep wordpress_svn | cut -c 37-39`
filehour=`ls -lh | grep wordpress_svn | cut -c 40-41`
if [ ${fileday} != ${day} ]; then
echo "SVN directory not up to date, [ file's date ${fileday} != today ${day} ] deleting and updating"
rm -rf wordpress_svn/*
cd wordpress_svn
svn co http://svn.automattic.com/wordpress/tags/${VERSION} .
else
if [ "$filehour" != "$hour" ]; then
echo "SVN directory not up to date, [ file's hour ${filehour} != now ${hour} ] deleting and updating"
rm -rf wordpress_svn/*
cd wordpress_svn
svn co http://svn.automattic.com/wordpress/tags/${VERSION} .
else
echo "SVN directory is up to date, skipping update"
cd wordpress_svn
fi
fi
else
echo "Creating new svn directory and checking out version $VERSION...";
mkdir wordpress_svn;
cd wordpress_svn;
svn co http://svn.automattic.com/wordpress/tags/${VERSION} .;
fi
cd ..
if [ -d wordpress_int ]
then
echo "Removing old intermediate container...";
rm -rf wordpress_int;
fi
echo "Creating new intermediate container..."
mkdir wordpress_int
echo "Moving version $VERSION files into intermediate container..."
cp -rpf wordpress_svn/* wordpress_int
echo "Moving config and custom files from $NAME into intermediate container..."
cp -p ${DIR2UPDATE}/wp-config.php wordpress_int
cp -rpf ${DIR2UPDATE}/wp-content/* wordpress_int/wp-content/
cp -p ${DIR2UPDATE}/.htaccess wordpress_int
echo 'Updating svn for wp-content'
svn update wordpress_int
echo "Backing up $DIR2UPDATE..."
mkdir ${NAME}_backup
#mv -f ${DIR2UPDATE}/* ${NAME}_backup # if you'd like to mv instead of cp
cp -rpf ${DIR2UPDATE}/* ${NAME}_backup
tar -czvf ${NAME}_backup.tar.gz ${NAME}_backup
rm -rf ${NAME}_backup
echo 'Removing svn data from intermediate container...'
rm -rf `find wordpress_int/ -type d -name .svn`
echo "Moving intermediate container contents to $DIR2UPDATE..."
cp -rpf wordpress_int/* ${DIR2UPDATE}
DIR2UPDATE=$1
NAME=$2
VERSION=$3
echo "Beginning update of $DIR2UPDATE to version $VERSION..."
if [ -d wordpress_svn ]
then
day=`date | cut -d" " -f3`
tme=`date | cut -d" " -f4`
hour=`echo ${tme} | cut -d":" -f1`
fileday=`ls -lh | grep wordpress_svn | cut -c 37-39`
filehour=`ls -lh | grep wordpress_svn | cut -c 40-41`
if [ ${fileday} != ${day} ]; then
echo "SVN directory not up to date, [ file's date ${fileday} != today ${day} ] deleting and updating"
rm -rf wordpress_svn/*
cd wordpress_svn
svn co http://svn.automattic.com/wordpress/tags/${VERSION} .
else
if [ "$filehour" != "$hour" ]; then
echo "SVN directory not up to date, [ file's hour ${filehour} != now ${hour} ] deleting and updating"
rm -rf wordpress_svn/*
cd wordpress_svn
svn co http://svn.automattic.com/wordpress/tags/${VERSION} .
else
echo "SVN directory is up to date, skipping update"
cd wordpress_svn
fi
fi
else
echo "Creating new svn directory and checking out version $VERSION...";
mkdir wordpress_svn;
cd wordpress_svn;
svn co http://svn.automattic.com/wordpress/tags/${VERSION} .;
fi
cd ..
if [ -d wordpress_int ]
then
echo "Removing old intermediate container...";
rm -rf wordpress_int;
fi
echo "Creating new intermediate container..."
mkdir wordpress_int
echo "Moving version $VERSION files into intermediate container..."
cp -rpf wordpress_svn/* wordpress_int
echo "Moving config and custom files from $NAME into intermediate container..."
cp -p ${DIR2UPDATE}/wp-config.php wordpress_int
cp -rpf ${DIR2UPDATE}/wp-content/* wordpress_int/wp-content/
cp -p ${DIR2UPDATE}/.htaccess wordpress_int
echo 'Updating svn for wp-content'
svn update wordpress_int
echo "Backing up $DIR2UPDATE..."
mkdir ${NAME}_backup
#mv -f ${DIR2UPDATE}/* ${NAME}_backup # if you'd like to mv instead of cp
cp -rpf ${DIR2UPDATE}/* ${NAME}_backup
tar -czvf ${NAME}_backup.tar.gz ${NAME}_backup
rm -rf ${NAME}_backup
echo 'Removing svn data from intermediate container...'
rm -rf `find wordpress_int/ -type d -name .svn`
echo "Moving intermediate container contents to $DIR2UPDATE..."
cp -rpf wordpress_int/* ${DIR2UPDATE}
Or you can just download the script here [rightclick + 'save as']->
updateWordpress.sh
February 12th, 2010 at 9:56 pm
Hi, this looks very useful! Couldn’t you use svn export instead of removing the .svn separately?
February 13th, 2010 at 1:13 pm
Yes! There are tons of tricks (and best practices) that I’ve learned since I wrote this. This script was probably my first interaction with svn altogether.
February 20th, 2010 at 3:45 pm
[...] Upgrade wordpress quickly in 3 easy steps from UNIX shell prompt wordpress update script WordPress Auto-Update Script For A Linux Server [...]
September 15th, 2010 at 11:11 am
[...] [...]
October 20th, 2010 at 10:17 pm
WordPress is definitely the best blogging platform, no one can beat wordpress-”~
August 9th, 2011 at 11:14 pm
If you don’t want to use the latest version from subversion, but the latest stable version, this script may interest you:
https://gist.github.com/1135581
It’s designed to run without interaction on Linux as a cron job. It will also backup your database and email the root user if WordPress is successfully updated.
August 31st, 2011 at 8:20 am
Awesome Richard, thanks!
September 9th, 2011 at 5:14 am
With thanks for piecing this together – it is a great post for those of us with our heads buried in the keyboard all day.
October 3rd, 2011 at 6:01 pm
mafri.ws…
[...]The efnx code blog. » WordPress Auto-Update Script For A Linux Server[...]…