backup

All posts tagged backup

Now that I have invested some time and content into my blog I decided to get serious about making sure I have a good backup system. One of the primary reasons I use Dropbox is because of its awesome cross-platform support for Windows, Mac, and Linux so I figured some genius must have already figured out how to make backups work from Linux.

I found a script called Dropbox Uploader hosted on GitHub that appeared to be exactly what I needed. Follow the instructions on the project’s site to either git clone the repo or simply (as I did) use curl to pull it down to your webserver. Once you execute the script for the first time you will be prompted to access a couple of URL’s, setup a new Dropbox app and provide authorization.

Once you have that working you can upload and download anything from the command line. You should test to make sure its working at this point since from here on we’re focusing on automation.

I created a script to prepare my backup before invoking the Dropbox uploader then clean it all up when its done. Its really a hack, there’s no error checking or backup file rotation. It will continue to backup until you run out of space on your Dropbox so keep that in mind.

Change the 3 bolded values to match your site:

create_site_backup.sh

#!/bin/bash

#Setup Environment

BACKUP_SRC=”/var/www/SITENAME
BACKUP_DST=”/tmp”
MYSQL_SERVER=”127.0.0.1″
MYSQL_USER=”root
MYSQL_PASS=”PASSWORD
NOW=$(date +”%Y.%m.%d”)
DESTFILE=”$BACKUP_DST/$NOW.tgz”

# Dump MySQL DB + Compress
mysqldump -u $MYSQL_USER -h $MYSQL_SERVER -p$MYSQL_PASS –all-databases > “$NOW-Databases.sql”
tar cfz “$DESTFILE” $BACKUP_SRC “$NOW-Databases.sql”
rm -f “$NOW-Databases.sql”

/root/bin/dropbox_uploader.sh upload “$DESTFILE”

rm -f “$DESTFILE”

Kick it off once and you should see Dropbox sync a tarball with your entire site inside:

Fullscreen capture 7192013 111405 AM

On CentOS any script in root’s /bin directory is automatically in the PATH. I put both my dropbox_upload.sh and create_site_backup.sh scripts in /root/bin.

Edit root’s crontab to kickoff the script every night at 2AM:

crontab -e

# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 * * * /root/bin/create_site_backup.sh

That’s it. Completely hands-off Dropbox backups of your self-hosted WordPress site. Keep an eye on it and stay tuned for an update on how to add some enhancements to the script.