Easily Back Up Folders to a USB Flash Drive on Mac

I have a problem and am ready to admit it: I hate creating regular back ups of my data. I know that it is a crime at this day and age not to have regular back ups (and several copies at that) of your data. I own a MacBook Pro and have set up Time Machine on a 1-TB external HDD, which I use to back up my whole MacBook once a year — namely before I upgrade to the latest MacOS in September. So why don’t I do it more often? Well, for one I use my MacBook for work and my HDD is at home (I can hear the eyes rolling out there: “why don’t you just move your HDD to your office?” I hear you ask. Well, this brings me to the second and third points). Point two: it takes a lot of time to create snapshots of my hard-drive. Actually, the data I really care about are located in one folder that I use for work. Everything else I don’t need to back up regularly. Point three is: I don’t want my back ups to stay in the office since they can be stolen. What I would like is to have a folder mirrored on a server somewhere which can be updated in real-time. There used to be a perfect solution: Bitcasa, but they shut down. Since then, I haven’t been able to find a replacement. So I thought I’d recreate the Bitcasa experience myself on a local server.

Unfortunately, that turned out to be easier said than done. I have a 4-TB local server at home and I installed OneCloud on it. I can easily access the thing while at home, but opening up the ports to the outside world was not possible due to the fact that I do not control the firewall. That is the way things stood until the few days back. That is when we suffered several break-ins at work and computers were stolen. This got me thinking: if my computer had been among those that were stolen, I would have lost invaluable data which I would not have been able to recover. There is nothing more sobering than having your worst-case scenario play out in front of you. I could not put this off any longer and needed a solution.

I can put the solution in two words: RSync and Automator. Automator is a cool tool found on Macs which makes it unbelievable easy to automate complex tasks with just a few clicks of a mouse. RSync is a program that comes preinstalled on many Unix systems. What I will describe monitors your ports for when you plug in your USB flash drive and then checks whether any changes have been made to a certain folder. If this is the case, the changes get synchronized with your USB flash drive.

Open Automator and start a new Folder Action

Under ‘Folder Action receives files and folders added to:’, select ‘Volumes’. This then will monitor the mounting of any external volume, be that an external HDD, a flash drive, or even a network drive. You can drag-and-drop a ‘Display Notification’ action and write whatever you want there (for example: ‘A new volume has been mounted’). Now comes the meat of the program:

Type in the following under ‘Run Shell Script’

ls -R /Volumes/INTENSO/ > a
ls -R /Users/User/Documents/Folder/ > b
DIFF=$(diff a b)
if [ "$DIFF" != "" ] && [ -e /Volumes/INTENSO ];
then
rsync -aE --delete /Users/User/Documents/Folders/ /Volumes/INTENSO/
else
exit
fi

You’ll want to change the ‘INTENSO’ to whatever name your USB flash drive has. I would suggest you change the default name of your drive to something unique so that you don’t get your data transferred to a stranger’s generic drive. What the script does is it checks whether your flash drive is mounted and whether any difference exists between its contents and those of your folder of interest (don’t forget to include the path to that folder instead of ‘/Users/User/Documents/Folder’) and if so, will run RSync to copy those files and folders to your drive. It will also delete the files and folders that were deleted from your source folder. If nothing has been changed between your source folder and your target folder, the script will not run.

You can now add another notification that tells you when the script has finished running and you’re done! Just save this Automator script and the next time you plug in your flash drive, you’ll get an instant back up of your most important data. You’re welcome!

Note: I am posting this after getting positive feedback from colleagues who were looking to do something similar. Unfortunately, as I said earlier, this only works on Macs due to its reliance on Automator. I will try to do something similar for Windows machines. If anyone has something that is easy to implement in Windows, I would be very interested to hear from you.