Tag Archives: backup

Backing up a WordPress site

I created this to have something simple to schedule backups of my WordPress site.
It’s tested OK on CentOS 5.5 and 5.6. It creates 2 bzip2 tarballs with timestamps, one for the website and one for the database. And it’s run once a day. It’s then shipped off to another system using Rsync and SSH.

You will need to edit the variables before running it. If you make copies of the script, you can use it to create backups of multiple sites. I hope this will be useful for more than myself.

#!/bin/bash
## DO NOT EDIT
DATE=`date +%d-%m-%Y`
## EDIT BELOW - Configuration parameters

WPSITE=/var/www/html/wordpress
WPSITE_NAME=mywebsite
WPBACKUP=/mnt/wordpress_backup

## STOP EDITING - Configuration parameters

if [ ! -d "$WPBACKUP" ]
then
        mkdir $WPBACKUP
fi

# Grabs the necessary MySQL information from the wordpress site.
DB_NAME=`echo "<?php require_once(\"${WPSITE}/wp-config.php\"); echo DB_NAME;" | php`
DB_USER=`echo "<?php require_once(\"${WPSITE}/wp-config.php\"); echo DB_USER;" | php`
DB_PASSWORD=`echo "<?php require_once(\"${WPSITE}/wp-config.php\"); echo DB_PASSWORD;" | php`
DB_HOST=`echo "<?php require_once(\"${WPSITE}/wp-config.php\"); echo DB_HOST;" | php`

# Dump the database
mysqldump --user=${DB_USER} --password=${DB_PASSWORD} --host=${DB_HOST} $DB_NAME | bzip2 -c > $WPBACKUP/$WPSITE_NAME-db-$DATE.sql.bz2

# Tarballs the database and wordpress files with bzip2
tar -jcvf $WPBACKUP/$WPSITE_NAME-backup-$DATE.tar.bz2 --exclude cache $WPSITE/

// CrashMAG