Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4: Trigger backups on connect with launchd UNIX
I have been searching for some sort of automated backup while I wait patiently for 10.5's Time Machine. While searching, I found this article on TUAW covering one method of automating the process.

I have re-written the backup scripts from that article to make them a little friendlier. Current features include:
  • Supports the backing up of multiple source directories
  • Uses rsync to cut down on un-necessary file transfers
  • Designed to allow for a minimum time between backups (default 24 hours) to prevent un-necessary system interruption.
I am planning on implementing several new features when time permits, including different backup profiles for different devices (eg: full backup to this drive, but only backup my documents and keychain when my iPod is plugged in...), and network rsync support...

Full code and instructions can be found here, and comments and suggestions welcome...

[robg adds: I haven't tested this one, but since backup is a personal hot topic of mine (not enough people have working (ideally multiple) backups), I thought it worth sharing.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[19,087 views]  

10.4: Trigger backups on connect with launchd | 9 comments | Create New Account
Click here to return to the '10.4: Trigger backups on connect with launchd' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use rsnapshot instead...
Authored by: dbs on Jan 10, '07 08:19:58AM

I'd suggest using rsnapshot instead of rsync.

Rsnapshot is not as space efficient as rsync, but it will keep previous changes snapshotted so you don't lose a file because you deleted it and then backed up. Rsnapshot is basically just a perl wrapper around rsync so it is quite good.

I currently use rsnapshot with an older version of rsync for HFS, but I'm not sure if the current rsync correctly supports all the extended attributes of HFS+. (Indeed I have good reason to believe it does not support ACLs.)



[ Reply to This | # ]
Resource fork?
Authored by: googoo on Jan 10, '07 09:23:14AM

Does rsnapshot preserve resource forks? You do this with rsync by using the -E option, but I think that is a Mac OS X-only enhancement. The backup script in the hint does use the -E option and will preserve resource forks.

-Mark



[ Reply to This | # ]
Possible to launch gui application with this script?
Authored by: nlemac on Jan 10, '07 09:22:22AM

Hi, I'm not a scripter, as you probably understand. But I was woundering is it possible to automaticly start a superduper! backup when connecting a external hd (and preferably unmount it afterwards)?

Thx! :)

---
//nlemac



[ Reply to This | # ]
10.4: Trigger backups on connect with launchd
Authored by: ptb@mac.com on Jan 10, '07 10:33:42AM

I've been using a similar script for a while now, except instead of rsync'ing to a hard drive, I rsync over the network to my home server (Mac mini). The problem is that when I'm at home, I need to use the Bonjour name (for this example I'm using "afp.local"), versus at work, I need to use it's public Internet hostname (for this example I'm using "afp.example.com"). The method I use to determine whether I'm at work or at home is to find the BSSID of the current AirPort base station and that gives me enough info.

I'm using launchd to execute a backup on a regular (timed) basis, instead of using a watch folder. Since I have a Mac on both ends of the line, I installed RsyncX on both systems and specify that binary so I can keep my resource forks (and other Mac specific bits).

I have also set launchd to run this as root, and I have set up ssh keys on both systems as root (in /var/root/.ssh/).


#!/bin/sh

username="steve"
hostname="mymac"

/bin/sleep 2

bssid=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | /usr/bin/awk '/BSSID/ { print $2 }'`

if [ $bssid = "00:60:1d:1e:26:39" ]
then
  location="Work"
  destuser="admin"
  desthost="afp.example.com"

elif [ $bssid = "00:0a:95:f1:e9:bc" ]
then
  location="Home"
  destuser="admin"
  desthost="afp.local"

fi

results1=`/usr/bin/sudo /usr/local/bin/rsync --archive --links --safe-links --delete --delete-excluded \
  --exclude=".DS_Store" --exclude=".Trash/" --exclude=".Trashes/" --exclude="Cache/" \
  --exclude="Caches/" --rsh=/usr/bin/ssh --rsync-path=/usr/local/bin/rsync --eahfs --stats \
  /Users/${username}/ ${destuser}@${desthost}:/Users/Shared/Backups/${hostname}/${username}/`

/usr/local/bin/growlnotify -n "Rsync Backup" -a "/Applications/Backup.app/" -m "Results of backup of \"${username}\""
/usr/local/bin/growlnotify -n "Rsync Backup" -a "/Applications/Backup.app/" -m "${results1}"
/usr/bin/logger -i -p daemon.notice Results of backup of "${username}" - "${results1}"

results2=`/usr/bin/sudo /usr/local/bin/rsync --archive --links --safe-links --delete --delete-excluded \
  --exclude=".DS_Store" --exclude=".Trash/" --exclude=".Trashes/" --exclude="Cache/" \
  --exclude="Caches/" --rsh=/usr/bin/ssh --rsync-path=/usr/local/bin/rsync --eahfs --stats \
  /Users/Shared/ ${destuser}@${desthost}:/Users/Shared/Backups/${hostname}/Shared/ --stats`

/usr/local/bin/growlnotify -n "Rsync Backup" -a "/Applications/Backup.app/" -m "Results of backup of \"Shared\""
/usr/local/bin/growlnotify -n "Rsync Backup" -a "/Applications/Backup.app/" -m "${results2}"
/usr/bin/logger -i -p daemon.notice Results of backup of "Shared" - "${results2}"

exit 0
Other possibly useful reading:

[ Reply to This | # ]
10.4: Trigger backups on connect with launchd
Authored by: macosx4me on Jan 11, '07 04:43:53AM
Please be very careful about using RsyncX. It is now quite dated and you should not really use it with 10.4 Rsync in 10.4 is at best a difficult proposition. I would suggest downloading and compiling 2.6.9 from source - samba.anu.edu.au and follow the instructions in patches/xattr.diff , compiling without ACL support (the provided ACL patch is for Linux ACLs).

[ Reply to This | # ]
Preference Pane alternative to launchd
Authored by: Eric3 on Jan 10, '07 07:20:52PM
I'm surprised no one has mentioned <a href="http://www.macupdate.com/info.php/id/15188">Do Something When</a> yet. It's a prefpane that lets you connect drives mounting/unmounting or apps launching with each other. I use it to run rsync scripts, wrapped as Automator actions, whenever my iPod or external hard drive mounts.

---
---
Eric3

[ Reply to This | # ]

Preference Pane alternative to launchd
Authored by: tinker on Jan 11, '07 07:55:41AM

I'll second the recommendation. I've fiddled with rsync for a while and, well, maybe I'm getting old and just don't like cobbling together my own scripts, or maybe I'm getting lazy and starting to prefer a GUI over the command line, but, DoSomethingWhen and SuperDuper! are an awfully nice combination.



[ Reply to This | # ]
Do Something When!?!
Authored by: mikerose on Jan 12, '07 07:29:34AM
Argh!!! I hate it when something simple comes along. :-)
There's also a Folder Action from Apple that does something very similar -- http://www.apple.com/applescript/folderactions/05.html

New post at TUAW with the details:

http://www.tuaw.com/2007/01/12/tuaw-tip-the-easy-way-to-backup-on-mount/

[ Reply to This | # ]
10.4: Trigger backups on connect with launchd
Authored by: macosx4me on Jan 11, '07 04:45:28AM
Please be very careful about using RsyncX. It is now quite dated and you should not really use it with 10.4 Rsync in 10.4 is at best a difficult proposition. I would suggest downloading and compiling 2.6.9 from source - samba.anu.edu.au and follow the instructions in patches/xattr.diff , compiling without ACL support (the provided ACL patch is for Linux ACLs).

[ Reply to This | # ]