首页 文章

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

提问于
浏览
3

我必须为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文件中检查其名称来检查是否在服务器上添加了新文件夹.. **

every thing is working fine only problem now is .. suppose file was being downloaded an at that time there was some network failure.. how will i make sure that i have completely downloaded the file .... i tried to use md5sum and chksum but it was taking to long to compute on mounted folder. please help

这是我的剧本..

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

  • 1

    这里有一些评论和要求澄清,请参见下面的一个可能答案的中断 .

    (很好的工作更新你的问题 . )

    这些文件有多大?

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

    它还有助于获得这些文件的更多细节,即大小,MB,GB,TB,PB?以及创建它们的源,db-backup或??? .

    您是否关注最坏情况下的理论,主动探索,或者您是否遇到真正的问题,多长时间以及后果是什么?

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


    如果要传输的文件是源系统创建的数据文件,则一种技术是让源系统创建在发送主文件后发送的'flag'文件 .

    它可能包含类似的细节

    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运行 . 但是当你试图涵盖所有条件时它会变得非常复杂 . 有第三方工具可以管理文件下载,但我们从来没有预算购买它们,所以我不推荐任何 .

    我希望这有帮助 .


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

  • 2

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

  • 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
    

相关问题