WordPress backup script

Modifications to a bash script by Konstantin Kovshenin to backup a WordPress site.

[code lang=”bash”]
# This script creates a compressed backup archive of the given directory and the given MySQL table.
# More details on implementation here: https://theme.fm
# Feel free to use this script wherever you want, however you want. We produce open source, GPLv2 licensed stuff.
# Author: Konstantin Kovshenin exclusively for Theme.fm in June, 2011
# Modification by Pete Stoves at September 2013:
# Lives well with ubuntu virtual hosts etc.

# Make sure we have been passed a site name to backup
if [ -z "$1" ]
then
echo "Please supply a site name (virtual host directory)"
echo "wordpress-backup www.example.com"
exit 1
fi

# Our virtual host folder and site name
SITE=$1
# Set the date format, filename and the directories where your backup files will be placed and which directory will be archived.
NOW=$(date +"%Y-%m-%d-%H%M")
# The name of the final backup file we will create
OUT_FILE="$SITE.$NOW.tar"
# Where our backup will end up
OUT_DIR="/home/stovesy/"
# The base directory for our web server virtual sites
WWW_DIR="/srv/www/"$SITE"/"
# The path and name of the WordPress configuration file
CONFIG_FILE=$WWW_DIR"wp-config.php"
# Get MySQL database credentials
DB_USER=`awk ‘/DB_USER/ {print $2}’ $CONFIG_FILE | awk -F\’ ‘{print $(NF-1)}’`
DB_PASS=`awk ‘/DB_PASSWORD/ {print $2}’ $CONFIG_FILE | awk -F\’ ‘{print $(NF-1)}’`
DB_NAME=`awk ‘/DB_NAME/ {print $2}’ $CONFIG_FILE | awk -F\’ ‘{print $(NF-1)}’`
# The name of the MySQL db dump file
DB_FILE="$DB_NAME.$NOW.sql"
echo "Backing up $WWW_DIR"
echo "Read wordpress config file $CONFIG_FILE"
echo "MySQL is $DB_USER, $DB_PASS, $DB_NAME, to $DB_FILE"
# Tar transforms for better archive structure.
WWW_TRANSFORM=’s,^$WWW_DIR,www,’
DB_TRANSFORM=’s,^$OUT_DIR,database,’
# Create the archive and the MySQL dump
tar -cvf $OUT_DIR$OUT_FILE –transform $WWW_TRANSFORM $WWW_DIR
echo "Running mysqldump –user=$DB_USER –password=$DB_PASS $DB_NAME > $OUT_DIR$DB_FILE"
mysqldump –user=$DB_USER –password=$DB_PASS $DB_NAME > $OUT_DIR$DB_FILE
chown www-data:www-data $OUT_DIR$DB_FILE
# Append the dump to the archive, remove the dump and compress the whole archive.
tar –append –file=$OUT_DIR/$OUT_FILE –transform $DB_TRANSFORM $OUT_DIR/$DB_FILE
rm $OUT_DIR/$DB_FILE
gzip -9 $OUT_DIR/$OUT_FILE
[/code]

Leave a Reply