日期:2014-05-16  浏览次数:20652 次

借助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 "ZXCdsaQWE\r"
expect "*password*"
send "testload\r"
expect "*#*"

# dump mysql data...
send "cd sqlScript\r"
send "mysqldump -u root -p $Database > /root/sqlScript/$Database'_'$fileName.sql\r"
expect "*password*"
send "\r"

#tar and scp...
expect eof
send "tar -zcvf $Database'_'$fileName.tar.gz $Database'_'$fileName.sql\r"
set timeout 10
send  "scp $Database'_'$fileName.tar.gz root@$BackIP:/root/haiming/sqlScript\r"
expect "*password*"
send "testload\r"
expect eof

执行:
./DumpSqlScript 10.101.102.63  10.101.102.64 test