bash中的shell脚本从ftp服务器下载文件

时间:2012-04-11 03:48:46

标签: linux bash shell ftp download

我必须为bash shell编写一个shell脚本来从ftp服务器传输文件 给出
ftp服务器 - fileserver@example.com
用户user1
密码pass1

现在在ftp服务器的/ dir1 / dir2我有以下表格的文件夹
0.7.1.70
0.7.1.71
0.7.1.72

我必须复制文件" file1.iso"从最新的文件夹,即本例中的0.7.1.72。 我还必须在复制时检查文件的完整性,即假设文件正在上传到服务器,那时如果我开始复制,在这种情况下复制将不会完成。

每隔4小时我就要这样做。这可以通过使它成为一个cron工作来完成。请帮忙

我做到了这一点  我将ftp服务器文件夹安装到我的本地机器上。检查文件是否已完全上传我是否每隔50秒检查一次大小5次,如果它是相同的话我会复制它,否则在4小时后运行脚本... 我保留了一个文本文件" foldernames.txt"其中包含我已复制所需文件的所有文件夹的名称..所以我通过在foldername.text文件中检查其名称来检查是否在服务器上添加了新文件夹。 **

每件事情都工作得很好现在只有问题..假设文件正在下载当时有一些网络故障..我将如何确保我已经完全下载了文件....我尝试使用md5sum和chksum但是在安装的文件夹上计算时间很长。请帮助

这是我的剧本..

