服务器之间文件定时复制

业务场景: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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MD5校验码:f4f9ea3f7bcc3375192be61dc110cb58 1、本软件是定时自动备份软件。 2、备份任务自动拷贝文件文件大小或修改时间变化的文件 3、MyCopy.exe是配置界面,该文件生成配置文件mycopy.ini,并能显示系统运行状态。 4、MyCopyTask.exe是执行拷贝任务的程序,它根据mycopy.ini配置的信息定时进行拷贝,在休眠状态下每5秒钟检查一次是否到达任务指定的拷贝时间。拷贝过程中出现错误,记录log.ini文件,但不会终止拷贝进程。 5、MyCopy.exe配置界面里有“启动”按钮把运行状态改为“运行”,并启动MyCopyTask.exe程序;“终止”按钮把运行状态改为“终止”,MyCopyTask.exe检测到系统状态时为“终止”则自动退出。“退出”按钮退出配置界面,但不会终止MyCopyTask.exe程序。“暂停”也会推出MyCopyTask.exe程序,但下次启动时,从上次暂停的任务开始继续拷贝。 6、MyCopyTask.exe运行时托盘上会显示图标。 7、要实现拷贝任务的自动启动,可以把MyCopyTask.exe加入到windows系统的“启动”菜单中,但配置文件中的运行状态一定是“启动”,否则程序会自动退出。 8、程序拷贝文件时意外终止,可以坚持mycopy.ini文件中的[系统状态]是否有“半个文件”,如果有说明“运行信息”中包含的文件没有拷贝完整。 9、如果因为某种原因错过了任务执行时机的话,拷贝程序会在启动后补回错过的拷贝任务。 10、任务名和文件路径中不能出现","(半角逗号) 11、标准版只记录log.ini文件,健康提示版当任务执行完之后会弹出提示信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

占星安啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值