Linux+shell acquiring and copying the latest data to remote devices
Our remote general copy of the data are created by the shared secret key, so that the copy of the words do not have to enter a password, specifically, I can refer to the previous article "Linux to realize the trust of SSH--http://gaowenlong.blog.51cto.com/451336/1856951 ”;
We will get the last modification time of the files in the Localbakdir directory through the script and remotely copy the latest modification time problem to Rmt_host
#!/bin/bashlocalbakdir=/oafs/weaver_backuprmtrestdir=/oafsrmt_host=192.168.6.38rmt_user=rootrmt_cmd= "$ (which  SSH) [email protected] $RMT _host "#function is_alive determine if the remote computer is communicating properly function Is_alive () {' which ping ' -c 4 $RMT _host >/dev/null 2>&1if [ "$?" -ne 0 ]; thenecho 2elseecho 0fi}function mktmpdir () {$RMT _cmd Mkdir -p /tmp/oafs}function rmvtmpdir () {$RMT _cmd rm -rf /tmp/oafs/*} Function get_last_targz () {echo $ (ls -lt $LOCALBAKDIR | awk {' Print $9 '} |grep -v ^$ |head -n 1)}function copy () {yum install -y openssh-clients >/dev/null 2>&1$RMT_CMD yum install -y openssh-clients >/dev/null 2>&1scp -q $LOCALBAKDIR/$ (get_last_targz) [email protected]$rmt_host:/tmp/oafs/}if [ "$ (is_alive)" -eq 0 ]; thenmktmpdircopyrmvtmpdirelseecho "$RMT _host can not be accessed via port 22, please check "fi
Finally, we use the script to obtain the file name under the Localbakdir directory to determine whether the latest file (the file name is named in the date format), the latest modification time problem is copied remotely to Rmt_host
#!/bin/bashlocalbakdir=/oafs/weaver_backuprmtrestdir=/oafsrmt_host=192.168.6.38rmt_user=rootrmt_cmd= "$ (which  SSH) [email protected] $RMT _host "function is_alive () {' which ping ' -c 4 $RMT _host >/dev/null 2>&1if [ "$?" -ne 0 ]; thenecho 2elseecho 0fi}function mktmpdir () {$RMT _cmd Mkdir -p /tmp/oafs}function rmvtmpdir () {$RMT _cmd rm -rf /tmp/oafs/*} Function get_last_targz () {echo $ (ls $LOCALBAKDIR | awk ' {match ($0,/.{ 4}-. {2}-. {2}/,a); B[a[0]]=$0;y=a[0]>y?a[0]:y}end{print b[y]} ')}function copy () {yum install -y openssh-clients >/dev/null 2>&1$rmt_cmd yum install -y openssh-clients >/dev/null 2>&1scp -q $LOCALBAKDIR/$ (get_last_targz) [email protected] $RMT _host:/tmp/oafs/}if [ "$ (is_alive) " -eq 0 ]; thenmktmpdircopyrmvtmpdirelseecho " $RMT _host can not be accessed via port 22, please check "fi
This article from "Gao Wenrong" blog, declined reprint!
Linux+shell acquiring and copying the latest data to remote devices