Backup WordPress to Dropbox on Amazon EC2 Centos Instance

I have been running multisite WordPress on my Amazon EC2 instance for almost a year and now that I have four sites running concurrently it is time to sort out a proper backup routine. Having had a look at all of the options and plugins it seemed like a good idea to backup WordPress to Dropbox and because of the size of my instance wouldn’t cost anything. I also wanted full control via Linux rather than installing a plugin which I didn’t trust. Also a good chance to do some more learning. Figured it would be a good idea to put the steps here so someone else can create should they want to do the same. I decided to run Dropbox under the ec2user as that is how everything else on my server is setup.

Backup WordPress to Dropbox

Install Dropbox

Download the latest stable Dropbox.

32bit

cd ~ && wget -O - "https://www.dropbox.com/download?plat=lnx.x86" | tar xzf -

64bit

cd ~ && wget -O - "https://www.dropbox.com/download?plat=lnx.x86_64" | tar xzf -

Lets start Dropbox

~/.dropbox-dist/dropboxd

You should get a message saying your account isn’t syncing and to visit a URL. Copy the URL to a browser and login. Wait for the welcome message and then use Ctrl-C to exit. Now lets ensure that Dropbox will start when we restart our instance. Create a startup script.

sudo vim /etc/init.d/dropbox

If you are using a stock Amazon Centos OS then copy the below. If not check for a script at the bottom of this page.

# chkconfig: 345 85 15
# description: Startup script for dropbox daemon
#
# processname: dropboxd
# pidfile: /var/run/dropbox.pid
# config: /etc/sysconfig/dropbox
#

### BEGIN INIT INFO
# Provides: dropboxd
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $syslog
# Should-Start: $syslog
# Should-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start up the Dropbox file syncing daemon
# Description: Dropbox is a filesyncing sevice provided by dropbox.com
# This service starts up the dropbox daemon.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

# To configure, add line with DROPBOX_USERS="user1 user2" to /etc/sysconfig/dropbox
# Probably should use a dropbox group in /etc/groups instead.

[ -f /etc/sysconfig/dropbox ] && . /etc/sysconfig/dropbox
prog=dropboxd
lockfile=${LOCKFILE-/var/lock/subsys/$prog}
config=${CONFIG-/etc/sysconfig/dropbox}
RETVAL=0

start() {
   echo -n $"Starting $prog"
   if [ -z $DROPBOX_USERS ] ; then
      echo -n ": unconfigured: $config"
      echo_failure
      echo
      rm -f ${lockfile} ${pidfile}
      RETURN=6
      return $RETVAL
   fi
   for dbuser in $DROPBOX_USERS; do
      daemon --user $dbuser /bin/sh -c "~$dbuser/.dropbox-dist/dropboxd&"
   done
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch ${lockfile}
   return $RETVAL
}

status() {
   for dbuser in $DROPBOX_USERS; do
      dbpid=`pgrep -u $dbuser dropbox | grep -v grep`
      if [ -z $dbpid ] ; then
         echo "dropboxd for USER $dbuser: not running."
      else
         echo "dropboxd for USER $dbuser: running (pid $dbpid)"
      fi
      done
}

stop() {
   echo -n $"Stopping $prog"
   for dbuser in $DROPBOX_USERS; do
      killproc ~$dbuser/.dropbox-dist/dropbox
   done
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

# See how we were called.
case "$1" in
   start)
      start
      ;;
   status)
      status
      ;;
   stop)
      stop
      ;;
   restart)
      stop
      start
      ;;
   *)
      echo $"Usage: $prog {start|status|stop|restart}"
      RETVAL=3

esac

exit $RETVAL

Now we need to add a file which contains the ec2-users that we want to run the service.

sudo vim /etc/sysconfig/dropbox

Add the following line to the file and save (:wq!)

DROPBOX_USERS="ec2-user"

Now we need to sort the file permissions.

