Skip to content

Instantly share code, notes, and snippets.

@drawcard
Forked from codeablehq/backup.sh
Last active February 7, 2017 12:36
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 drawcard/d48ba3113934c6b171d81e76b4564df3 to your computer and use it in GitHub Desktop.
Save drawcard/d48ba3113934c6b171d81e76b4564df3 to your computer and use it in GitHub Desktop.
A script that creates a backup of your WordPress site and uploads it to Dropbox
#!/bin/bash
# For this script to work, save it somewhere in the executable path, like /usr/local/sbin/backup.sh
# make it executable: chmod +x /usr/local/sbin/backup.sh
# then add it to cron: crontab -e
# and add the line below, which will run backup 3am each day, then upload to Dropbox
# 0 3 * * * /usr/local/sbin/backup.sh > /dev/null 2>&1
# You also need WP CLI installed: http://wp-cli.org/
# Generate your Dropbox token: https://www.dropbox.com/developers/apps ("Generate access token" section)
DROPBOX_TOKEN=
# Directory that holds your WordPress sites' root folders
PREFIX=/var/www/html
# If you have multiple folders with WordPress sites, add/remove them from this array
# Syntax: directories=( "foo" "bar" )
# TO DO: Automatically scan directory, find all folders, and add as array
directories=( "tcw" )
# the logic, shouldn't need to modify anything below
for dir in "${directories[@]}"
do
:
printf "Backing up $PREFIX/$dir:\n"
cd $PREFIX/$dir
printf "Exporting database...\n"
/usr/local/bin/wp db export --add-drop-table
cd ..
printf "Compressing directory...\n"
BACKUP_FILENAME=$dir.$(date -d today "+%Y%m%d").tar.gz
tar czf $BACKUP_FILENAME $dir
printf "Uploading to Dropbox...\n"
curl -k --progress-bar -i --globoff -o /tmp/dbrnd \
--upload-file $BACKUP_FILENAME https://api-content.dropbox.com/1/files_put/auto/$BACKUP_FILENAME \
-H "Authorization:Bearer $DROPBOX_TOKEN"
# Comment out the next line if you want the backup to stay on the server
rm $BACKUP_FILENAME
printf "Done!\n"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment