danpros.com
Posted on
Server

Mount remote FTP server as folder on Linux and auto startup it

Author

Sometimes we need to mounting FTP server as folder on Linux and than auto mount it across reboot. This example is for CentOS 6.

# Installing the CurlFtpFS package (need EPEL repo):

yum install curlftpfs

A few CurlFtpFS command.

# $ftphost = The FTP host
# $mount = Mounted to folder
# $user = FTP username
# $pass = FTP password

# Mount the CurlFtpFS

curlftpfs $ftphost $mount -o user=$user:$pass,allow_other

# Unmount the CurlFtpFS

fusermount -u $mount

Auto mount CurlFtpFS

Creating the startup script:

vi /etc/init.d/mountftp

Copy and paste this. Change the FTP credentials and mounted to folder.

#!/bin/sh
#
# Run-level Startup script for curlftpfs
#
# chkconfig: 345 91 19
# description: Startup/Shutdown the curlftpfs

# FTP user, password, and host (you can specify the port also eg. ftp.example.com:2002)
ftpUser=user
ftpPass=password
ftpHost=ftp.example.com

# Mounted to folder
mPath="/var/mounted/ftp"

# Create the mounted to dir if doesn't exist
if [ ! -d $mPath ]; then
    mkdir -p $mPath
fi

case "$1" in
    start)
        curlftpfs $ftpHost $mPath -o user=$ftpUser:$ftpPass,allow_other
        ;;
    stop)
        fusermount -u $mPath        
        ;;
    reload|restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Usage: $0 start|stop|restart|reload"
        exit 1
esac
exit 0

Make the startup script persistent across reboot.

chkconfig mountftp on

Available command.

# Start

service mountftp start

# Restart and reload

service mountftp restart
service mountftp reload

# Stop

service mountftp stop