#!/bin/bash
#
# changing the directory to source location 
echo " ########### " >> /tempdir/pvmscript/scriptlog.log
echo `date`>> /tempdir/pvmscript/scriptlog.log
echo " script is strting " >> /tempdir/pvmscript/scriptlog.log
cd /var/mountpt/pvm-vmware
#
# array to hold the name of last five folders of the source location
declare -a arr
i=0
for folder in `ls -1 | tail -5 `; do
arr[i]=$folder
#echo $folder
i=$((i+1))
done
echo " array initialised " >> /tempdir/pvmscript/scriptlog.log
#
#now for these 5 folders we will check if their name is present in the list of copied         
#  folder names
#
echo " checking for the folder name in list " >> /tempdir/pvmscript/scriptlog.log
## $(seq $((i-1)) -1 0 
for j in $(seq $((i-1)) -1 0  ) ; do
var3=${arr[$j]}
#var4=${var3//./}
echo " ----------------------------------------" >>  /tempdir/pvmscript/scriptlog.log
echo " the folder name is $var3" >> /tempdir/pvmscript/scriptlog.log
#
# checking if the folder name is present in the stored list of folder names or not
#
#
foldercheck=$(grep $var3 /tempdir/pvmscript/foldernames.txt | wc -l)
#
if test $foldercheck -eq 1
then 
echo " the folder $var3 is present in the list so will not copy it " >>  /tempdir/pvmscript/scriptlog.log
foldercheck=" "
continue
else
#
echo " folder $var3 is not present in the list so checking if it has the debug.iso file ">> /tempdir/pvmscript/scriptlog.log
#enter inside  the new folder in source
#
cd  /var/mountpt/pvm-vmware/$var3
#
# writing the names of content of folder to a temporary text file
#
ls -1 > /var/temporary.txt
#checking if the debug.iso is present in the given folder
var5=$(grep debug.iso /var/temporary.txt | wc -l)
var6=$(grep debug.iso //var/temporary.txt)
#
check1="true"
#
# if the file is present then checking if it is completely uploaded or not  
#
rm -f /var/temporary.txt
if test $var5 -eq 1 
then 
echo " it has the debug.iso checking if upload is complete   ">>/tempdir/pvmscript/scriptlog.log
#
# getting the size of the file we are checking if size of the file is constant or     changing    # after regular interval
#
var7=$(du -s ./$var6 |cut -f 1 -d '.')
#echo " size of the file is $var7"
sleep 50s
#
# checking for 5 times at a regular interval of 50 sec if size changing or not 
#
#
for x in 1 2 3 4 5 ;do
var8=$(du -s ./$var6 |cut -f 1 -d '.')
#
#if size is changing exit and check it after 4 hrs when the script will rerun
#echo " size of the file $x is $var7"
if test $var7 -ne $var8
then
check1="false"
echo " file is still in the prossess of being uploadig so exiting will check after 4 hr  " >> /tempdir/pvmscript/scriptlog.log
break
fi
sleep 50s
done
#
#if the size was constant copy the file to destination
#
if test $check1 = "true" 
then
echo " upload was complete so copying the debug.iso file  " >>  /tempdir/pvmscript/scriptlog.log
cp $var6 /tempdir/PVM_Builds/ 
echo " writing the folder name to the list of folders which we have copied " >>  /tempdir/pvmscript/scriptlog.log
echo $var3 >> /tempdir/pvmscript/foldernames.txt
echo " copying is complete  " >> /tempdir/pvmscript/scriptlog.log
fi
#else 
#echo $foldercheck >> /vmfs/volumes/Storage1/PVM_Builds/foldernames.txt
else
echo " it do not have the debug.iso file so leaving the directory "  >>/tempdir/pvmscript/scriptlog.log
echo $var3 >> /tempdir/pvmscript/foldernames.txt
echo 
fi
#rm -f /var/temporary.txt
fi
done

3 个答案:

答案 0 :(得分:2)

这里有一些评论和要求澄清,请参见下面的一个可能答案的休息时间。

(很好的更新你的问题。)

这些文件有多大?

这些文件是否可以控制其创建的开始时间(例如,数据库备份)。

这些文件还有一些细节,即大小,MB,GB,TB,PB还有帮助吗?以及创建它们的源,db-backup或???。

您是否关注最坏情况下的理论,主动探索,或者您是否遇到实际问题,影响的频率和后果是什么?

您的SLA是一个不现实/无法实现的管理梦想吗?如果是这样,那么您必须开始创建文档以显示当前系统将需要X量的额外资源(人员,硬件,编程等)来纠正系统中的缺陷。


如果要传输的文件是源系统创建的数据文件,一种技术是让源系统创建一个“标志”文件,在发送主文件后发送

它可能包含

等详细信息
  filename : TradeData_2012-04-13.dat
  recCount : 777777
  fileSize : 37604730291
  workOfDate: 2012-04-12
  md5sum    : ....

因此,现在您的系统等待发现标记文件已经发送,因为您正在为您收到的每个文件使用标准命名约定,并且您使用嵌入在文件中的stand日期戳。当文件到达时,您的脚本会计算每个相关的详细信息,并将它们与存储在标记文件中的值进行比较。

如果您无法安排此级别的详细信息,至少可以使用通用标记文件,每天每个文件,或每日批处理文件(在完成所有文件时发送),可以使用比较新文件的测试针对一组对您的特定情况有意义的测试,......以下部分内容:

  • 文件必须至少为X大
  • 文件必须至少为N条记录
  • 文件永远不会小于昨天的文件

然后你的辩护是“我们没有完全控制文件,但我们检查了它们的X,Y,Z,它通过了那些测试,这就是我们加载它们的原因。”


虽然rsync可能会很好,但我不知道如果提到某些情况,您可能会确定开始加载文件是安全的,因为rsync可能开始向文件添加更多数据。


阅读您的脚本,如果您无法从源中获取详细的标记文件,那么您就是在正确的轨道上。 Glenn Jackman的解决方案希望用更少的代码实现相同的目标。您可以将它放在scriptFile'getRemotedata.sh'或类似内容中,并将其置于while循环中,该循环仅在'getRemotedata.sh'成功退出时退出。我想我想要一些类型的通知,它已经花了3 * normalTime运行。但是当你试图涵盖所有条件时它会变得非常复杂。有第三方工具可以管理文件下载,但我们从来没有预算购买它们,所以我不推荐任何。

我希望这会有所帮助。


P.S。欢迎使用StackOverflow(SO)请记住阅读常见问题解答,http://tinyurl.com/2vycnvr,使用灰色三角形http://i.imgur.com/kygEP.png投票选出好的Q / A,并接受解决问题的答案,如果有的话,勾选复选标记http://i.imgur.com/uqJeW.png

答案 1 :(得分:1)

FTP协议不够健壮。它不处理原子性,并且在下载文件时无法知道文件是否仍在上传。如果您需要此功能,则需要使用rsync进行调查,以便下载和上传。

答案 2 :(得分:1)

#!/bin/sh
if mkdir /tmp/download_in_process 2>/dev/null; then
    echo "cannot start, download in process"
    exit 1
fi

latest=$(ftp hostname << END1 | tail -1
user user1 pass1
cd /dir1/dir2
ls
END1
)

ftp hostname << END2
user user1 pass1
cd /dir1/dir2/$latest
get file1.iso
END2

rmdir /tmp/download_in_process