文件傳輸腳本(expect+rsync)

完成此腳本,主要是爲了將本地服務器上的文件同步到騰訊雲跳板機,然後再中轉上傳到騰訊雲服務器。當然這也只是初步的方案。


# vim /data/scripts/put_file.exp

#!/usr/bin/expect
########################################################################
# push local server’s file to remote server
#
# code by rocketzhang (2015.04.20)
########################################################################

if { $argc!=6 }  {
    send_user "Usage: push_file.exp localfile remoteip remoteport remoteuser remotepwd remotedir\n\n"
    exit 1
}

set localfile  [lindex $argv 0]
set remoteip   [lindex $argv 1]
set remoteport [lindex $argv 2]
set remoteuser [lindex $argv 3]
set remotepwd  [lindex $argv 4]
set remotedir  [lindex $argv 5]

set timeout 3600

spawn /usr/bin/rsync -arvPz -e "ssh -l$remoteuser -p$remoteport" $localfile $remoteip:$remotedir

expect {
    "password:" {
        send "$remotepwd\r"
        exp_continue
    }

    "yes/no)?" {
        send "yes\r"
        exp_continue
    }

    timeout {
        close
        break
    }

    eof {
        exit 0
    }
}

exit


使用方法:

/data/scripts/put_file.exp "/srv/search/searchindex/" "IP地址" "端口" "用戶名" "密碼" "/data/searchindex/"

然後加到crontab中,定期同步就OK!!!j_0057.gif



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章