Created
April 12, 2013 15:05
-
-
Save cwlls/5372693 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ThriftyPants Wordpress backup script <backup-tp.sh> | |
# This script was created with help from: | |
# http://theme.fm/2011/06/a-shell-script-for-a-complete-wordpress-backup-4/ | |
# http://www.guyrutenberg.com/2013/03/28/incremental-wordpress-backups-using-duply-duplicity/ | |
# http://www.mobiledev.nl/backup-complete-wordpress-site-with-a-script/ | |
# Copyright (c) 2013, Chris Wells <[email protected]> | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# | |
# * Redistributions of source code must retain the above copyright notice, | |
# this list of conditions and the following disclaimer. | |
# | |
# * Redistributions in binary form must reproduce the above copyright notice, | |
# this list of conditions and the following disclaimer in the documentation | |
# and/or other materials provided with the distribution. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
# Files and directories | |
NOW=$(date +"%Y%m%d_%H%M") | |
SITE="<INSERT SITE NAME HERE>" | |
FILE="$SITE-$NOW.tar" | |
WWW_DIR="<INSERT FULL PATH TO WP INSTALL DIR>" | |
BACKUP_DIR="<INSERT FULL PATH TO LOCAL BACKUP DIR>" | |
DB_FILE="$SITE-$NOW.sql" | |
WWW_XFORM="s,^${WWW_DIR:1},www," | |
DB_XFORM="s,^${BACKUP_DIR:1},database," | |
# Grab DB information from wp-config.php | |
DB_NAME=`echo "<?php require_once(\"${WWW_DIR}/wp-config.php\"); echo DB_NAME;" | php` | |
DB_USER=`echo "<?php require_once(\"${WWW_DIR}/wp-config.php\"); echo DB_USER;" | php` | |
DB_PASS=`echo "<?php require_once(\"${WWW_DIR}/wp-config.php\"); echo DB_PASSWORD;" | php` | |
DB_HOST=`echo "<?php require_once(\"${WWW_DIR}/wp-config.php\"); echo DB_HOST;" | php` | |
# Specify **EITHER** a GPG key as an encryption recipient, or a passphrase | |
# for symmetric encryption. If set, the key takes precedence. | |
ENC_RECP="" | |
ENC_PASS="" | |
# Remote rsync endpoint. If left empty, no remote backup will occur | |
RSYNC_USER="" | |
RSYNC_PASSWORD="" | |
RSYNC_PORT="" | |
RSYNC_HOST="" | |
RSYNC_MODULE="" | |
# Create backup of www directory | |
echo "Gathering www files" | |
tar -cf $BACKUP_DIR/$FILE --transform $WWW_XFORM $WWW_DIR/ | |
# Create backup of database and append it to the backup tar file | |
echo "Creating DB dump and adding to archive" | |
mysqldump -u$DB_USER -p$DB_PASS $DB_NAME -r $BACKUP_DIR/$DB_FILE | |
tar --append --file=$BACKUP_DIR/$FILE --transform $DB_XFORM $BACKUP_DIR/$DB_FILE | |
rm $BACKUP_DIR/$DB_FILE | |
# Compress backups using gzip | |
echo "Compressing backup" | |
gzip -9 $BACKUP_DIR/$FILE | |
# Encrypt the backup using GPG | |
if [[ $ENC_RECP ]]; then | |
echo "Encrypting backup" | |
gpg -e -r $ENC_RECP "${BACKUP_DIR}/${FILE}.gz" | |
find $BACKUP_DIR/*.gz -type f -exec rm {} \; | |
elif [[ $ENC_PASS ]]; then | |
echo "Encrypting backup (symmetrically)" | |
echo $ENC_PASS | gpg -c --batch --passphrase-fd 0 --output "${BACKUP_DIR}/${FILE}.gz.gpg" "${BACKUP_DIR}/${FILE}.gz" | |
find $BACKUP_DIR/*.gz -type f -exec rm {} \; | |
fi | |
# Remove all backups older than 2 weeks | |
find $BACKUP_DIR/* -type f -mtime +14 -exec rm {} \; | |
# Rsync the local backup directory to remote location | |
if [[ $RSYNC_USER ]]; then | |
echo "Sending backup offsite" | |
export RSYNC_PASSWORD | |
rsync --inplace --rsh="ssh -p${RSYNC_PORT}" $BACKUP_DIR/*.gpg $RSYNC_USER@$RSYNC_HOST::$RSYNC_MODULE/$SITE/ | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment