miriup.de

...because open source matters

  • Increase font size
  • Default font size
  • Decrease font size

Starting your backup when the disk is plugged on MacOSX

Scrivi e-mail Print
There are no translations available.

Introduction

I'm using rsync to backup my data to an external USB disk. I keep forgetting to do it, so automation is what I need. This procedure doesn't just apply to rsnapshot, it should work with any backup method that can be initiated from the command line.

The basic idea

Whenever a disk is plugged, Finder will automatically mount it to /Volumes. Monitoring the /Volumes directory for changes is enough to detect the change and then we can look whether the backup volume is actually present.

Configuration

We basically need to create two things: a launchd config file and a shell script.

The launchd configuration file is in the plist format. It's most conveniently created with the Property List Editor, but an ordinary text editor such as vim or TextEdit is fine, too. The format of the configuration file is documented in the launchd.plist man page that can be read using the following command:

man launchd.plist

The plist file needs to go into Library/LaunchAgents (in your home directory) and it needs to have a .plist extension. The rest of its name is arbitrary. On my system the plist file is called Library/LaunchAgents/volume-connected.plist and has the following configuration options defined:

Label volume-connected
LowPriorityIO true
Program /Users/user/bin/volume-connected
ServiceDescription Do things upon mounting volumes
StandardErrorPath /tmp/volume-connected.err
StandardOutPath /tmp/volume-connected.out
WatchPaths /Volumes
The Program key identifies the location of the second file: the shell script that is executed whenever a volume is mounted. The job of the shell script is to determine whether the right volume has been mounted and to execute the backup program.
As I mentioned before: my backup program is rsnapshot. I do monthly backups only and have a separate sync step. My shell script looks like this:
#!/bin/sh
# I'm executed on MacOSX when a new volume is conncted.

BACKUP_ROOT=/Volumes/USB/rsnapshot/.sync

if test -d ${BACKUP_ROOT}
then
        if [ "`ls -dl --time-style='+%m' ${BACKUP_ROOT} | sed 's!^.* \([0-9]\{2\}\) /.*$!\1!'`" != "`date '+%m'`" ]
        then
                /where/bin/rsnapshot -v monthly
        fi
        /where/bin/rsnapshot -v sync
fi
The BACKUP_ROOT is where my backup is synced to. The first line with test -d determines whether the right volume has been mounted by checking for the existence of the backup directory. The fancy line with the time-style determines whether the number for the current month is different from the number of the month for the .sync directory. If it is, we do the monthly backup rotation. Otherwise we just synchronize all the files with the backup. The -v flag stands for verbose and has the effect that rsnapshot is logging loads of information to standard output which is redirected to a file in /tmp using the plist file above.
Attachments:
Download this file (volume-connected.plist.txt)/tmp/volume-connected.plist[launchd configuation file]0 Kb22/09/2010 00:38
Download this file (volume-connected.txt)/tmp/volume-connected[Shell script to be executed on volume change]0 Kb22/09/2010 00:39
LAST_UPDATED2