Archive for the ‘Tools’ Category

Backing up WordPress with Git

Thursday, September 29th, 2011

You can never back up too often.  I’ve been bitten by WordPress bugs (both when upgrading the core or plugins, and I’ve had sites hacked at least once).  I haven’t found a good, automated backup mechanism for WordPress that keeps old history and allows an easy roll-back to an older (presumably working) version.  I’ve become a huge fan of Git in the last year, so I whipped up some scripts to do what I needed.

I’ll assume you’re starting with an existing WordPress install, and it lives on the filesystem at ~/www/blog, and that git is in your path.

  1. $ cd ~/www/blog
  2. create a new repo. $ git init .
  3. add everything. $ git add .
  4. make the inital commit. $ git commit -m 'initial commit'
  5. prevent access to the .git directory via http; create ~/www/blog/.git/.htaccess with the following contents:
            Order allow,deny
            Deny from all

I don’t recommend automating the backup of the WordPress directory; the files in there shouldn’t change automatically. Instead, I like to make sure there are no changes before upgrading the WordPress core, or a plugin, and then explicitly create a new commit with the changes immediately after an upgrade. I do these pretty granularly, so that I can roll back a single plugin.

  1. $ cd ~/www/blog

  2. ensure git status shows no changes

  3. do your thing in the WordPress admin UI

  4. commit the changes:
    $ git add -A && git commit -m 'automatically upgraded to WordPress 3.2.1'

I do automate database dumps, and I like the system I came up with. I create a new “orphan” branch in Git that contains only the database dump. This allows a single Git repository to have two completely separate trees, making it one logical entity containing everything you need to manage the WordPress instance. I keep these repository clones in a different directory, for security reasons.

Here’s how you set that up. First, create a script (I call it do_dump.sh) to dump the mysql database to a file; it will also commit the dump after a commit. Collect the database name, username and password from wp-config.php, then put them into your script. Fill in the blanks from the sample below;
    #!/bin/bash

    # provide location of git binary, if it's not in your path
    git=/path/to/git

    # db info from wp-config.php
    dbname="wordpress_db"
    dbuser="wp"
    dbpassword="rAnd0m^pa55w0rd"

    cd $(dirname $0)

    do_commit=0
    if [ "commit" = "$1" ]; then
        do_commit=1
    fi

    mysqldump
        --skip-dump-date
        --skip-extended-insert
        --single-transaction
        -h localhost
        -u "${dbuser}" "-p${dbpassword}"
        "${dbname}" > "${dbname}.sql"

    if [ $do_commit -eq 1 ]; then
        if [ ! -z "$($git status -s)" ]; then
            echo -e "** autocommit **nn$(date) @ $(hostname)" | $git commit --all --quiet -F -
        fi
    fi

  1. $ cd ~/db_repos/

  2. clone the existing wordpress repo:
    $ git clone ~/www/blog blog

  3. $ cd ~/db_repos/blog

  4. create the new orphan branch:
    $ git checkout --orphan wordpress_db

  5. remove the wordpress files from the index:
    $ git rm --cached -r .

  6. remove the wordpress files from the directory:
    $ git ls-files --other | tr 'n' '' | xargs -0 rm

  7. remove the empty directories:
    $ find . -depth -type d ! -path '*/.git/*' -empty -exec rmdir {} ;

  8. move the dump script into place:
    $ mv /path/do/do_dump.sh .

  9. run the script the first time:
    $ ./do_dump.sh

  10. create a new commit containing the dump file and script:
    $ git add -A && git commit -m 'initial commit on wordpress_db'

Now, automate the database backup. I do this via cron, every 6 hours:
    0 */6 * * * $HOME/db_repos/blog/do_dump.sh commit
When run from cron, the dump script will automatically commit the changes to the local repository. I actually have a slightly different version of the script that also pushes the changes to a remote repository (so my backups aren’t all on one server), but that’s a little more complex.

Hopefully someone finds this useful!

In-car camera mounts

Monday, April 23rd, 2007

I’ve purchased a fair bit of gear in the last few months for mounting both my video camera and DSLR in the car and I thought I’d take a few minutes and describe the setups I’m using.

