我们需要远程登录服务器,登陆的同时呢执行find命令获取一个时间段内的文件名,并将他们通过rsync下载到本地。
因为要跳过已经下载好的目录,scp命令就不够用了,这里用了rsync。
因为这里find命令只是获取文件名方便后续下载而已,所以设置find的参数-maxdepth 1来设置扫描深度,防止遍历子文件夹。
同时为了防止根目录也输出,我们设置find /data/abc/*这里的*号就是能够防止输出根目录,毕竟我们只是需要/data/abc/下的文件名,而不是/data/的文件名,这是很大的区别。
需要apt/yum安装expect
1#!/bin/bash 2if expect -c " 3spawn ssh finch@10.0.3.3 \"find /data/abc/* -maxdepth 1 -newermt '2020-09-04 13:00' -a -not -newermt '2020-09-07 23:59';\" 4expect { 5 \"*assword\" {set timeout 300; send \"12345678\r\";} 6 \"yes/no\" {send \"yes\r\"; exp_continue;} 7 } 8log_file /home/finch/bcd.txt 9expect eof" 10 11then 12 sed -i '1d' /home/finch/bcd.txt 13 ABC=`cat /home/finch/bcd.txt` 14 for i in $ABC 15 do 16 echo "start get files: $i" 17 expect -c " 18 spawn rsync -avz --progress finch@10.0.3.3:$i /data/tmp/ 19 expect { 20 \"*assword\" {set timeout 300; send \"12345678\r\";} 21 \"yes/no\" {send \"yes\r\"; exp_continue;} 22 } 23 expect eof" 24 done 25 echo "start clear the cache file named bcd.txt" 26 :>./bcd.txt 27fi