Earthweb.com Practically Networked Home Earthweb developer.com HardwareCentral earthwebdeveloper CrossNodes Datamation
Welcome to PractiallyNetworked
Product Reviews

 • Routers
 • Hubs/Switches
 • Wireless Gateway
 • Wireless AP
 • Wireless NIC
 • Network Storage
 • Print Servers
 • Bluetooth Adapters
Troubleshooting
& Tutorials

 • Networking
 • Internet Sharing
 • Security
 • Backgrounders
 • Troubleshooting
    Guides

 • PracNet How To's
User Opinions
Practicallynetworked Glossary

 Find a Network Term  
 
Forums
About
Jobs
Home

  Most Popular Tutorials

• Microsoft Vista Home Networking Setup and Options
The most daunting part of upgrading to Windows Vista may be trying to figure out where in the layers of menus the networking and file-sharing options are hidden.

• Do It Yourself: Roll Your Own Network Cables
It may not be something you do everyday, but having the supplies and know-how to whip up a network cable on the spot can be very handy.

• Tips for Securing Your Home Router
Seemingly minor and easily overlooked settings can still have profound security implications. Here are some steps you can take to make sure your wired or wireless home router and by extension, your network is as secure as possible.

  Most Popular Reviews

• Microsoft Windows Home Server
If you have a home network, you'll welcome the easy file sharing, remote access and the image-based backup features of Windows Home Server.

• Iomega StorCenter Network Hard Drive
Iomega's fourth generation StorCenter Network Hard Drive brings many of the features found in higher-end storage devices down to an attractive price.

• MikroTik's The Dude
This free tool delivers many of the same capabilities that you'd find in pricey network monitoring tools. As long as you don't mind tinkering, The Dude is a decent network utility that should be worth the download.



Networking Notes: Build a Dropbox with SSH and Applescript

This week, we take a quick look at a handy feature in Mac OS X called Folder Actions, and discuss how we can use them in conjunction with a program called rsync to create a secure network dropbox.

Networking Notes

In the past two weeks we've dived in to setting up ssh on your Mac and configuring it to create secure tunnels for network services. This week, we're going to finish it all up with a quick look at a handy feature in OS X called "Folder Actions," and how we can use them in conjunction with a program called "rsync" to create a secure network dropbox.

Folder Actions allow you to assign a script or Automator workflow to a given folder and cause that script or workflow to trigger when something happens to the folder. Folder Actions can be set to trigger when the following actions occur:

  • the folder is opened
  • the window of the folder is closed
  • the window of the folder is moved or resized
  • items are placed into the attached folder
  • items are removed from the attached folder

What's That Term?
Not sure what a particular term means? Check out the searchable PracticallyNetworked Glossary.

What we're doing today is creating a folder action that lets us drop a file into a folder and have it securely copied to a server. The copying will take place in the background, and in many ways it will be as if the remote folder is sitting right on your local computer. Once we've set that up, I'll show how to change the Folder Action slightly so that files you remove from the local folder are also removed from the remote folder. By default, though, we're not going to do that because it's better to have folders that aren't perfectly synced than a critical remote folder that's been synced improperly.

The pieces you'll need to do this project are the following:

  1. A working ssh key, as explained two weeks ago. You have it all set up correctly if you can open the terminal and log in to the remote host without using a password, as discussed.

  2. Apple's Script Editor, which is available in the AppleScript folder of your Applications folder.

  3. The Folder Actions Setup program, which is also in the AppleScript folder of your Applications folder.

  4. Two folders you want to sync: one on the remote machine and one on the local machine.

Setting Up the Folders

To set up the folders, you'll need to log in to the remote server and create the folder you want to sync to. Please avoid using anything besides letters and numbers for the name to keep our script from getting confused. I named mine "Dropbox" for this script.

Next, create the folder you want to sync from locally. I'm calling mine "LocalDropbox" to keep things straight.

You should make a few duplicates of files that are small enough to upload quickly and put them in your LocalDropbox folder. Small text documents or images will work fine. Just remember to make duplicates instead of copying them directly, because you're going to want to experiment and it's best to do so with replaceable files, or at least clearly marked copies of irreplaceable files.

Set Up Your Tools

Next you need to run the Script Editor and the Folder Actions Setup program, which you can find in the folder named "AppleScript" in your Applications folder. The script is available in more readable HTML format, so visit that link and either copy and paste the script into Script Editor or click the link that reads "Open this script in a new Script Editor window," which will do what it says.

