#!/usr/bin/expect -f
###################################################
# Script Function :Backup batch for oracle
# Auther: Zhao Hai
# Date: 20160209
# Usage:
# 1. Input "ip,dba_user,password,db_user,password" information to the configuration file named "SERVERLIST".
# example: 10.8.190.24 oracle oracle dbuser dbuserpasswd
# 2. run command "expect oracle_backup.exp"
###################################################
###################################################
# Set the timeout for expect operation.
# Set date variable for dump file name and log name usage.
# Set SERVERLIST variable as configuration file name.
###################################################
set timeout 6000
set DATE [exec date "+%Y%m%d"]
set SERVERLIST [open SERVERLIST r]
###################################################
# main()
###################################################
while { [gets $SERVERLIST LINE]>=0 } {
set ADR [lindex $LINE 0]
set DBA [lindex $LINE 1]
set DBAPASS [lindex $LINE 2]
set USR [lindex $LINE 3]
set USRPASS [lindex $LINE 4]
spawn ssh $DBA@$ADR
expect {
"yes/no" {send "yes\r"; exp_continue}
"Password:" {send "$DBAPASS\r"}
"password:" {send "$DBAPASS\r"}
}
# Make dump directory for oracle backup job.
expect "oracle@"
send "mkdir /tmp/db-backup\r"
expect "oracle@"
send "sqlplus / as sysdba;\r"
expect "SQL>"
send "create directory ora_dump as '/tmp/db-backup'\r"
expect "SQL>"
send "grant read,write on directory ora_dump to public\r"
expect "SQL>"
send "quit\r"
# Make dump file for oracle db.
expect "oracle@"
send "export NLS_LANG=AMERICAN_AMERICA.ZHS32GB18030\r"
expect "oracle@"
send "expdp $USR/$USRPASS schemas=$USR dumpfile=ora_dump:$USR.dump.$DATE logfile=ora_dump:$USR.log.$DATE\r"
expect "oracle@"
send "exit\r"
send "exit\r"
}
close $SERVERLIST
expect eof
exit