Any bash buffs out there? WordPress update script 2 alpha

Are any of you all good and *nix scripts?

I’ve been working on the next version of the “35 second upgrade” script and I’d like some second eyes on it before I release it officially.. I would like your help in ensuring this method isn’t gonna crash any typical *nix based, non-core-code-customized blogs. I was wondering if some of you might review this script and tell me of any errors or problems you can foresee. I’ve got it working just fine updating my blogs. But, I’d like more of a confidence factor than what I can get just having it work for me and only me.

Current Improvements:
1. Can update any number of directories by just adjusting the array at the top
2. Can pull from other sources. You don’t HAVE to update to the current and can just use it to roll back your code, every night, to your customized WP version.
3. Now works for blogs stored in the “WordPress” directory.
4. Cleans up after itself
5. Error checking
6. Observes tmp directory locations.

Coming soon:
1. File backups
2. SQL backups

If you know anything about scripts, could you give it a review and tell me what you think?

This IS alpha stuff, so use it with that in mind…

Source code follows


# *************************************************************************
# UpdateWP ver 2.0 alpha 1 01/Aug/2006
# Written by Brian Layman
#
# A unix shell script to update multiple WordPress blogs to the current
# stable release.
#
# Usage: (A full instruction set is at https://thecodecave.com/article300)
# Browse out to www.TheCodeCave.com and get the latest
# Customize the "Configuration variables" found below
# Use chmod to grant yourself execute status on the script
# Run the program
#
# You can use this program in several ways.
# First, you can use it in the default mode to download the latest
# update and then install it to several directories.
# Second, you can use it to automate backing up files and tables when
# updating.
# Third, you can cron it and change the source of the update and use
# it to force the start of a known clean system every morning.
#
# Original Author - Brian Layman
#
# Created - 01/AUG/2006
# Last Modified - 21/DEC/2006
# Contributors: (Put your name & Initials at the top)
# Brian Layman - BL - http:#www.TheCodeCave.com
#
#
# History:
# 01/AUG/2006 - BL - Created
# 21/DEC/2006 - BL - Finalized Ver. 2 tweaks
#
# License - If this helps you - Great! Use it, modify it share it.
#
# Indemnity -
# Use this file at your own risk. I'm not going to deliberately hack
# your server, but others might. This is a shell script. Very bad
# things can happen. I am relatively new to *nix scripts. So
# I'M HAVING others review this script. But NONE of this guarantees
# things won't go wrong or that this script is unchanged, even if
# you've gotten it from TheCodeCave.com or another site you trust.
#
# THIS SCRIPT SHOULD BE USED AT YOUR OWN RISK. It CAN erase hours of
# hard work put into your site. Before using this script it is
# required that you review and understand every line and vouch for
# its safety. If you are not comfortable with this, don't run this
# script. I have one host that I can test this on. Only you can say
# that this script will not do irreperable harm to your site or your
# host if you use it.
#
# YOU are responsible for YOUR site. Learn how to protected it and
# understand what every line of code does before you call it. I
# am not liable for any damages, lost data, lost time, or interupted
# services because you've chosen to run this script on a system
# for which I cannot vouch. Use at your own risk.
#
# Donations - If this batch file really helps you out, feel free to
# make a donation of the cost of a cup of expresso via Paypal to
# Brian@TheCodeCave.com. A morning coffee or cheesy nachos and I
# your friend for life. And/or leave a comment on my site:
# http://www.thecodecave.com/did-that-help.
#
# *************************************************************************

# ##################################################################
# Configuration variables
# ##################################################################
# Common root is the part of your path that is shared by all of your
# WP blogs. It is probably your htdocs directory. Blank is fine if
# you want to specify the full path in the BlogDirs variable.
# Using ~ will not work.
CommonRootPrefix="/put/your/homepages/path/here/htdocs/"

# List all of your WordPress directories here
# Add more lines if you have more blogs.
# Remove one or two if you have less.
BlogDirs[1]='myflowers'
BlogDirs[2]='storesite/news'
BlogDirs[3]='example.com'

# These variable can be used to change where you get the clean copy of WordPress
# Override this variable if you wish to use this script to restore your files to
# a known version of WordPress. As part of a nightly routine, this can keep all
# of your sites running un-hacked codes.
SourceURL='http://wordpress.org/'
TarBallName='latest.tar.gz'

# MAKEFILEBACKUPS will back up the FILES from the folders above if it is set to 1.
# Only use this feature when the WP folders only have WP stuff in them.
# This will take time and space. If you have, for example, a downloads folder under
# one of the folders listed above, you will make a copy of it. You may run out of
# space and that can crash your site. That's why this is off by default.
# MAKEFILEBACKUPS=0 # REMOVED FROM THIS VERSION

# MAKESQLBACKUPS enables Database backups to be made of the WP specific tables
# for each blog. It reads the database username, password and table prefix
# It is disabled by default for several reasons
# 1. Copies of a DB on a webserver is a security risk
# 2. I suspect it will not work on all systems mysqldump must be present
# 3. Your wp-config file might not allow me to read it
# 4. Your wp-config file might customized in a way I cannot predict
# 5. I'm not done testing it
# MAKESQLBACKUPS=0 # REMOVED FROM THIS VERSION

# ##################################################################
# Constants - Do not change these
# ##################################################################
#Error Codes
E_SUCCESS=0 # No Error Code. It worked.
E_XCD=66 # Can't change directory?
E_XMD=67 # Can't make directory?
TMPPREFIX='TCCWPUPDATE-'

# ##################################################################
# Prepare the stage by getting the files in order.
# ##################################################################

# Make Temporary directory for downloading the WordPress file
tmp=${TMPDIR-/tmp}
tmp=$tmp/$TMPPREFIX$RANDOM$RANDOM$RANDOM.$$
(umask 077 && mkdir $tmp) || {
echo "Could not create temporary directory! Exiting." 1>&2
exit $E_XMD
}

# Show this to allow potential manual cleanup...
echo "A temp dir was created at: $tmp"

#Change to the temporary directory
cd $tmp

# Doublecheck if in right directory, before messing with downloading files.
if [ `pwd` != "$tmp" ]
then
echo "Can't change to new temp dir. Aborting."
exit $E_XCD
fi

# Download the tarball into the temp directory, extract it and deleted it.
wget $SourceURL$TarBallName
tar -zxf latest.tar.gz
rm $TarBallName

# ##################################################################
# Perform the full file backup before touching any files.
# ##################################################################

# ##################################################################
# Perform the full table backups before touching any files.
# ##################################################################

# ##################################################################
# Iterate all of the directories and overwrite their contents.
# ##################################################################

# Loop through the BlogDirs array
for CurDir in "${BlogDirs[@]}"
do
echo "Now Updating: $CurDir"
# Go to each directory
cd $CommonRootPrefix$CurDir
# Doublecheck if in right place, before copying over hundress of files.
if [ `pwd` != "$CommonRootPrefix$CurDir" ]
then
echo "Aborting. Can't reach one of the blog directories: $CommonRootPrefix$CurDir"
rm $tmp -R # Don't leave the temp dir out there if we don't have to
exit $E_XCD
fi

# Copy all of of the files from the temp dir
cp -R -v --remove-destination $tmp/wordpress $CommonRootPrefix$CurDir
echo "$CurDir Update complete"
done

# ##################################################################
# Cleanup
# ##################################################################

# Once everything is done, we can remove the temp directory
rm $tmp -R

# ##################################################################
# Close
# ##################################################################
echo UPDATE COMPLETE
exit $E_SUCCESS

9 Comments

Add a Comment

Your email address will not be published. Required fields are marked *