Looking at the Script

There's not a lot to a folder action script for something as simple as what we're doing. There are as many comments (lines that start with "--") and setup/teardown calls as their are lines of code that do something useful.

Since we want this script to do something when a file is added to the target folder, the first line tells the script to look for files being dropped into the folder, and assigns the folder to the variable "myLocalDropbox."

Because we're going to use a Unix command to do the sync, the second line of code translates the folder's name to its "POSIX path," which is another way of saying "the directory path the folder would have if we were trying to access it from the command line instead of Finder."

The third and fourth lines handle setting up our login name and remote server name. You should change these to your login name and the name of the server you're going to sync with.

The fifth line sets the name of the remote folder we're syncing to.

The sixth line creates a variable that makes the actual command we're going to run in the seventh line easier to read by concatenating several values into the string called "myRemote."

The seventh line runs the actual command, running the rsync command with the parameters needed to sync our local folder with our remote folder. We used the following arguments for the rsync command:

  • a - tells rsync to preserve key data about the files in terms of permissions and modification times, and recursively copy the folder. That means if you put a folder in your dropbox, the folder and all its contents will be copied over.
  • z - tells rsync to compress the files as it transfers them to speed things up.

The last line just tells the script it can end now.

Once you've set the myRemoteUser and myRemoteServer variables, you can click the "compile" button on the toolbar (it's got a hammer icon). The text should go from being gray to being color-coded similar to what you saw on the page with the script. You should save the script in your FolderActions directory, which is in your home directory under "Library:Scripts:Folder Action Scripts." If you don't have a Folder Action Scripts directory, just create it. I named my script "Dropbox."

Setting up the Folder Action

Once you've saved the script, it's time to attach it to your Dropbox folder.

Open the Folder Actions Setup app, make sure the "Enable Folder Options" box is checked, then add your local dropbox folder by clicking on the "+" button in the lower left. Choose your dropbox folder and click "Open." You'll get a list of possible folder actions, so choose the script you saved then click "Attach."

At this point, the script is attached to your dropbox folder as a Folder Action. You can try it out by dropping a copy of a file into it and checking your remote server after the script has a second or two to run. If the script doesn't work, you can always open up your terminal and try the rsync command manually, adding a "v" argument to get verbose output:

$ rsync -azv /Users/your user name/Dropbox/ yourremoteusername@yourserver:Dropbox/

If you get an error message, see what you can do to correct it, open the script back up in the script editor, and make any changes then try again.

Why rsync? An Advanced Option

For something as simple as this script, rsync might seem like overkill. scp would have handled the job, too, but if we want to create a remote folder that's a perfect mirror of our local folder, we need to also be able to handle removal of files. That's where the "--delete" argument comes in and rsync does something extra for us.

If you add "--delete" after the "-az" in the rsync command in your script, rsync will remove any files in the remote folder that aren't in the local folder.

You shouldn't add this argument unless your sure the script is working with your setup and putting the files in your remote dropbox folder. If it's putting them anywhere else, you stand to lose any files in that directory that aren't also in your local directory.

Oh, You Wanted Something Really Easy?

Back in early December, I wrote about FUSE and sshfs, which make it really simple to create virtual filesystems out of ssh-accessible servers. Since then, an enterprising developer at Google ported FUSE and sshfs to the Mac. You can find them on the MacFUSE project page. What you get with MacFuse is a graphical interface to sshfs that puts an icon on your desktop you can open like any folder.

Resources

  • Apple's Folder Actions page provides quite a bit more information about Folder Actions in general, and provides some sample AppleScript if you want to explore Folder Actions a little more.
  • rsync is the program that drives our script. You can learn more about it at that link.
  • CyberDuck is a simple, pleasant graphical ftp/sftp client that will let you take advantage of your ssh keys, too.
  • I wrote about Transmit last November. It's another fine ftp/sftp client if you don't mind paying. I haven't regretted my license.
Add to del.icio.us | DiggThis


For more help, don't forget to try one of our PracticallyNetworked Forums.



Earthwebnews.com Earthweb developer.com HardwareCentral earthwebdeveloper CrossNodes Datamation


Home | Networking | Backgrounders | Internet Sharing | Security | HowTo | Troubleshooting | Reviews | News | About | Jobs | Tools | Forums