There is a requirement in recent work that the files on a server need to be uploaded to the B server.
Originally want to use Java development, but think of Java development cycle long, to deal with such a small need not need to use Java, finally selected shell script, the relevant code is as follows:
1#!/bin/Bash2 3 functionError_exit {4Echo" $" 1>&25Exit16 }7 8#Local Directory (modifiable items)9Sourcepath=/opt/file One#remote server IP, port, directory (modifiable items) Atargetip=192.168.1.100 -targetport= A -Targetpath=/opt/file -#how often to scan a directory (modifiable items) -Sleeptime= - - + - while true + Do ANowtime= ' Date'+%y-%m-%d%h:%m:%s'` atEcho"$nowTime-start scan dir files ..." -curday= ' Date +%y%m%d ' -#Create a directory -Targetdatepath="$targetPath/$curday" -Ssh-p $targetPort $targetIp"[-D $targetDatePath]">/dev/null2>&1 - if[ $? !=0 ] in Then -Echo"$nowTime-auto Create remote dir $targetDatePath ..." toSsh-p $targetPort $targetIp"mkdir $targetDatePath"|| Error_exit"$nowTime-line Number: $LINENO, create remote dir failed, exit ..." + fi - the forFileinch$ (Find $sourcePath/$curday-name"*.xml") * Do $Scp-p $targetPort $file $targetIp: $targetDatePath | | Error_exit"$nowTime-line Number: $LINENO, SCP file failed, exit ..."Panax Notoginseng -RM-RF $file the Done +Echo"$nowTime-end scan dir files ..." A Sleep $sleepTime theDone
Specifies the local directory, which is a series of subdirectories with the date format as the directory name, scanning out all the XML files in the date directory;
Transfer to the remote server, the remote directory if there is no corresponding date directory is created, there is not created, and 5 minutes (configurable) to scan the directory;
File transfer between two Linux servers