Although there are many services and plugins that allow you to backup WordPress site (usually for a small fee), the easiest way to get going is to simply upload a compressed file of your WordPress files and database to your Dropbox.

Prerequisites

This tutorial assumes you have your own VPS (shared hosting won’t work as you need WP-CLI installed, but for those with the technical skills it should be easy to adapt the script we’re going to use. Also some basic linux shell knowledge is required, although nothing complicated, and I’ll explain each step in detail. Lastly, if you have multiple WordPress sites to backup, they should all be in the same folder, like /var/www/wpsite1.com, /var/www/wpsite2.com for this to work (don’t worry if you don’t, though).

Let’s get started.

Create a Dropbox application

In order to access Dropbox via their API, you first need to create an application. To do so, visit Dropbox App Console and create a new application and select/insert options similar to the following screenshot:

WordPress Backup Drobox developers panel
Drobox developers screen

Once your app is created, you need to generate an OAuth 2 access token (on the app page you just created), which you’ll need in the custom shell script we’re about to write.

WordPress Dropbox access token
Dropbox access token

That’s it, save/copy the token and close the tab, you won’t need it anymore.

Log in to your VPS

Now that we have our necessary token, let’s SSH onto the server and create a file, preferably somewhere in the $PATH:

$ sudo nano /usr/local/sbin/backup.sh

This will create an unsaved file called backup.sh, which you should paste the following content in (don’t worry if you don’t understand everything, I’ll explain what it does after the snippet):

#!/bin/bash

# Generate your Dropbox token: https://www.dropbox.com/developers/apps
DROPBOX_TOKEN=<your dropbox token>

# Directory that holds your WordPress sites' root folders
PREFIX=/home/webmaster/www

# If you have multiple folders with WordPress sites, add/remove them from this array
directories=( "wp_folder_1" "wp_folder_2" )

for dir in "${directories[@]}"
do
  :
  printf "Backing up $PREFIX/$dir:n"
  cd $PREFIX/$dir

  printf "Exporting database...n"
  DATABASE_FILENAME=$dir.sql
  /usr/local/bin/wp db export --add-drop-table $DATABASE_FILENAME
  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/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 $dir/$DATABASE_FILENAME
  rm $BACKUP_FILENAME

  printf "Done!n"
done

What the script above does (lines numbers are off if you look the code above, but should be correct if you pasted it into the file):

  • line 1 tells the shell which interpreter to use
  • lines 3-10 are config parameters which you need to edit according to your needs and locations of the WP directories
  • line 12 creates a loop that goes through all the WP directories and uploads each and every one
  • line 20 exports the database to a file inside your WP directory
  • line 25 compresses directory with all the files and exported database with a same name as your WP directory
  • line 28 uploads the compressed filename to your Dropbox folder
  • lines 33 and 34 remove the compressed file and database dump so it’s not accessible to download by typing in correct URL

Now save the file and exit the editor, but before we can use it, we make need to it executable (runnable):

$ sudo chmod +x /usr/local/sbin/backup.sh

You can now manually test it by entering the full path to the file:

$ /usr/local/sbin/backup.sh

If all goes well, you should see something like this:

WordPress Dropbox backup progress
Dropbox upload progress

Setting up Cron

Now that our script is working properly, it’s time to automate it, otherwise we’d have to run it manually each day. Luckily, there’s Cron to provide that kind of functionality for us.

In order to set it up, just run the following command:

$ crontab -e

This will open Cron editor which should already have some basic instructions on how to use it, but to make our script being ran on a daily basis, add this line at the very end:

0 3 * * * /usr/local/sbin/backup.sh > /dev/null 2>&1

The first five characters of this line mean that the backup should be ran each day (or rather night) at 3 am, follwed by the location of the file to execute and what’s left are instructions what to do with any output (text) that running the script generated. I’ve set it to /dev/null, which basically means the output should be discarded.

That’s it, save the cron instructions and close editor - you’re done!

Conclusion

As you see, all it takes to automate your WP backups is a simple script and a (free) Dropbox account. While you could copy the backup file to some directory on the server, that’s not really a true backup - if your server gets hacked, you’ll most likely loose those files too!

Of course if you want more functionality you can always use a service like VaultPress or BackupBuddy which also support restoring from backup points with a click and many other features.

Next week we're resuming our efforts to create a crazy-fast WordPress site, so I'm going to show you what I think is the ultimate trick to load pages almost instantly!

Don't forget to comment if you found this article useful or have any questions!