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" ]
echo "Please supply a site name (virtual host directory)"
echo "wordpress-backup www.example.com"
exit 1

# Our virtual host folder and site name
# 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
# Where our backup will end up
# The base directory for our web server virtual sites
# The path and name of the WordPress configuration file
# 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
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.
# Create the archive and the MySQL dump
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
gzip -9 $OUT_DIR/$OUT_FILE

Leave a Reply