如何在连接可用时使用fstab挂载FTP资源?

我想使用curlftpfs自动挂载一个FTP文件夹,放入fstab如:

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

通常情况下它不起作用,因为在启动期间,我的笔记本电脑无法使用网络(通常是wifi)。 我读到fstab中的_netdev选项应该确保仅在网络可用时才挂载,但是我收到消息:

 Error connecting to ftp: Couldn't resolve host myhost 

或者,我可以在登录后使用自动运行脚本装载资源,但我更喜欢fstab解决方案。

最终目标是使用crontab rsync将本地文件夹与ftp文件夹同步,所以如果您有其他建议,我将不胜感激!

由于您的目标是“将本地文件夹与带有crontab rsync的ftp文件夹同步”,我建议您编写一个安装FTP,rsync,卸载FTP的小脚本。 然后从crontab运行此脚本。

它应该是这样的:

 #!/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 

确保你在脚本上chmod + x。

crontab -e

 #mhd M wd 0 * * * * /usr/local/bin/backup-script 

此外,如果您确实希望始终挂载FTP文件夹,则可以创建一个安装/卸载驱动器的脚本。 如果还将其添加到fstab,则可以手动安装驱动器。

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 

将其添加到crontab(crontab -e):

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

还要注意你的rsync在下一次运行之前没有完成。 这可以自动完成(检查rsync是否正在运行),或者根据需要同步的数据量(rsync所需的时间量,最坏的情况)。

假设你没有为其他任何东西运行rsync,检查它是否正在运行可以这样做:

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

我正在使用SFTP / sshfs来达到这个目的

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

不确定是否有fstab方式。 如果你每x分钟执行一次cron作业,那就没问题。 如果文件夹已经安装,则只会出现“已连接”错误。 如果断开连接,则会出现另一个错误,并且不会建立连接。 但是当你再次连接时,你将在一分钟内连接…如果是cron作业,你可以删除-o reconnect部分。

使脚本文件安全,因为它包含密码。

你可以尝试curlftpfs选项

connect_timeout = N(秒)。

在你的例子中(让我们说30秒就够了),

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

或者,您可以从fstab中删除并在Nautilus中连接,然后将其另存为书签并在每次启动时连接。