Skip to content

Instantly share code, notes, and snippets.

@w3guy
Forked from codeablehq/backup.sh
Last active August 29, 2015 14:12
Show Gist options
  • Save w3guy/a619356f2ac124531565 to your computer and use it in GitHub Desktop.
Save w3guy/a619356f2ac124531565 to your computer and use it in GitHub Desktop.
#!/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
DROPBOX_TOKEN=<your dropbox token>
# Directory that holds your WordPress sites' root folders
PREFIX=/var/www
# If you have multiple folders with WordPress sites, add/remove them from this array
directories=( "wp_folder_1" "wp_folder_2" )
# 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