First up is the Gripper suction-cup mount from Filmtools (which I bought back in January and didn’t remember posting about). It’s nothing more than a suction cup with a plunger that draws a vacuum, a Bogen 3D head, and some custom hardware to tie the two together. It’s simple and effective and well worth the $90 or so I paid for it. The rubber suction cup itself is quite rugged, and the vacuum pump helps to make a nice firm seal to the surface. The pump plunger has a red line on it, and when that line shows, you know that an appreciable amount of suction has been lost and it’s time to take action. Honestly, when the red line shows, there’s still quite a bit of grip, but when you’ve got more than a thousand dollars worth of sensitive equipment suspended above the ground you probably shouldn’t mess around too much. These pictures were both taken using the Gripper mount:

The straight-ahead shot with the Gripper was taken using this orientation:

Note that the camera’s mounted upside-down to accomplish this. I have kind of a love/hate relationship with that head. It’s more flexible than probably any other head I’ve seen and is an excellent fit for this application, but the three knobs and independent axes of adjustment are a royal pain in the ass, and I’m constantly pinching a finger in there somewhere.

The other mount is a CruiseCam in-car headrest mount. It’s a simple mount that clamps to the two supports that make up most adjustable headrests. The camera gets positioned between the two seats and will typically look straight out the front or rear of the car. I used it for the first time this weekend at an autocross and I think I like the results (although I haven’t even seen the video off the camera yet). The mounting options aren’t as flexible as the Gripper, but it gives you a couple that aren’t possible (or aren’t as “good”), those being the straight-ahead or -behind shots. You can also use it on the rear headrests.

Here’s a rear view of my video camera (and associated wires…) mounted to the CruiseCam:

I’ve only taken a few stills with this mount and it was on a very bumpy road; the results of longer exposures (1/10s) weren’t very good due to the massive jarring of the car. The test videos I’ve taken look pretty good so far, however. This is one quick shot I took with my wide-angle lens and the CruiseCam:

For video, I’m using a Sony DCR–HC96 MiniDV camera, a Sony VMCK100 microphone adapter (the –HC96 doesn’t have a regular mic input), an Audio–Technica ATR–35S lavaliere mic, and the Sony DC adapter for power. The stock battery’s good for about 20 minutes which is pretty damn pathetic in my opinion, especially since the large capacity battery’s over $100. Acquiring this rig is another post (hell, book) in its own right, and I’ll let the results speak for themselves (soon, hopefully!).

For both the CruiseCam and Gripper mounts I’m using a Bogen 3299 Quick Release adapter. It attaches to any support that would normally screw into the bottom of a camera but provides a quick-release feature that’s really handy. Once the camera’s aimed, you can remove it from the mount without having to worry about aiming it again later. It also makes setup and tear-down much easier. I’ve got two or three of these, now, and need at least one more.

I’ve got a small set on Flickr showing the Gripper and CruiseCam mounts in my car to give an idea of how I use them and what’s possible.

Suction cup camera mount

Monday, January 22nd, 2007

Playing around with a new suction cup mount that arrived last week. This one’s going to be a lot of fun…

Updated Flickr Bookmarklets

Monday, January 8th, 2007

I’ve updated my Flickr Bookmarklets that I originally introduced in January ‘06. They’ve now got their own page, and I’ll make changes and updates there.

Cool Tools: XPE

Thursday, September 16th, 2004
I recently discovered the joy of XPath, thanks to a talk at one of the IndyJUG meetings a couple of months ago. Once I got the most basic of grasps on XPath queries, I was able to start churning out some pretty slick scraped RSS feeds for things like Pollstar artist and city searches, eBay searches, Iconfactory and Konfabulator recent updates, and a bunch of others. Actually, that little project’s a good showcase of a couple of my favorite tools, Henplus and Tagsoup. Anyway, one of the biggest difficulties I ran into was visualizing the results of XPath queries without having to dump the results with an XSL stylesheet, which was a less-than-elegant solution. After a bit of Googling, I came up with a tool called XPE. It's the only "free" (for any definition of free) tool I’ve found so far that allows me to evalue XPath statements and see their affect on a document. XPE isn't terribly fancy in the interface department, and can be a little sluggish at times, but it is extremely effective. The default view on startup is a graphical tree representation of the nodes in the loaded XML document. When you evaluate an XPath, XPE highlights the matched elements (but not text nodes) and expands the short-format to it's fully-qualified (and durn ugly, I might add) form.
All in all, a very nice Open Sourced tool (BSD license).