How to mount FTP resources with fstab when connection is available?

I'd like to automount an FTP folder using curlftpfs putting in fstab a row like:

curlftpfs#user:pwd@myhost:port/folder/ /mnt/mymountfolder fuse allow_other,uid=1000,gid=1000,umask=0022,_netdev 0 0

Normally it wouldn't work, as during the boot the network (wifi usually) is not available for my laptop. I read that _netdev option in fstab should ensure the mounting only when the network is available, but I receive the message:

Error connecting to ftp: Couldn't resolve host myhost

Alternatively I could mount the resource with an autorun script after the login has been made, but I'd like much more the fstab solution.

The final goal is to syncronize a local folder with the ftp folder with a crontab rsync, so if you have other suggestions, I will be grateful!


Solution 1:

As your goal is "to syncronize a local folder with the ftp folder with a crontab rsync", I suggest you to write a small script that mounts the FTP, rsync, unmount FTP. Then run this script from crontab.

It should go something like this:

#!/bin/bash
curlftpfs user:pwd@myhost:port/folder/ /mnt/mymountfolder
#might need sleep 1 here
rsync -a /mnt/mymountfolder /local/folder
fusermount -uz /mnt/mymountfolder

Make sure you chmod +x on the script.

crontab -e

#m h d M wd
0 * * * * /usr/local/bin/backup-script

Also, if you really want the FTP folder mounted all the time, you could make a script that mounts/unmounts your drive. If you also add it to fstab, you could manually mount the drive.

fstab:

curlftpfs#user:pwd@myhost:port/folder/ /mnt/mymountfolder fuse noauto,user,uid=1000,gid=1000,umask=0022 0 0

network-mount.sh:

#!/bin/bash
folder=/media/ftp
# check if host is alive
ping=`/usr/bin/fping -q host.dyn.org`
if [ $? == 0 ]; then
  # check if folder is mounted
  mountpoint $folder > /dev/null
  if [ $? != 0 ]
    # mount, timeout in case something goes wrong
    then timeout 10s mount $folder
  fi
  else
  mountpoint $folder > /dev/null
  if [ $? = 0 ]
    #unmount lazy (network down)
    then umount -l $folder
  fi
fi

Add this to crontab (crontab -e):

* * * * * /usr/local/bin/network-mount.sh

Also watch out for your rsync not completing before the next is run. This could be done automatically(check if rsync running), or based upon how much data that need to be in sync(amount of time rsync takes, worst case scenario).

Assuming you don't run rsync for anything else, checking if it's running could be done like this:

pgrep rsync
if [ $? == 0 ]; then
  # rsync running
  exit
else
  # rsync not running
  #do stuff
fi