I have an networked drive connected to an Airport Extreme, so it's available whenever I'm on my home wifi network. I'd like to automatically move files from a specific folder to a specific folder on the networked drive when it's available. What's the best way to automate this?

link|improve this question

I messed around with Hazel and it works but will display a Growl error if it tries to move files when the external drive is unavailable. – fideli Nov 18 '10 at 7:05
feedback

1 Answer

up vote 2 down vote accepted

If you set launchd to monitor /Volumes as a WatchPath, it can run a script every time a drive is mounted. Simply make this script one that checks if your Airport Extreme drive is mounted then runs the copy if it is. For instance:

#!/bin/bash

if [ -e /Volumes/AirportExtremeDriveName ]
    rsync -aE /a/specific/folder/ /Volumes/AirportExtremeDriveName/another/specific/folder/
fi

I suggest rsync as it's smart enough to not copy files that are the same in both the source and the destination, which will speed things up no end. If you add the -delete argument, any files that no longer exist in the source will be deleted from the destination.

At no extra cost, here's the launchd script. Save the following in ~/Library/LaunchAgents/some.meaningful.name.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<dict>
    <key>Label</key>
    <string>some.meaningful.name</string>

    <key>LowPriorityIO</key>
    <true/>

    <key>ProgramArguments</key>
    <array>
        <string>/where/you/saved/your/script.sh</string>
    </array>

    <key>WatchPaths</key>
    <array>
        <string>/Volumes</string>
    </array>
</dict>
</plist>

Then, load the launchd job with:

launchctl load ~/Library/LaunchAgents/some.meaningful.name.plist

It'll be loaded automatically when you log in and unloaded when you log out.

link|improve this answer
Thanks for the detailed answer, this is great. – Evan Solomon Nov 20 '10 at 4:05
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.