Script to mount drives and run rsync

Discussion in 'Mac Programming' started by chazco, Sep 13, 2010.

  1. macrumors member

    Aug 22, 2010

    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:

    1. Connect to an AFP share (using password from keychain)
    2. Mount a sparseimage which is located on the share (also getting password from the keychain)
    3. If either of the above commands fail the script should abort with an error
    4. Execute the rsync command
    5. Unmount the sparseimage
    6. 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.
  2. thread starter macrumors member

    Aug 22, 2010
    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?
  3. macrumors 604

    Aug 9, 2009
    Post your code.
  4. thread starter macrumors member

    Aug 22, 2010
    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 ..."
  5. Moderator emeritus


    Aug 16, 2005
    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

Share This Page