Saturday, May 8, 2010

Mount remote filesystem with sshfs in crontab

So, I use my remote fileserver for XBMC by mounting it via sshfs (live-stream 1080 video FTW!).

Sometimes the server has to be rebooted though, and I want the mount to be automatically re-mounted when the system is back up again, and I use crontab for that. One problem still exist, and that is that the key I use is passphrase protected, something that is good, and since I use ssh-agent it's not really a problem:

*/5 * * * * test -d /mounts/remote.sys/video || . /home/myuser/.ssh/environment ; /usr/bin/sshfs remote.sys:/data /mounts/remote.sys/ -o Cipher=blowfish -o transform_symlinks -o follow_symlinks -o allow_other

So, what does it do? test -d will test if "/mounts/remote.sys/video" is a directory (the -d). If not, "||" means "not equal", so if it isn't a directory, then do the command following "||". Here I first source the environment variables from ssh-agent, then I just run sshfs to mount the remote system.

Very nice indeed!