If you run multiple WordPress blogs on WebFaction it can be a chore to update. You know the song and dance: visit the admin panel, click upgrade, wait for it to install, and repeat for the next blog. I decided to write a shell script that helps out with this process. It assumes you use the default path WebFaction sets up for you. E.g /home/username/webapps/wordpress-site/. Place it in your home folder.
The script will:
- Download the latest WordPress
- Backup the database
- Backup the site
- Copy over new WordPress files
The script will not:
- Upgrade the database
- Upgrade your plugins
- Compensate for non standard installations
- Win the Lottery
As noted above, in some cases you may need to upgrade the database. That can be done by visiting http://yoursite.com/wp-admin/ and clicking the upgrade button (You don’t have to login).
The script is below or you can download it here.
#!/bin/bash
# Backup and update wordpress sites on webfaction
# Author: Glen Zangirolami
# URL: http://twitter.com/glenbot
# Get a list of sites under webapps
WEB_APPS=$HOME/webapps/*
# Set backup paths
DB_BACKUPS=$HOME/wordpress_backups/databases
SITE_BACKUPS=$HOME/wordpress_backups/sites
# Set new wordpress extraction path
WP=$HOME/wordpress_extaction
# Download wordpress and extract
wget http://wordpress.org/latest.tar.gz
mkdir -p $WP
tar -C $WP -xzf latest.tar.gz
rm latest.tar.gz
# Make the database backup path
if [ ! -d $DB_BACKUPS ]; then
mkdir -p $DB_BACKUPS
fi
# Make the site backup path
if [ ! -d $SITE_BACKUPS ]; then
mkdir -p $SITE_BACKUPS
fi
# Loop through the apps and:
# 1. Backup the database
# 2. Backup the site directory
# 3. Copy new wordpress files
for APP in $WEB_APPS
do
WP_CONFIG=$APP/wp-config.php
APP_NAME=`echo $APP | cut -d \/ -f5`
IS_WP=0
if [ -f $WP_CONFIG ]; then
IS_WP=1
fi
if [ $IS_WP -eq 1 ]; then
DB_NAME=`cat $WP_CONFIG | grep -P -o "define\(\'DB_NAME',\s*\'([^\']+)\'\);" | cut -d \' -f4`
DB_USER=`cat $WP_CONFIG | grep -P -o "define\(\'DB_USER',\s*\'([^\']+)\'\);" | cut -d \' -f4`
DB_PASSWORD=`cat $WP_CONFIG | grep -P -o "define\(\'DB_PASSWORD',\s*\'([^\']+)\'\);" | cut -d \' -f4`
DB_HOST=`cat $WP_CONFIG | grep -P -o "define\(\'DB_HOST',\s*\'([^\']+)\'\);" | cut -d \' -f4`
echo "Backing up database for $APP_NAME ..."
mysqldump -h $DB_HOST -u $DB_USER -p$DB_PASSWORD $DB_NAME > $DB_BACKUPS/$DB_NAME.sql
echo "Backing up site $APP_NAME ..."
tar -czf $SITE_BACKUPS/$APP_NAME.tar.gz $APP/*
echo "Copying over new wordpress to $APP_NAME ..."
cp -rf $WP/* $APP
fi
done
Happy coding!
