dropboxlogo

I recently discovered the power of dropbox. (2GB for free!) and decided I would deploy it to backup all my school documents and files from my H drive (aka my home directory)
that resides on my school’s servers. I have access to my department server via a bash shell and decided I would install dropbox (it runs out of a directory) and
sync certain directories residing in my home drive with it.

One problem….The dropbox directory.

Basically, for anything to be synced to dropbox, it must reside in the Dropbox folder that the program creates when you install it.
So you throw anything you want local and on dropbox in that folder and it syncs and maintains an exact copy of what is in there.

The problem is that I have already established directory structures for the things I want to backup and don’t want to have to move everything into a stupid
dropbox dir. The answer….lies in hard links…

First of all, let me explain hard links. All they are is another alias to the data that represents a file. Where a symlink is a link to the name of a file, a hard link is a link to the actual file and its contents. This means that if you have 3 hard links to a file, any hard link you use to modify that file will reflect to all the other hard links since they all point to the same data. We will use hardlinks because it will make life much more easier, we are on the same filesystem, and symlinks can be a pain in the butt. (plus they don’t work all the time)

So go ahead and install dropbox for linux on your server (they have install instuctions for linux here) and it should automatically make your Dropbox directory (make sure you are working in your home directory)

So after you have done this and you have the dropbox client running in the background ($ ~/.dropbox-dist/dropboxd &), you can make your bash script to setup your hard links and keep the directories synced.

NOTE: When you edit your files, those changes should be reflected to dropbox since the hard links are to the same data. But if you create a new file, the new file will not be synced to Dropbox until you run this script..

Here we are:

#This script will create hardlinks for the selected directories that I want to backup to dropbox via the dropbox dir! #Add links for new files that were added cp -al ~/Hours/ ~/Dropbox/ cp -al ~/School.Documents/ ~/Dropbox/ cp -al ~/Desktop/Resume/ ~/Dropbox/ #clean files that have been deleted from main directory and now only have link left in dropbox dir find ~/Dropbox/Hours -type f -links 1 -delete find ~/Dropbox/School.Documents -type f -links 1 -delete find ~/Dropbox/Resume -type f -links 1 -delete #clean empty dirs just in case find ~/Dropbox/Hours -depth -type d ! -name . -exec rmdir –ignore-fail-on-non-empty {} \; find ~/Dropbox/School.Documents -depth -type d ! -name . -exec rmdir –ignore-fail-on-non-empty {} \; find ~/Dropbox/Resume -depth -type d ! -name . -exec rmdir –ignore-fail-on-non-empty {} \;

So reading the comments, you should see exactly what this script does. Basically, I want to backup the Hours, School.Documents, and Resume directories to my dropbox.

That’s it…Just make a cron job for this script to run whenever you want and as long as you have dropboxd running, everything should sync perfectly!

Mario Loria is a builder of diverse infrastructure with modern workloads on both bare-metal and cloud platforms. He's traversed roles in system administration, network engineering, and DevOps. You can learn more about him here.