首页 > 代码库 > linux下自动ftp到服务器对比拷贝指定目录下文件

linux下自动ftp到服务器对比拷贝指定目录下文件

#!/bin/bash
process=8
PWD=`pwd`
DAT=`date`
ftpip="192.168.1.1"
user="ftp_username"
password="ftp_password"
local_dir="/home/"
getlist()
{
ftp -n $ftpip <<EOF |awk ‘{print $9}‘|sed ‘/^$/d‘ >$PWD/list
user $user $password
cd file/
dir
bye
EOF
}
copy()
{
if [ -f $PWD/list ]
then
unset i
for i in `cat $PWD/list|sed -n ‘‘$star‘,‘$end‘p‘`
do
if [ -f $local_dir/file/$i ]
then
echo "$i file exist..."
else
wget -c -t 0 -T 1800 -r -nH -nv -P $local_dir  ftp://"$user":"$password"@$ftpip/file/"$i"
fi
done
else
echo "list is not exist"
fi
}
#judge ip
ping $ftpip -c 1 >>$PWD/COPY.log
res=`echo $?`
if [ x"$res" != x"0" ]
then
printf "\033[40;31;1mThe address cannot connect!\n\033[0m" |tee -a $PWD/COPY.log
exit 1
else
echo "IP is ok" >>$PWD/COPY.log
fi
getlist
total=`cat $PWD/list |wc -l`
perno=`expr $total / $process`
leftno=`expr $total % $process`
#judge process
if [ "$leftno" -ge "$perno" ]
then
printf "\033[40;31;1mToo many process!\n\033[0m" |tee -a $PWD/COPY.log
exit 1
else
echo "PROCESS NO. is ok!" >>$PWD/COPY.log
fi
###
step=`expr $process + 1`
star=1
end=$perno
echo $DAT >>$PWD/COPY.log
for ((a=1;a<=$step;a++))
do
echo "ROCESS:$a FILES:$star $end">>$PWD/COPY.log
copy "" >>$PWD/COPY.log  2>&1 &
star=`expr $star + $perno`
end=`expr $end + $perno`
done
printf "\033[40;32;1mNow is copying in backgroud,you can use ‘ps -elf‘ to check...\n\033[0m"

简单的脚本,实现不同服务器之间指定目录下的文件对比,相同认为该文件存在,不存在的拷到本地制定目录。进程数、目录都可以根据自己需求修改。

本文出自 “技术交流” 博客,请务必保留此出处http://linuxsuperman.blog.51cto.com/2219295/1412622