In recent weeks, I’ve seen a lot of friends sign up for Dropbox. Dropbox is an online server that lets you store files that are then retrievable from any computer with an internet connection. They also provide the ability to share files with friends or the public.
The great thing about having an easy to use file storage service online is that it enables you to create a backup of your files available from any computer on the internet. Unfortunately, Dropbox doesn’t provide you with a way to automatically back up your files without manually copying them to your Dropbox folder. This little how-to will guide you through the process of automatically backing up your files using Cron and Dropbox. (NOTE: These instructions are for Mac users running OS X. The instructions will be very similar if you are using *nix. If you are using Windows, you can use Windows Scheduler instead of Cron to accomplish the same thing.)
The first thing that you’ll need to do is get Dropbox set up and working. If you already have a Dropbox account and the software installed, feel free to skip over this part to the next section.
- To get Dropbox working, you’re going to need to do is to create a Dropbox account.
- Once your account has been created, download and install the Dropbox software.
- Once the Dropbox software is installed and configured, it will create a new folder in your user directory. For me, the folder is /Users/Grant/Dropbox. Any files in this directory will be uploaded to Dropbox’s server and available online.
Now that Dropbox is set up, it’s time to get it to back up your files automatically. We’re going to do this with a cron job that will copy whatever files you want backed up into the /Dropbox folder.
- For purposes of this tutorial, I’m going to assume we want to back up your Documents folder every hour. Please note, however, that Dropbox limits how much free space they provide. If your Documents folder is larger than the space they provide, you will either need to purchase more space or be more selective about what you’re backing up.
- Go into your Applications folder and start Terminal.app.
- In Terminal, type
crontab -e, a new file will open up that is most likely blank with every line showing a ~, this is your list of cron jobs. Cron jobs run specific commands at predetermined intervals. We’re going to add a new cron job to the list.
- First, we must determine how often to run your backup script. As I said earlier, we’re going to assume we want it backed up every hour. To do this we start the line by typing in
0 * * * *. This says that when the minute says 0 (i.e. at the top of the hour), on every hour, every day, every month, and every day of the week, run this command.
- Now that cron knows how often to run the command, we need to tell it what command to run. This is a very basic copy command. On the same line that we have
0 * * * *, add
cp -r /Users/<USERNAME>/Documents /Users/<USERNAME>/Dropbox/Backup. This says that we’re going to recurrsively copy (
cp -r) our source directory (
/Users/<USERNAME>/Documents) and place it in our destination directory (
/Users/<USERNAME>/Dropbox/Backup). Of course, you’ll have to replace with whatever your login name is on your computer. For me it’s Grant, for you it’s probably something different.
- You should end up with a single line in cron that reads
0 * * * * cp -r /Users/<USERNAME>/Documents /Users/<USERNAME>/Dropbox/Backup.
- Now, the final step is saving your cron job. In your Terminal window, hit
:wq. This will write the cron job to a file and then quit out of crontab.
- Quit out of Terminal.app.
Now, all of the files in your Documents folder should be being copied to the Dropbox folder and uploaded to Dropbox’s website. If you want to backup multiple directories, not just your Documents folder, or a subset of your Documents folder because of size constraints, just add additional lines to crontab, changing the source directory location.
I hope this short tutorial helped you create an automatic backup of all of your documents. If you have any questions or problems, feel free to leave a comment.