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
shebang!
Yeah, that’s one thing that I was wondering about. I don’t think I used anything that is shell specific, bash, or bourne or whatever. At least I tried not to. So, isn’t it better then not to specify a preferred shell and let the system do what it will?
Or do I specify #!/bin/sh and assume that any Linux sys admin will override it?
What is the more correct thing to do?
I could write it for bash and get more features, but is that then going to limit the servers? Does every server have bash?
Programming is like putting a puzzle together, you know the final picture that you want, But different companies cut the pieces in different shapes. With each new language you learn, you just have to recognise how the shapes fit together to make the same picture. But choosing which shebang to use based upon its adoption rate in the *nix world doesn’t fit into that analogy.
Any suggestions?
I pointed out the shebang because my preferred shell, fish, doesn’t do $RANDOM whereas sh does. As to what is correct, I’ve little experience of writing shell scripts for wide audiences, so I’d Google for “writing portable unix scripts” and work from there.
I’d use #!/bin/sh unless I knew for sure I could use something else.
OK, here we go. As mentioned via email, I’m not THAT much of a shell-buff, but a few thoughts:
… or – if possible – let your script finish the update itself.
Can’t think of anything more right now.
Your WP theme/stylesheet has “eaten” my unordered list in the comment above. Post looks a bit garbled due to this…
Odd… You’re right it does. I’ve never noticed that. I think it might actually be KSES but it should have no problem with UL and LI . I wonder if OL makes a different.
Hey there,
Thanks for your work on this script. I know this article is dusty, but here’s a few suggestions. First, explicitely define the shell you’re calling, e.g. BASH:
#!/bin/bash
…That will solve problems for people who use unusual shells like FISH or whatever. Next, please make it UTF-8! That will solve a lot of syntax problems across international borders. Finally, some WordPress updates require a DB update too, not just files. This can accomplished by adding a parallel set of site variables, for example:
BlogURLs[1]=”http://somesite.whatever”
BlogURLs[2]=”https://anothersite.someplace/blog”
“”
…then add another loop to touch the update page(s) with wget (or curl, telnet, etc.):
for CurURLs in “${BlogURLs[@]}”
do
echo “Updating database for: $CurURLs”
# Touch the upgrade script to trigger a DB update if necessary
wget -q -O – $CurURLs/wp-admin/upgrade.php?step=1 > /dev/null
echo “$CurURLs database update complete”
done
All the best,
-Chris
Good Suggestions Chris!
In fact the are in place in the subsequent version of the script here:
http://www.thecodecave.com/downloads/EasyWPUpdate.txt
The latest version of this script will always be reachable from:
http://www.thecodecave.com/EasyWPUpdate
(That said I haven’t updated it in a year and a half and really should now that I’ve been living in unix for all that time…)
I see… not only did you implement basically the same things I suggested, you have the nerve to implement it a year in advance of my suggestions… very sneaky. And after I completely re-wrote your old one for my own use, too! 😛 Thanks for being a good sport, Brian.
-Chris