How to Automate (just about) Anything - Case Study 1: WordPress Subversion Change

One of the biggest things I regret in my technical career is not becoming more of a *nix ninja earlier.

Macs were always way too expensive, and not nearly as cool (in my circle) anyway, so getting one was never really an option. (and, it only would’ve helped once macs came with OS X)

So I was stuck with Windows on the desktop until about a year ago when I invested the requisite $600 for a Mac Mini.

Sure, I worked in linux on the server through SSH, but somehow, it’s not quite the same. You don’t nearly get the opportunity to play with various *nix tools and scripting on a day-to-day basis over an SSH connection as you do when you have a *nix-based OS running on your desktop.

Tip: if you would miss XP too much for some things, i.e. gaming, you can still run both XP and OS X through Synergy if you have two flatscreens.

The Problem

Over at Niner Niner, we’re changing Subversion hosts, which means a change in URLs, authentication, and svn uuids. If a few less of these variables were changing, we could simply use the svn switch command.

The reason we care so much is that we run a custom WordPress (slighty hacked core + a bunch of plugins we use) checked out of one of our own Subversion repositories.

At 27 blogs, that’s a lot of manual labor if we had to switch out all these Subversion checkouts by hand and make sure any static data (file uploads, etc) do not get lost in the process.

The Solution

Through the power of automation & shell scripting, the lines of shell commands to run this operation was reduced from 11 down to 1.

Note: some of the commands are chained together onto the same line using the double-ampersand (&&) operator in *nix, which will run one after another.

The single line of commands used to perform the switch (run once on each blog):

wget http://ninerniner.com/switch.sh && chmod +x switch.sh && ./switch.sh

Below - the contents of switch.sh. This was uploaded to ninerniner.com in the root html directory:

#!/bin/sh

echo "Copying public_html to a backup folder - public_html.back"
mkdir public_html.back && cp -R public_html/* public_html.back/

echo "Nuking: public_html/*"
rm -rf ~/public_html/*
rm -rf ~/public_html/.svn/

echo "Checking out the new repo"
cd ~/public_html

svn co --username svn_username --password svn_pass http://niner.unfuddle.com/svn/niner/trunk/backroom/base_install .

echo "Copying over wp-config.php from the backup dir"
cp ~/public_html.back/wp-config.php ~/public_html/

echo "Copying over wp-content/uploads/* from the backup dir"
cp -R ~/public_html.back/wp-content/uploads/* ~/public_html/wp-content/uploads/

echo "Copying over wp-content/af-extended-live-archive/* from the backup dir"

mkdir ~/public_html/wp-content/af-extended-live-archive/
cp -R ~/public_html.back/wp-content/af-extended-live-archive/* ~/public_html/wp-content/af-extended-live-archive/

echo "Updating Permissions"
./svn_update.sh

This probably won’t be useful to that many people in and of itself, but hopefully it will spark some ideas.

Do you ever encounter tasks that seem rote, that upon further inspection could be automated?