Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backing up small wp site with DB

Backup for small WP sites using dropbox and wp-cli and 🎩

Go to : https://www.dropbox.com/developers/apps

Create new app call it anything it's gonna be your private app.

Generated access token and save it somewhere in your locker 😄

Download file backup-site.sh and change directories path and DROPBOX_TOKEN

sudo chmod +x backup-site.sh

Setup crontab with crontab -e add following line there

0 5 * * * /root/backup-sites.sh > /dev/null 2>&1

This will backup site everyday at 5 am.

Notes:

  • This scripts assumes your installation will be under example.com/htdocs if not it's time to fork this and modify script.
  • This will backup your WP tables and wp-content folder.
  • You can also change path of backup-sites.sh.
  • You should clean up your dropbox folder before you run out of memory.
  • Modify crontab if you want to change backup interval to less frequent or more frequent.

##Credit:

#!/bin/bash
# Generate your Dropbox token: https://www.dropbox.com/developers/apps
DROPBOX_TOKEN={dropbox access token here}
# 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=( "foo.com" "bar.com" )
for dir in "${directories[@]}"
do
:
printf "Backing up $PREFIX/$dir \n"
cd $PREFIX/$dir/htdocs
printf "Exporting database... \n"
DATABASE_FILENAME=$dir.sql
wp db export --add-drop-table $DATABASE_FILENAME --allow-root
cd ..
printf "Compressing directory... \n"
BACKUP_FILENAME=$dir.$(date -d today "+%Y%m%d").tar.gz
tar czf $BACKUP_FILENAME htdocs/$DATABASE_FILENAME htdocs/wp-content/
printf "Uploading to Dropbox... \n"
curl -k --progress-bar -i --globoff -o /tmp/dropbox --upload-file $BACKUP_FILENAME https://api-content.dropbox.com/1/files_put/auto/$BACKUP_FILENAME -H "Authorization:Bearer $DROPBOX_TOKEN"
printf "Removing files... \n"
rm htdocs/$DATABASE_FILENAME $BACKUP_FILENAME
printf "Done!"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment