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

标签 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

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

我必须每 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

最佳答案

这里有一些评论和要求澄清,请参阅下面的中断部分以获得一个可能的答案。

(很高兴更新您的问题。)

这些文件有多大?

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

了解这些文件的更多详细信息也会有所帮助,例如大小、MB、GB、TB、PB?以及创建它们的来源、db-backup 或 ???。

您的担忧是理论上的、对最坏情况的主动探索,还是如果您遇到真正的问题,频率有多高,后果是什么?

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


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

它可能包含类似的细节

  filename : TradeData_2012-04-13.dat
  recCount : 777777
  fileSize : 37604730291
  workOfDate: 2012-04-12
  md5sum    : ....

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

如果您无法安排这种级别的详细信息,至少可以在通用标志文件、每天每个文件或每天一批文件(在所有文件完成后发送)之后进行比较新文件的测试针对一组对您的特定情况有意义的测试,......以下一些:

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

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


虽然 rsync 可能不错,但我不明白,在提到的某些情况下,您如何确定开始加载文件是安全的,因为 rsync 可能会开始向文件中添加更多数据。


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

希望对您有所帮助。


附言欢迎使用 StackOverflow (S.O.) 请记得阅读常见问题解答,http://tinyurl.com/2vycnvr , 使用灰色三角形为好的 Q/A 投票,http://i.imgur.com/kygEP.png , 并接受可以解决您的问题的答案(如果有的话),方法是按复选标记 http://i.imgur.com/uqJeW.png

关于linux - bash中的shell脚本从ftp服务器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10099540/

相关文章:

bash - 如何使用Bash自动执行两个OS.Stdin输入

linux - 在notepad++中连续搜索两行

Linux - 将制表符分隔转换为管道分隔并删除前导和尾随空格

linux - Bash 记住两个脚本出现之间的值

linux - 通过交换机通过以太网将嵌入式系统连接到主机

c++ - C++ 中的正则表达式如何搜索有效的 Linux 设备节点?

c - 套接字文件描述符中的可读/可写是什么意思?为什么普通文件不会为此烦恼?

bash - 使用 unix grep 指定时间范围

shell - 告诉 Impala 忽略错误并继续

linux - 构建将内容附加到特定文件的脚本的最简单方法,用于此类任务的 Windows 或 Linux?