Skip to content

Instantly share code, notes, and snippets.

@mcclory
Last active July 22, 2018 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcclory/753037c7033d54178888ac698e75f28b to your computer and use it in GitHub Desktop.
Save mcclory/753037c7033d54178888ac698e75f28b to your computer and use it in GitHub Desktop.
A simple Wordpress backup script... it's not perfect, but it'll get the job done for most basic setups.
#!/bin/bash
# MySQL database credentials
DB_USER="" # put your wordpres mysql user name here
DB_PASS='' # put your database password here
DB_NAME="" # put your mysql database name here
DB_HOST="" # put your mysql host here
# Base path for wordpress files
WWW_DIR="/var/www/html" # set this to the base path of your wordpress install
# Set the date format, filename and the directories where your backup files will be placed and which directory will be archived.
BASE_URL="introspectdata.com"
NOW=$(date +"%Y-%m-%d-%H%M")
YEAR=$(date +"%Y")
MONTH=$(date +"%m")
DAY=$(date +"%d")
FILE="$BASE_URL.$NOW.tar"
BACKUP_DIR="/tmp"
DB_FILE="$BASE_URL.$NOW.sql"
# Tar transforms for better archive structure.
WWW_TRANSFORM='s,^"'${WWW_DIR:1:${#WWW_DIR}-1}',html,'
DB_TRANSFORM='s,^tmp,database,'
# Create the archive and the MySQL dump
# note: if you want to exclude specific plugins or other files, you can put additional --exclude statements in the tar command
tar -cvf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM --exclude='*/wp-config.php' $WWW_DIR
# mysql dump
echo -e "[mysqldump]\nuser=$DB_USER\npassword='$DB_PASS'" > /tmp/.my.cnf
mysqldump --defaults-extra-file=/tmp/.my.cnf -h$DB_HOST $DB_NAME > $BACKUP_DIR/$DB_FILE
rm -rf .my.cnf
# Append the dump to the archive, remove the dump and compress the whole archive.
tar --append --file=$BACKUP_DIR/$FILE --transform $DB_TRANSFORM $BACKUP_DIR/$DB_FILE
rm $BACKUP_DIR/$DB_FILE
gzip -9 $BACKUP_DIR/$FILE
# Optionally upload your backup file somewhere like S3...
# BUCKET_NAME= # name of the bucket to upload to
# aws s3 cp $BACKUP_DIR/$FILE s3://$BUCKET_NAME/$FILE
# Optionally delete the backup file... i.e. if you've uploaded it
# rm -rf $BACKUP_DIR/$FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment