#!/bin/bash #------------------------------------------------------------------------------# # Switch to newest version of Wordpress using SVN # # # # Supply the version from the prompt. The array contains the paths where # # the Wordpress installs are located. # # # # Must be done using root or sudo, so that the script has permissions to # # write over files. # # # # @Author: Ammon Shepherd # # @Date: 04.29.08 # VERSION="1.2.3" #------------------------------------------------------------------------------# #ToDo: #Change log # 09.04.07 - Fixed some minor text by adding ellipses at the end of text. Added a # constant for where to save the svn reports. # 01.03.08 - Figured out the HELPTEXT variable stuff and updated the text. # - Moved the code that asks for the file location and creates the array # to be within the if...else... logic so --help works, and if no version # is given, the help text is displayed. # 02.26.08 - Added ability to have comments in the FILENAME file. Commented # lines begin with a semi-colon ";". Edited the HELPTEXT. # 03.31.08 - Changed the way permissions are done. Only files/folders starting # with "wp-" should be altered. Added a "/" in the "Don't forget to # run..." line. Changed the prompt asking for the file containing # svn installs. Allows for a default location hard coded into the # program with the FILENAME variable. # 04.29.08 - Changed the line for option 'd' in the first prompt to use the # FILENAME variable. #------------------------------------------------------------------------------# # Constants FILENAME="~/wp-versions" SVNREPORT="svnreport-`date +%F`" REPORTPATH="~/svnreports" # http://codex.wordpress.org/Installing/Updating_WordPress_with_Subversion SVNURL="http://svn.automattic.com/wordpress/tags" HELPTEXT='wpupdate '$VERSION' by Ammon Shepherd Usage: wpupdate [wordpress version] To use wpupdate, call it from the command line as you would any other program. The only flag this command takes is the version of wordpress to upgrade to. You will be prompted for other needed information. Ex. "wpupdate 2.2.2" This would upgrade all of the installs to version 2.2.2. wupudate first makes a backup copy of the MySQL database and the wp-contents/ directory before upgrading. The program also runs as sudo. The program will prompt you for the following information: - Full path to a text file containing a list of wordpress installs, or you can type c to upgrade the current directory. - MySQL user and pass with access to all of the databases A report of the svn update is output to '$REPORTPATH' by default. This can be changed by altering the REPORTPATH variable.' if [ -z $1 ]; then echo "$HELPTEXT" echo exit 0; elif [ $1 == "--help" ]; then echo "$HELPTEXT" echo exit 0 elif [ -n $1 ]; then # Get the array containing the full paths to the Wordpress installs to update from a file. # tip from http://www.linuxquestions.org/questions/showthread.php?p=2867155#post2867155 exec 6<&0 echo -n "Enter path to WordPress install: 1: Enter the FULL path to file containing list of wordpress installs. 2: Type 'c' to upgrade the current directory. 3: Type 'd' to use the default file '$FILENAME'. [Enter choice here: ]" read list_input if [ $list_input == "c" ]; then pwd > tempfile exec < tempfile elif [ $list_input == "d" ]; then exec < $FILENAME elif [ -f $list_input ]; then exec < $list_input else echo "Path is not an svn WordPress install." exit 0 fi while read line do # Check if line is commented, if not add to the array comment=";" case $line in *"$comment"*) ;; *) (( CNT++ )) filearray[$CNT]=$line ;; esac done exec 0<&6 6<&- # Get the MySQL user/pass but don't show it on the CLI echo -n "Enter the MySQL user to use for all installs: " read -s sqluser echo echo -n "Enter the MySQL pass: " read -s sqlpass echo for path in ${filearray[@]} do if [ -d $path ]; then cd $path # Check to see if it's an .svn install if [ -d .svn ]; then echo "---------------------------------------------" echo "Updating $path" #Get the base folder name of the install - the name of the website. sitename=`pwd | sed -r -e "s/\//-/g"` # Get the owner of the .svn folder svnowner=`ls -lad .svn | cut -f3 -d" "` # Get the database name from the wp-config.php file in order to make a backup copy db_name=`grep "DB_NAME" wp-config.php | sed -r -e "s/^[^']+'.*', '([^']+)'.*/\1/"` echo "Creating backup of MySQL database ..." sudo su - $svnowner -c "mysqldump -u $sqluser -p$sqlpass --opt $db_name > $path/.$db_name.sql.backup" echo "Creating backup of wp-content ..." if [ -d .wp-content-backup ]; then sudo rm -rf .wp-content-backup fi sudo cp -rp $path/wp-content/ $path/.wp-content-backup # Open a file descriptor to put all svn output to a file. echo "Running svn on $path ..." echo "Saving output to $REPORTPATH/$SVNREPORT$sitename ..." echo exec 7>&1 exec > $REPORTPATH/$SVNREPORT$sitename echo "Running svn sv on $path ..." sudo svn sw $SVNURL/$1/ $path/ exec 1>&7 7>&- # Fix the permissions echo "Fixing permissions ..." sudo find $path -iname "wp-*" -exec chown $svnowner '{}' \; sudo find $path -type d -iname "wp-*" -exec chmod g+w+s '{}' \; sudo find $path -type f -iname "wp-*" -exec chmod 664 '{}' \; echo "Don't forget to run "$path"wp-admin/upgrade.php" echo echo # remove the tempfile when updating the current directory if [ -f tempfile ]; then rm -f tempfile #echo "tempfile" fi sleep 1 else echo "----------------------------------------------" echo "$path is not an svn install of wordpress" echo sleep 1 fi else echo "------------------------------------------------" echo "$path is not a directory. Skipping this install." echo sleep 1 fi done else echo "$HELPTEXT" echo exit 0 fi exit 0