我必须为 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 秒检查一次大小,如果相同,我将复制它,否则在 4 小时后运行脚本... .txt”,其中包含我从中复制所需文件的所有文件夹的名称..所以我通过检查文件夹名称.文本文件中的名称来检查是否在服务器上添加了新文件夹..**
一切都很好,现在唯一的问题是..假设当时正在下载文件,并且当时有一些网络故障..我将如何确保我已经完全下载了文件..我尝试使用 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