借助expect编写的mysql数据导出脚本
脚本内容:
[root@app01 haiming]# vim DumpSqlScript3
#!/usr/expect/bin/expect
# par and var
set DBIP [lindex $argv 0]
set BackIP [lindex $argv 1]
set Database [lindex $argv 2]
set fileName [exec date +%Y%m%d%H%M] //时间变量格式化作为文件名
# login to DBserver…
spawn ssh -i /root/clickoo-openssh $DBIP
expect “*key*”
send “ZXCdsaQWEr”
expect “*password*”
send “testloadr”
expect “*#*”
# dump mysql data…
send “cd sqlScriptr”
send “mysqldump -u root -p $Database > /root/sqlScript/$Database’_’$fileName.sqlr”
expect “*password*”
send “r”
#tar and scp…
expect eof
send “tar -zcvf $Database’_’$fileName.tar.gz $Database’_’$fileName.sqlr”
set timeout 10
send “scp $Database’_’$fileName.tar.gz root@$BackIP:/root/haiming/sqlScriptr”
expect “*password*”
send “testloadr”
expect eof
执行:
./DumpSqlScript 10.101.102.63 10.101.102.64 test