Filed under: OS, Terminal Tips
Trigger backups on connect with launchd
The fine folks at macresearch.org have put out an interesting tutorial that walks through setting up launchd and rsync to do automatic backups whenever a specific external drive is mounted. Loyal readers may note that this is similar to the core feature of Martian Lifeboat (which should not be confused with Mojave Shade's Lifeboat, as mentioned in several comments). The script, however, is free; Martian Lifeboat is not.Although the currently published script leaves out the crucial -E flag to force rsync to copy resource forks (credit to Josh Wisenbaker of afp548.com for spotting the silent E), it's still a good primer on the power of launchd. For a simple way to create your own launchd configuration files, be sure to check out the powerful, open-source and free Lingon utility. If you try this out, let us know how it works for you.
[via macenterprise mailing list]

![TUAW [Cafepress]](http://www.blogsmithmedia.com/www.tuaw.com/media/tuaw-cafepress-promo.png)


Reader Comments (Page 1 of 1)
Nik said 10:08AM on 12-26-2006
You can do the same thing with a folder action AppleScript attached to the /Volumes/ directory, or by using Proxi in the same fashion (folder watcher trigger pointing at /Volumes/).
Reply
Blurred Vision said 8:07AM on 12-29-2006
I have done some testing, and made some changes, and here is what I have found so far:
The script will run EVERY time ANY drive image is mounted or unmounted (including iPods). If you are like me, and your backup drive is connected for hours at a time, it means you will be needlessly backing up again and again. I think I will need to add some "time since last successful backup" checks so it will only run once every 24 hours.
The script also only handled 1 backuo source dir. If yo uare like me, this is not enough. This is fixed in teh script below.
I have converted the tcsh shell script to bashand added a few things, like logging, and multiple source dirs:
-- begin
#!/bin/bash
folderToBackup="/Users/ /Applications"
backupVolume="/Volumes/MyDrive"
backupTo="${backupVolume}/backup"
sleepFor=60
echo -n "[*]-- Autobackup envoked at `date`" | logger
echo -n "[*]-- Sleeping for ${sleepFor} seconds..." | logger
sleep ${sleepFor}
if [ ! -e ${backupVolume} ]
then
echo -n "[*]-- BackupVolume NOT connected - Exiting" | logger
exit 0
else
echo -n "[*]-- BackupVolume Connected - Continuing" | logger
fi
# Create the folder to back up the data to (defined above)
if [ ! -e ${backupTo} ]
then
echo -n "[*]-- Backupdir does not exist... Creating...)" | logger
mkdir -p ${backupTo}
else
echo -n "[*]-- Backupdir exists! - Continuing" | logger
fi
# Copy the files over. Here we are using rsync.
for i in ${folderToBackup}
do
echo -n "[*]-- Starting Rsync of ${i} to ${backupTo}" | logger
rsync -aqE --delete ${i} ${backupTo}
echo -n "[*]-- rsync of ${i} to ${backupTo} complete..."| logger
done
rsync -aqE --delete $folderToBackup $backupTo
# Optionally, once the rsync is done, unmount the drive.
#hdiutil detach $backupVolume
exit 0
-- end
Reply
Blurred Vision said 9:53PM on 12-30-2006
Have made some updates: backup script now supports multiple source dirs, a minimum interval between backups, and logging (so you can see what has been happening)
available for download at http://www.users.on.net/~aidanandjen/BlurredVisions/files/edc01be85bcd4f5ae1789fd9c290a20e-2.html
Reply
David said 11:45AM on 1-10-2007
Try rsnapshot instead of rsync. It will automatically keep snapshots which are just hard links to the original files unless the file has changed, in which case it will copy the file. It requires somewhat more configuration than rsync, but is much more useful as a backup.
Reply