Quick WordPress backup script

A friend is thinking about moving his blogger service to WordPress on my server. However he says he doesn’t trust databases, and that text files rule. To make him feel happier sleeping at night I put together a quick script to backup the wordpress data in the mysql database to a text file.

Note, you can s//g replace wp_ with which prefix you are using to get your particular table structure. The user -u bak is setup with no password and only select privileges to the wordpress database.

If I was very keen I could spend a little more time, have the script produce a differ between the last version and the working version, then pipe the output to stdout. He could then do a backup via ssh remotely whenever he, for instance, posted a new message.


#!/bin/bash
# Backup script for WordPress data

user=bob
TABLES="wp_categories wp_comments wp_linkcategories wp_links \
wp_optiongroup_options wp_optiongroups wp_options wp_optiontypes \
wp_optionvalues wp_post2cat wp_postmeta wp_posts wp_users"

DATE=`/bin/date -Iminutes`

TARGETDIR=/home/$user/backup/

if [ ! -d $TARGETDIR ]; then
echo No $TARGETDIR
exit 1
fi

TARGETFILE="$TARGETDIR/nic.wordpress-$DATE.bz2"

mysqldump -u bak -h host. wordpress $TABLES | bzip2 -c > $TARGETFILE

Comments are closed.