Backup Wordpress.com Blogs

Posted on June 12th, 2008 by dandavis
Tags: ,

OK, first off, I'll admit that there might be a better way to do this automatically. I don't use Wordpress.com, so I don't know. I know with my own copy of Wordpress, I have plugin options for backing up my database.

A friend of mine asked if I knew a way to backup his Wordpress.com blog. He knew how to download the XML dump, but he wanted an automated way to do it.

I hacked this out in a few minutes… and it seems to work. I haven't tested it extensively, but my friend seems happy with it.

#!/bin/bash

# Edit these to match your blog.
USERNAME="<WORDPRESS.COM USERNAME>"
PASSWORD="<WORDPRESS.COM PASSWORD"
URL="http://<BLOG URL>.wordpress.com"
   
DATE=`date +'%Y%m%d'`
   
if [[ $1 == "" ]]; then
   # If not specified on the command line, the default directory is…
   BACKUP="."
else
   # Otherwise we'll use the one that the user wants.
   BACKUP=$1
   # if BACKUP doesn't exist, make it.
   [[ ! -d ${BACKUP} ]] && mkdir -p ${BACKUP}
fi
BACKUP="${BACKUP}/backup.${DATE}.xml"
   
# Set some default file names…
COOKIES="/tmp/cookies.txt.${DATE}.$$"
OUTPUT="/tmp/output.html.${DATE}.$$"
   
# This is the command we'll use to connect to wordpress.com…
WGET="wget --load-cookies=${COOKIES} --save-cookies=${COOKIES} --keep-session-cookies -q -O"
   
# First, we'll login and get the cookies…
POSTDATA="log=${USERNAME}&pwd=${PASSWORD}"
POSTDATA="${POSTDATA}&rememberme=forever&wp-submit=Log%20In"
POSTDATA="${POSTDATA}&redirect_to=wp-admin/&testcookie=1"
${WGET} ${OUTPUT} --post-data="${POSTDATA}" ${URL}/wp-login.php
   
# Then, using those ever so tasty cookies, we'll download an XML back from wordpress.com…
${WGET} ${BACKUP} "${URL}/wp-admin/export.php?author=all&download=true"
   
# Remove our temporary files…
rm -f ${OUTPUT} ${COOKIES}

I called it wpbackup.sh.

To use it, you can just run it at the command-line with no options and it'll pop out an XML backup of your blog. Or, you can set it up in cron to save a backup periodically depending on how much your content changes. Run from cron, I would suggest specifying a command-line option for the directory into which to save the backups:

wpbackup.sh /home/me/wpbackups

Enjoy. And if you find any bugs, post a comment. If you improve on it in other ways, post it somewhere and take credit… just post a comment here letting me know… maybe you'll have an idea I can steal and use in my own scripts. :)

Post a comment

You must be logged in to post a comment.