November 11, 2012

backing up to a USB drive when plugged in

For some reason, I cannot use Time Machine. I have it set up and going but if I am for example, as I recently was, looking for a lost picture in iPhoto, how do you find it with Time Machine ?

So I set out to complement Time Machine with a "simpler" back up. Not simpler to set up, granted, but simpler in the sense that it just copies files over to another disk. That's it. Just somewhere else.

So ... I stumbled upon this article describing out to use launchd to trigger a script when a "watched" folder's state changes. Their solution works great, here's how I implemented mine: same recipe, you will want a plist to kick the backup script and the backup script itself, using rsync.

I used ~/Documents/perso/com.arnaudlacour.osx.backup.plist and ~/Documents/perso/backup
That's because I'm going to use that to back up ~/Documents/perso to a USB disk called OPALE.

here's the plist:


 
         Label
         com.arnaudlacour.osx.backup
         LowPriorityIO
         
         Program
         /Users/arno/Documents/perso/backup
         ProgramArguments
         
   backup
                 OPALE
         
         WatchPaths
         
          /Volumes
         
 

And here is the backup script:

#!/bin/bash -llog=/var/log/com.arnaudlacour.osx.backupecho "`date` - starting back up to ${1}" >> $logfrom=~/Documents/persoto=/Volumes/${1}if ! test -z "$1" && test -e "${to}" ; then  # WAIT some time until the disk is ready  sleep 10
  # Start rsync'ing to the target  rsync -ahmuv --progress ${from} ${to} 2>&1 >> ${log}else echo "target [${to}] not valid" > ${log}
fi echo "`date` - done" >> ${log}
then it is a simple matter of registering the plist in launchd
launchctl load ~/Documents/perso/com/arnaudlacour.osx.backup

When I then plug my USB drive and OSX mounts it, the script kicks in:

Sun Nov 11 16:38:18 MST 2012 - starting back up to OPALE
rsync -ahmuv --delete --progress /Users/arno/Documents/perso /Volumes/OPALE
building file list ... 
37419 files to consider

sent 670.76K bytes  received 20 bytes  268.31K bytes/sec
total size is 24.61G  speedup is 36691.20
Sun Nov 11 16:38:31 MST 2012 - done

Incremental backup upon USB Drive plug-in

EDIT: it's worth noting that rsync will prefer the source and destination partitions to be of the same type. I tried from HFS+ to FAT and it ended up rsync'ing from scratch every time without doing the incremental backup because FAT does not preserve the HFS metadata. Just sayin'

1 comment:

  1. GSCopy Pro v6.0 (RoboCopy Alternative) with Open File Agent
    GSCopyPro is a single command-line tool (CLI) that can copy, replicate and move files from one folder to another. This folder can be on the same machine/ server or another server elsewhere. What makes GSCopyPro stand out from other competitors is the fact it works on 32-bit as well as 64-bit systems and has no restrictions. It can easily be scheduled to run as a scheduled task and fully automated. GSCopyPro also comes with an open file agent which can copy files that are locked/ opened by other processes. This feature is supported in all windows vSCersions from widows XP/ 2003 and later.
    Go To:>> http://www.gurusquad.com/GOPYPRO

    ReplyDelete