Dropbox is pretty awesome, and enhancing the Automatic WordPress Backup script to send the files to Dropbox is fairly straight forwards.

You can find the original post here.

This tutorial is going to give us an off-site backup of anything we want. That’ll occur automatically, like magic. :D

As usual, ssh into your server.

But before we get started, let’s go ahead and create an app on Dropbox which we can use for authenticating our system.

Go ahead and login to the developer backend.

Click create app.

You’ll need access to the Dropbox API.

I chose Full Dropbox for the access type – but it should still work on App folder.

Finally, name it something meaningful (and unique).

On the settings screen, scroll down to Generated access token and press generate. You’ll need the long access token in a moment.

There’s an excellent Dropbox backup Shell script which can be found on GitHub, we’ll be using this in our implementation.

https://github.com/andreafabrizi/Dropbox-Uploader

Let’s pull it in and set it up!

cd /tmp
curl "https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh" -o dropbox_uploader.sh
chmod +x dropbox_uploader.sh
./dropbox_uploader.sh

You’ll be asked for the access token, paste it in and continue with the wizard.

Awesome, at this point you have a command line util connecting to your Dropbox. Uploading our backups is now a cinch. See the new script below, changes highlighted.

Be sure to replace /home/eliottrobson/.dropbox_uploader with the profile location for your user.

#!/bin/bash

#
# This script backs up the entire WordPress Database and filesystem, using
# the file naming format will determine how often the backup will take place.
# The backups are then synced to Dropbox
#
# Author: Eliott Robson (https://eliottrobson.me)
#

WP_ROOT=/var/www/html
BAK_ROOT=/var/www/backups/
DB_ROOT=/Backups/Websites/eliottrobson.me

CORE_FOLDER=$(date +%Y/%m)
CORE_DB=$(date +%Y%m%d)_database.sql
CORE_FILES=$(date +%Y%m%d)_files.tar.gz

BAK_FOLDER=${BAK_ROOT}/${CORE_FOLDER}
BAK_DB=${BAK_FOLDER}/${CORE_DB}
BAK_FILES=${BAK_FOLDER}/${CORE_FILES}

DB_FOLDER=${DB_ROOT}/${CORE_FOLDER}
DB_DB=${DB_FOLDER}/${CORE_DB}
DB_FILES=${DB_FOLDER}/${CORE_FILES}

# Check interactive shell
ECHO_CLEAR=""
ECHO_RED=""
ECHO_YELLOW=""
ECHO_GREEN=""
ECHO_BLUE=""

if [ -t 1 ]; then
	ECHO_CLEAR="\e[0m"
	ECHO_RED="\e[1;31m"
	ECHO_YELLOW="\e[1;33m"
	ECHO_GREEN="\e[1;32m"
	ECHO_BLUE="\e[1;36m"
fi

# Indicate start of backup, useful for cron logs
echo -e "${ECHO_YELLOW}Beginning backup sequence.${ECHO_CLEAR}"
echo -e "${ECHO_YELLOW}$(date "+%Y-%m-%d %H:%M:%S")${ECHO_CLEAR}"

echo ""

mkdir -p ${BAK_FOLDER}
/opt/dropbox/dropbox_uploader.sh -f /home/eliottrobson/.dropbox_uploader mkdir ${DB_FOLDER}

cd ${WP_ROOT}

# Backup database
echo -e "${ECHO_YELLOW}Attempting database backup.${ECHO_CLEAR}"
if [ ! -f "$BAK_DB" ]
then
    wp db export ${BAK_DB} --add-drop-table
    /opt/dropbox/dropbox_uploader.sh -f /home/eliottrobson/.dropbox_uploader upload ${BAK_DB} ${DB_DB}
else
    echo -e "${ECHO_BLUE}Skipped:${ECHO_CLEAR} File already exists."
fi

echo ""

# Backup filesystem
echo -e "${ECHO_YELLOW}Attempting filesystem backup.${ECHO_CLEAR}"
if [ ! -f "$BAK_FILES" ]
then
    tar --create --verbose --listed-incremental ${BAK_FOLDER}/bak.snar --gzip --file ${BAK_FILES} .
    /opt/dropbox/dropbox_uploader.sh -f /home/eliottrobson/.dropbox_uploader upload ${BAK_FILES} ${DB_FILES}
    /opt/dropbox/dropbox_uploader.sh -f /home/eliottrobson/.dropbox_uploader upload ${BAK_FOLDER}/bak.snar ${DB_FOLDER}/bak.snar
    echo -e "${ECHO_GREEN}Success:${ECHO_CLEAR} Exported to '${BAK_FILES}'."
else
    echo -e "${ECHO_BLUE}Skipped:${ECHO_CLEAR} File already exists."
fi

echo ""
echo "------------------------------"

Now go ahead and relax. Let Dropbox handle the power and data problems, turns out they’re pretty good at that sort of thing!

There are currently no comments.