As everyone I am sure knows to well upgrading wordpress can be a bit of a pain, so based on a couple of other scripts I found around the net I created this script for automatically backing up and then upgrading a wordpress install.

What it basically does is if you give it the full path of a wordpress install it will automatically reads the config file and get the DB details, then perform a backup of the db, then a backup of the wordpress files and then finally an upgrade of the wordpress install.

To run just use `bash wpUpgrader /var/www/wordpress/`

Download the Script Here

#!/bin/bash
# Linux WordPress Upgrader Script
#
# Mark Davidson | [email protected] | www.pablumfication.co.uk
#

function wpExtract {
	echo $(grep -o "define('$1', '\([^']*\)');" $WP/wp-config.php | cut -f 2 -d ' ' | awk '{ print substr($0, 2, length($0) - 4) }')
}

TEMP=/tmp

if [ $# = "1" ]; then
	WP=$1
	# need to either force full path input or create canconical version
	# Validate that this directory is valid and contains wordpress files 
	BACKUP_DIR=$PWD/backups/wp/$(date +%s)
	
	echo "Creating Backup Directory"
	mkdir -p $BACKUP_DIR
	echo "Backup Directroy Created " $BACKUP_DIR
	cd $BACKUP_DIR

	echo "Backing up wordpress database"
	result=`mysqldump -u $(wpExtract DB_USER) -p$(wpExtract DB_PASSWORD) --database $(wpExtract DB_NAME) 2>&1 > blog.sql` 
	if [ -n "$result" ]; then 
		echo $result
	else
		echo "Taring DB Backup"
		result=`tar -cf db.tar.gz blog.sql 2>&1`
	
		if [ -n "$result" ]; then
			echo "DB backup could not tared be created exiting"
		else
			echo "DB backup tared"
			rm blog.sql

			echo "Creating Backup of WordPress Files"
			tar -Pzcf blog.tar.gz $WP

			echo "Checking Backup Integrity"
			result=`tar -dPf blog.tar.gz $WP`
			
			if [ -n "$result" ]; then
				echo "Integrity check failed"
				echo $result
			else
				echo "Downloading Latest WordPress"
				wget -O $TEMP/latest.tar.gz http://wordpress.org/latest.tar.gz
				tar -zxf $TEMP/latest.tar.gz

				result=`tar -ztf $TEMP/latest.tar.gz | grep wordpress/index.php | wc -l` # Really basic chek that file is intact proberly a better way to do this
				
				if [ "$result" = 1 ]; then
					echo "File OK"

					echo "Extracting WordPress"
					result=`tar -C $TEMP -xf $TEMP/latest.tar.gz` #TODO: Validate extraction
					echo $result

					cd $WP
					result=`cp -avr $TEMP/wordpress/* .`

					echo $result

					rm -rf $TEMP/wordpress $TEMP/latest.tar.gz

					echo "New files copied vist http://yourdomain.com/wp-admin/upgrade.php to complete the upgrade"
				else 
					echo "File Corrupt or missing"
				fi
			fi
		fi
	fi
else 
	echo "Incorrect Number of Arguments"
fi

I based this script off a few others I suggested checking them out

Upgrade wordpress quickly in 3 easy steps from UNIX shell prompt
wordpress update script
WordPress Auto-Update Script For A Linux Server
WordPress Upgrade Script

Share this post

Leave a Reply

CAPTCHA (required) Time limit is exhausted. Please reload CAPTCHA.