使用ftp获取服务器上的整个目录

ftp命令只可以使用get命令获取到普通文件,不能获取目录。

写了个脚本getDir.sh,获取服务器上某个目录下所有文件。

getDir.sh

#! /bin/bash

# filename : getDir.sh
# Usage : $0 [remoteIp] [remoteDir] [localDir] [logfile] [ftpuser] [passwd] 
# example : ./getDir.sh 192.168.72.128 /home/wwb/Documents/source /home/wwb/Documents/tmp/source /home/wwb/Documents/logs/getDir.log ftpuser passwd 

remoteIp="192.168.72.128"
remoteDirRoot="/home/wwb/Documents/source"
localDirRoot="/home/wwb/Documents/tmp/source"
logfile="/home/wwb/Documents/logs/getDir.log"
ftpuser="ftpuser"
passwd="passwd"

if [ "" == $1 ]
then
    remoteIp=$1
fi
if [ "" == $2 ]
then
    remoteDirRoot=$2
fi
if [ "" == $3 ]
then
    localDirRoot=$3
fi
if [ "" == $4 ]
then
    logfile=$4
fi

if [ "" == $5 ]
then
    ftpuser=$5
fi
if [ "" == $6 ]
then
    passwd=$6
fi

logdir=`dirname ${logfile}`
if [ ! -d ${logdir} ]
then
    rm ${logdir} 1>/dev/null 2>&1
    mkdir -p ${logdir}
fi

getDir()
{
    remoteDir=$1
    localDir=$2
    echo ${remoteDir} ${localDir}
    if [ ! -d ${localDir} ]
    then
        rm ${localDir} 1>/dev/null 2>&1
        mkdir -p ${localDir}
    fi
    cd ${localDir}
    ftp -i -n "${remoteIp}" <<EOF | awk '/^d/{print remoteDir"/"$NF" "localDir"/"$NF}' remoteDir="${remoteDir}" localDir="${localDir}" > __dirname_$$.log
    user ${ftpuser} ${passwd}
    cd ${remoteDir}
    lcd ${localDir}
    ls -l
    bin
    mget *
    bye
EOF
    while read line
    do
        if [ 0 -ne ${#line} ]
        then
            getDir ${line}
        fi
    done < __dirname_$$.log
}

getDir ${remoteDirRoot} ${localDirRoot} 2>&1 >> ${logfile}

find ${localDirRoot} -name __dirname_$$.log | xargs rm -f


你可能感兴趣的:(使用ftp获取服务器上的整个目录)