7

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!

jasmines
  • 11,011
  • Looks like during boot the system was not able to resolve to myhost. You can add a IP <=> name mapping in /etc/hosts, see if it helps. Otherwise change myhost to FQDN or IP address. – Terry Wang Jul 17 '13 at 00:29
  • myhost is a dynamic host provided by a service such as no-ip.org or ddns.com. It's pretty unuseful to substitute it with an IP... – jasmines Jul 17 '13 at 05:05
  • Ok,is your system able to resolve to myhost? For example, do I ping and see if you can reach it. If not, you may have to do a IP - Hostname mapping in /etc/hosts to make it work. – Terry Wang Jul 19 '13 at 01:39
  • Please explain me why don't you use autofs? – Danatela Jul 19 '13 at 03:48
  • Sure, I can resolve myhost! For the mapping, as already said in the previous comment, is unuseful as it's dynamic host!

    Autofs is quite difficult to configure, and I'm not sure it would solve the issue, as I'm investigating and I found my wireless is available only after the login. Both fstab and autofs solutions are near to be abandoned...:(

    – jasmines Jul 19 '13 at 05:03
  • Does ist work when your network is up (sudo mount -a)? If not, can you try another server, e.g. ftp.mozilla.org? Also, I'm not sure if it's possible to add the /folder/-part in fstab. – Clausi Jul 19 '13 at 09:30
  • Have you tried noauto switch? you can put it in place of _netdev – bob Jul 20 '13 at 10:00
  • Six years after you posted this question I'm thinking cron@reboot to sleep 120 seconds, mount ftp on android phone, do some stuff and umount should work. Wondering if your experience over the years will confirm strategy? – WinEunuuchs2Unix Oct 06 '19 at 01:37

3 Answers3

11

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
dessert
  • 39,982
arve0
  • 276
  • Yes, it works. The folder is correctly mounted, but rsync isn't copying anything. – jasmines Jul 23 '13 at 11:47
  • Try adding verbose to rsync, to check whats going on. "rsync -av /media/ftp /local/folder" – arve0 Jul 23 '13 at 13:23
  • it's a simple "operation not permitted". Note that a cp works! – jasmines Jul 23 '13 at 19:52
  • Do you have write permission on the destination folder? ls -ld /local/folder. Are you able to browse and open files on mounted folder? What brand is your NAS? Most NAS today support more modern protocol than FTP. – arve0 Jul 24 '13 at 08:52
  • I wrote that cp works, so I have permissions to write! – jasmines Jul 24 '13 at 09:40
  • Sorry, didn't catch that. Is it the script or rsync that gives you the error message? – arve0 Jul 24 '13 at 09:44
  • rsync, I tried the single commands first. – jasmines Jul 24 '13 at 10:44
  • 1
    -a is equivalent to -rlptgoD, so it could be a number of things. For example rsync tries to preserve owner, which may fail if your not privileged to do so. Try running it as root: sudo rsync ... Without any more then, "operation not permitted", I'm not sure what it could be. Also note that rsync still copies the files in many cases where you get an error message. – arve0 Jul 24 '13 at 21:13
  • 2
    Good answer. But consider putting your password in the /root/.netrc file. https://linuxconfig.org/mount-remote-ftp-directory-host-locally-into-linux-filesystem – aaiezza May 25 '17 at 15:35
0

I'm using SFTP / sshfs for that purpose

echo password | sshfs username@serverIP:/ /mnt/mountpointfolder -p portnumber -o reconnect -o password_stdin

Not sure if there is a fstab way. If you execute a cron job doing this every x minutes, would be ok. If the folder is already mounted, you will just get "already connected" error. If you are disconnected, you will get another error and no connection will be made. But when you are connected again, you will connect in a minute... In case of cron job you can remove -o reconnect part.

Make the script file secure as it contain the password.

Dee
  • 1,976
0

You may try curlftpfs option

connect_timeout=N (seconds).

In your example (let us say 30 secs are enough),

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

Alternatively, you can remove from fstab and connect in Nautilus, then save it as bookmark and connect each time you startup.

bob
  • 1,198