View Full Version : Script to mount drives and run rsync

Sep 13, 2010, 07:20 AM

I've finally got a working filevault+NAS backup solution using rsync. I'm looking to make the process more automatic with a script. I'd like the script to do the following:

Connect to an AFP share (using password from keychain)
Mount a sparseimage which is located on the share (also getting password from the keychain)
If either of the above commands fail the script should abort with an error
Execute the rsync command
Unmount the sparseimage
Disconnect from the AFP share

I have some experience with batch files and Unix scripts, but OSX is new to me and I'm not sure where to begin. There are also other options which may be more suitable... (Automator? Applescript?).

Any suggestions on the best way to do this?

Thanks in advance.

Sep 17, 2010, 07:21 AM
I've had some luck with this using Applescript and the "mount volume" command. I've not had any luck getting the sparseimage to mount though...

Any ideas?

Sep 17, 2010, 12:40 PM
... Any ideas?

Post your code.

Sep 18, 2010, 03:51 AM
Here it is :)

# Attempt to connect to network share
mount volume "afp://username@MyBookWorld.local/backupshare"
on error
# Displays it's own error on failure, so just exit
end try

# Mount sparseimage here

# Run rsync
do shell script "rsync ..."

Sep 18, 2010, 06:36 AM
I have a bash script for doing this type of thing. For mounting the sparseimage the command I use is,
# Attach
hdiutil attach /path/to/sparse.dmg >/dev/null
# Detach
hdiutil detach /Volumes/mountedDrive/ >/dev/null