sudo /bin/chmod 0755 /etc/init.d/dropbox
sudo /bin/chmod 0644 /etc/sysconfig/dropbox
sudo /bin/ls -l /etc/init.d/dropbox /etc/sysconfig/dropbox

Turn it on

sudo chkconfig dropbox on

Check that it worked, look for Dropbox in the list.

sudo chkconfig --list | egrep '3:on|5:on' | less

Start the service

sudo service dropbox start

Hopefully everything should now be working fine.

Move Dropbox Folder

I moved my Dropbox folder to my EBS volume so it would persist, then symbiotically linked it back to the home directory.

sudo service dropbox stop
sudo mv ~/Dropbox /vol/Dropbox
sudo ln -s /vol/Dropbox ~/
sudo service dropbox start

Now it is time to tidy up the directories where Dropbox sit and ensure that only the folders you want synced are turned on.

Turn On Dropbox Selective Sync

Go to your home directory

cd ~

We now need to download the Python Script which is used to manage Dropbox from the Command Line and give it the right permissions.

wget -O ~/dropbox.py "http://www.dropbox.com/download?dl=packages/dropbox.py"
chmod 755 ~/dropbox.py

Now lets check Dropboxes status

./dropbox.py status

Hopefully should now be syncing up, most likely you won’t want everything to sync so you can use the ‘exclude add’ command to stop folders you don’t need. Run the below and it will show your synced folders.

cd Dropbox
~/dropbox.py ls

You can stop each of them syncing by using the following command.

~/dropbox.py exclude add ~/Dropbox/Public
~/dropbox.py exclude add ~/Dropbox/'Camera Uploads'

If you run the below again you can see that they are dropping off.

~/dropbox.py ls

Also worth turning off LAN sync as you won’t need it.

~/dropbox.py lansync n

Backup WordPress to Dropbox Including Database and Content

Create a folder for backups.

mkdir /vol/Dropbox/Backup

Now we want to write a quick script to copy the files from html/wp-content to Dropbox/Backup/wp-content and dump the MySQL. Create the below file, it should be synced to your other computers as soon as you save.

vim /vol/Dropbox/Backup/backup.sh

Here is the script, change the paths /vol/html/ to where you have your wp-content folder and /vol/Dropbox/Backup to where your Dropbox sits, plus update your MySQL password.

#!/bin/sh

echo "Backup Start"

echo "Syncing wp-content Files from html to Dropbox"

sudo rsync -vaz --delete --update /vol/html/wp-content/ /vol/Dropbox/Backup/wp-content/

echo "Backing up database"

mysqldump -uroot '-p[password]' --single-transaction --routines --triggers --all-databases > /vol/Dropbox/Backup/backup_db.sql

echo "Backup Finish"

Now lets run it to see if it works. (Update: previously I had used cp to copy all the files across every night. However I just noticed that it melts my server doing the update so have just updated to use rsync and just copy the files that have changed. Showing my Linux rookie credentials.)

sudo chmod u+x /vol/Dropbox/Backup/backup.sh
sudo /vol/Dropbox/Backup/backup.sh

Now we have our script sorted we need to create a cron job to run it once a day at 1am. You will need to stop cron.

sudo service crond stop

Now lets edit the file which runs our jobs.

vim /etc/crontab

Insert the below line, below this one “# * * * * * user-name command to be executed”:

0 1 * * * root ./vol/Dropbox/Backup/backup.sh

Save and start cron backup.

sudo service crond start

You can now rest easily that every night at 1am you canĀ backup wordpress to dropbox including theĀ entire wp-content folder plus a database dump. Perfect for the small time blog runner. Props to the following sites that were a big help in this process.

Music To Write This Code To

Richie Hawtin – Essential Mix: Live At Watergate, Berlin. Hopefully shouldn’t take you two hours to do this but this is just the type of mix to get lost in code to. Off to Berlin next month and if I hear anything like this I would be very happy.

Leave a Reply

%d bloggers like this: