最近应同事要求,需要一个shell脚本来完成ftp对文件夹的传送。我们都知道,在字符界面的Linux平台下,FTP工具只支持单个文件的传送,但有的时候我们确实是需要传整个文件夹的,此时有很多同仁可能都想到了,要实现这个远程传送文件夹的目的其实很多途径嘛,没错!当时我也给出并逐个分析了一下几种不同途径:
1. 很简单的方法,就是在源服务器上将文件夹压缩打包,然后通过ftp传送。当然这个办法的限制也显而易见,那就是对于SIZE太大(例如几个G什么的)的文件夹来说这个步骤是很费时费资源的,打包费时,一次性传送也费时。
2.用远程拷贝命令scp,rcp。对于这个方式由于传送过程是要写到脚本里的,这里就免不了需要密码验证,但是考虑到环境的安全,不能随意的设置主机间的信任,实现无密码传输,所以该方式也被那位同事拒绝掉
3.最后没办法只能通过FTP的传输了,当然要通过ftp传输文件夹就需要通过算法来实现了,我的实现思路如下,下面是我写的一个FTP递归区文件夹的脚本,供大家参考和指正
1.在源服务器端通过递归得到整个文件夹的结构并写入文件/tmp/Recu_files_list:S_file_collect.ksh
#**************************************************************
#
#Shell excute: <S_file_collect.ksh parameter>
#the paremeter means the directory that you want to be transfered
#Description: Get the directory structure by recursive.
#
#****************************************************************
#! /bin/ksh
transfered_dir=$1
cd $transfered_dir
if [[ -f /tmp/Recu_files_list ]]
then
rm -f /tmp/Recu_files_list 2> /dev/null
fi
touch /tmp/Recu_files_list
echo "$(pwd):" > /tmp/Recu_files_list
function myfun
{
for i in $(ls)
do
if [[ -d $i ]]
then
echo "$(pwd)/$i:" >> /tmp/Recu_files_list
If_Dir_Empt=$(ls -l $i | wc -l)
if [[ $If_Dir_Empt -gt 0 ]]
then
cd $i
myfun
cd ..
fi
else
echo $(pwd)/$i >> /tmp/Recu_files_list
fi
done
}
myfun
2. 将文件/tmp/Recu_files_list从源服务器传送到目标服务器,并且在目标服务器上通过脚本去获取整个文件夹内容:Get_Ftp_Files.ksh
#*************************************************
#Description: This is script will complete below tasks:
# 1. Get the directories and files list (/tmp/Recu_files_list)
# that generated at Souruce server
# 2.create all the directory structure and get the files by ftp
#Implement: ksh Get_Ftp_Files.ksh
#Output: /tmp/ftp_run_cmds.bak
#*************************************************
#!/bin/ksh
echo "********** Begin **********"
#************************************
#Description: Before run this ftp command, it is neccessary to
#run the S_file_collect.ksh on Source server, it will
#generated a file named /tmp/Recu_files_list.
#************************************
cat << EOF >/tmp/result.sh
ftp -n 192.168.139.130 <<EOF
user root 1qaz2wsx3edc
cd /tmp
lcd /tmp
mget Recu_files_list
bye
EOF
EOF
chmod +x /tmp/result.sh
ksh /tmp/result.sh >/dev/null 2>&1
rm -rf /tmp/result.sh
echo "1. Create the directory Structure ....."
#*********************************************
#Description: filter the directory from file #/tmp/Recu_files_list to created the dir strcture
#*********************************************
sed -n '/:$/p' /tmp/Recu_files_list | sed -n 's/://p' > /tmp/dirs
awk '{system("mkdir -p " $1);}' /tmp/dirs
rm -rf /tmp/dirs
if [[ -f /tmp/runftp.sh ]]
then
rm -rf /tmp/runftp.sh > /dev/null 2>&1
#echo "the last runftp file has been deleted"
fi
touch /tmp/runftp.sh
echo "2.Created the ftp script to get the files...."
#**************************************************
#Description: Generate the FTP script and run it
#***************************************************
awk 'BEGIN{print "ftp -n 192.168.139.130 <<EOF" >> "/tmp/runftp.sh";
print "user root 1qaz2wsx3edc" >> "/tmp/runftp.sh";
print "prompt off">>"/tmp/runftp.sh";}
{print "cd "$1>>"/tmp/runftp.sh";
print "lcd "$1>>"/tmp/runftp.sh";
print "mget *">>"/tmp/runftp.sh";}
END{print "bye">>"/tmp/runftp.sh";
print "EOF">>"/tmp/runftp.sh";}' /tmp/dirs
chmod +x /tmp/runftp.sh
echo "3.Start FTP to get all the files......"
ksh /tmp/runftp.sh >/dev/null 2>&1
cat /tmp/runftp.sh >> /tmp/ftp_run_cmds.bak
rm -rf /tmp/runftp.sh >/dev/null 2>&1
echo "********** END **********"
如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!
赞1
添加新评论3 条评论
2012-03-16 15:26
ksh Get_Ftp_Files.ksh
********** Begin **********
1. Create the directory Structure .....
2.Created the ftp script to get the files....
awk: 0602-533 Cannot find or open file /tmp/dirs.
The source line number is 3.
3.Start FTP to get all the files......
********** END **********
好像找不到/tmp/dirs了
2012-03-07 09:35
2012-03-06 23:50