业务场景:B机器定时复制A机器生成的csv文件
/express/express/dta/20190311/tb_scan_20190311_0010.csv
1,取消scp传输密码限制
1,在B机器生成密钥对
>ssh-keygen -t rsa
<!--在/root/.ssh下生成id_rsa 和 id_rsa.pub 两个文件, -->
<!--其中公共密钥保存在 /root/.ssh/id_rsa.pub,私有密钥保存在/root/.ssh/id_rsa。-->
2,将生成的id_rsa.pub重命名,并发送到A机器
>cp id_rsa.pub id_rsa.pub.B
>scp -P 22022 /root/.ssh/id_rsa.pub.B root@A机器ip:/root/.ssh/
<!--22022是连接端口,默认是22,会提示输入A机器root的密码-->
3,在A机器创建authorized_keys文件,并追加id_rsa.pub.B
>touch authorized_keys
>cat id_rsa.pub.B >> authorized_keys
4,修改权限
>chmod 400 authorized_keys
2,在B机器测试复制A的文件到本地
scp -P 22022 root@10.19.205.58:/express/express/dta/20190311/tb_scan_20190311_0010.flg /u02/dab/scanfile/
<!--执行过程不再提示输入密码-->
3,定时执行复制
#!/bin/sh
# config
RANGE=299
DELAY=10
workpath=/express/express
srcfileRootPath=/express/express/dta
destfileRootPath=/express/express/dta
fileprefix=tb_scan
ercount=0
cd $workpath
#cd /express/express
while [ 1 ]
do
# 1 get time from file
timepre_t=`head -1 scan_update.txt`
timepre_s=$(date +%s -d "$timepre_t")
datepre=$(date +%Y%m%d -d "$timepre_t")
timepre=$(date +%Y%m%d_%H%M -d "$timepre_t")
# 2 get cur work time (need change range->5 min->3)
timecur_s=$(($timepre_s + $RANGE))
timecur_t=$(date '+%Y-%m-%d %H:%M:%S' -d "1970-01-01 UTC $timecur_s seconds");
timecur=$(date +%Y%m%d%H%M%S -d "$timecur_t")
# 3 get next time
timenxt_s=$(($timepre_s + $RANGE + 1))
timenxt_t=$(date '+%Y-%m-%d %H:%M:%S' -d "1970-01-01 UTC $timenxt_s seconds");
# 4 get now sys time
timenow_t=$(date '+%Y-%m-%d %H:%M:%S' -d '-1 min')
timenow_s=$(date +%s -d "$timenow_t")
# 5 if have task
if [ $timecur_s -lt $timenow_s ]; then
echo "now time : ${timenow_t}"
echo "execute time : ${timepre_t}"
srcflgpath=${srcfileRootPath}/${datepre}/${fileprefix}_${timepre}.flg
echo "flg file path : $srcflgpath"
srcfilepath=${srcfileRootPath}/${datepre}/${fileprefix}_${timepre}.csv
echo "src file path : $srcfilepath"
# create floder
destfiledir=${destfileRootPath}/${datepre}
if [ ! -d $destfiledir ]; then
mkdir -p $destfiledir
echo "create floder : ${destfiledir}"
fi
# line count
#srcLineCount=$(cat $srcfilepath | wc -l)
#echo "srcLineCount : $srcLineCount"
# 拷贝扫描文件标志
echo "scp ${srcflgpath} ${destfiledir}/"
scp -P 22022 root@**.**.**.**:$srcflgpath $destfiledir/
destflgpath=${destfiledir}/${fileprefix}_${timepre}.flg
if [ -f $destflgpath ];then
echo "scp ${srcfilepath} ${destfiledir}/"
scp -P 22022 root@**.**.**.**:$srcfilepath $destfiledir/
destcsvpath=${destfiledir}/${fileprefix}_${timepre}.csv
if [ -f $destcsvpath ];then
destLineCount=$(cat ${destfiledir}/${fileprefix}_${timepre}.csv | wc -l)
echo "destLineCount : $destLineCount"
# 5-2 save cur
echo "$timenxt_t" > scan_update.txt
rlog "$timenxt_t"
echo "!!!done <$(date)>done"
echo
else
ercount=$((ercount+1))
echo "!!!command error - sleep<$(date)>"
echo
sleep $DELAY
fi
else
ercount=$((ercount+1))
echo "!!!command error - sleep<$(date)>"
echo
sleep $DELAY
fi
sleep 1
else
# 4-8 delay
echo "!!!time is not - sleep<$(date)>"
echo
sleep $DELAY
fi
done