码迷,mamicode.com
首页 > 其他好文 > 详细

2个rman自动恢复的脚本

时间:2017-11-06 11:07:24      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:manage   password   exit   head   sleep   archive   sed   echo   off   

### scripts 1
--the scirpt is used for restore db from vcs to a point to time recovery
--and the target datafile and logfile is different from source

###### config env
export start_date="10/31/2017 06:00:00"
export end_date="11/01/2017 00:00:00"
export restore_date="10/31/2017 23:00:00"
export CLIENT_NAME="vatsmanage.db.crb"
export ORACLE_SID=vats
export ORACLE_HOME=/oracle/product/database/11.2.0
export old_log_path="/db/vats/oradata/vats/"
export new_log_path="/db/oradata/vats/"

##no need change config
export PATH=$PATH:$ORACLE_HOME/bin
export controlfile=`/usr/openv/netbackup/bin/bplist -C $CLIENT_NAME -S pnbumaster -l -t 4 -s $start_date -e $end_date -R / | grep -a "cntrl" |awk ‘{print $8}‘| head -1`

export oracle_user=oracle1

##revert datafile * to new location in target in /tmp/datafile_$ORACLE_SID.sql

su oracle1 -c "sqlplus / as sysdba "<<db
set escape on
spool /tmp/datafile_$ORACLE_SID.sql
set linesize 999 linesize 999 head off feedback off
select ‘set newname for datafile ‘||FILE#||‘ to ‘||‘‘‘‘||name||‘‘‘;‘ from v$datafile;
spool off
db

##su oracle1 -c "sqlplus / as sysdba "<<db1
##@/tmp/datafile_$ORACLE_SID.sql
##db1

 


###### begin to restore controlfile

su oracle1 -c "sqlplus / as sysdba "<<eof0
shutdown immediate;
startup nomount;
eof0

su oracle1 -c "rman target / catalog rman11g/rman11g@cat11g" <<eof1
run {
allocate channel c1 type ‘sbt_tape‘;
send ‘NB_ORA_SERV=pnbumaster,NB_ORA_CLIENT=$CLIENT_NAME‘;
restore controlfile from ‘$controlfile‘;
release channel c1;
}
eof1

su oracle1 -c "sqlplus / as sysdba "<<eof2
select status from v\$instance;
alter database mount;
eof2


####produce restore and recover command
echo "run { " > /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
echo "allocate channel c1 type ‘sbt_tape‘;" >> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
echo "allocate channel c2 type ‘sbt_tape‘;" >> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
echo "allocate channel c3 type ‘sbt_tape‘;" >> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
eche ‘sbt_tape‘;" >> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
echo "send ‘NB_ORA_SERV=pnbumaster,NB_ORA_CLIENT=$CLIENT_NAME‘;">> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
cat /tmp/datafile_$ORACLE_SID.sql >> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
echo "set until time \"to_date(‘$restore_date‘,‘mm yy hh24:mi:ss‘)\";">> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
echo "restore database;" >> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
echo "switch datafile all;" >> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
echo "recover database;">> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
echo "release channel c1;" >> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
echo "release channel c2;" >> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
echo "release channel c3;" >> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
echo "release channel c4;" >> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql
echo "}" >> /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql

chmod 777 /tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql


su oracle1 -c "rman target / catalog rman11g/rman11g@cat11g cmdfile=/tmp/restore_`date ‘+%y%m%d‘`_$ORACLE_SID.sql"


##### mount db and change logfile to new location in target

su oracle1 -c "sqlplus / as sysdba "<<eof5

set linesize 999 linesize 999 head off feedback off
spool /tmp/logfile_$ORACLE_SID.sql
select ‘alter database rename file ‘||‘‘‘‘||member||‘‘‘‘||‘ to ‘||chr(39)||replace(member,‘$old_log_path‘,‘$new_log_path‘)||‘‘‘;‘ from v\$logfile;
spool off
eof5


su oracle1 -c "sqlplus / as sysdba "<<eof6
@/tmp/logfile_$ORACLE_SID.sql
eof6

####open resetlogs ,if meet ora-00392 ,it means it is for to-time reovery,need clear the log .
su oracle1 -c "sqlplus / as sysdba "<<eof7
alter database open resetlogs;
eof7

 

 

### scripts 2
export ORALCE_SID=vats
export ORACLE_HOME=/db/app/product/database/11g

export source_db_home=/oracle/product/database/11.2.0 s
export target_db_home=/db/app/product/database/11g s

export source_db_file_loc=/db/oradata/vats
export target_db_file_loc=/db/oradata/vats

export source_archive_log=/db/oradata/vats/archivelog
export target_archive_log=/db/oradata/vats/archivelog

export source_ip=25.10.0.197
export target_ip=10.241.96.37

export target_passwd="123456"
export oracle_user=oracle
export PORT=15025


### it will prompt passwd
##

scp root@$source_ip:$source_db_home/*$ORALCE_SID* $target_db_home

### it will prompt passwd
###scp root@$source_ip:$source_db_file_loc/*.* $target_db_file_loc

#### use expect to send the datafile use scp
#expect -c "
# spawn scp root@$source_ip:$source_db_file_loc/*.* $target_db_file_loc
# expect {
# \"*assword\" {set timeout 300; send \"$target_passwd\r\"; exp_continue;}
# \"yes/no\" {send \"yes\r\";}
# }
#expect eof"

###use expect to send the datafile use sftp becuase sftp 可以容忍更多的网络延时
#expect -c "
# spawn

sftp root@$source_ip:$source_db_file_loc/suntx02.dbf $target_db_file_loc
# expect {
# \"*assword\" {set timeout 300; send \"$target_passwd\r\"; exp_continue;}
# \"yes/no\" {send \"yes\r\";}
# }
#expect eof"

####如果 send \"quit\r\"; 加入最后,那么sftp 会在遇到网络中断,异常退出
cd $target_db_file_loc
expect -c "
spawn sftp root@$source_ip:$source_db_file_loc
expect {
\"*assword\" {set timeout -1; send \"$target_passwd\r\"; exp_continue;}
\"sftp\" {send \"get *.dbf\r\

";}
}
expect eof"
exit

 


su $oracle_user -c "sqlplus / as sysdba "<<eof2
select status from v\$instance;
alter database mount;
alter database open read only;
shutdown immediate;
startup;
archive log list;
alter system set local_listener=‘(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$target_ip)(PORT=$PORT)))‘ scope=both;
shutdown immediate;
startup;
eof2


su $oracle_user -c "echo "SQLNET.EXPIRE_TIME=10" >> $ORACLE_HOME/network/admin/sqlnet.ora"

su $oracle_user -c "echo "$ORACLE_SID =" >> $ORACLE_HOME/network/admin/listener.ora"
su $oracle_user -c "echo "(DESCRIPTION_LIST =" >> $ORACLE_HOME/network/admin/listener.ora"
su $oracle_user -c "echo "(DESCRIPTION =">> $ORACLE_HOME/network/admin/listener.ora"
su $oracle_user -c "echo "(ADDRESS_LIST =">> $ORACLE_HOME/network/admin/listener.ora"
su $oracle_user -c "echo "(ADDRESS = (PROTOCOL = TCP)(HOST = $target_ip)(PORT = $PORT))" >> $ORACLE_HOME/network/admin/listener.ora "
su $oracle_user -c "echo ")" >> $ORACLE_H

EXPIRE_TIME=10" >> cd $ORACLE_HOME/network/admin/sqlnet.ora "

 

 

--
--scp root

#!/usr/bin/expect
#spawn scp root@$source_ip:$source_db_home/*$db_name* $target_db_home
#set timeout 20
#expect "$target_passwd"
#exec sleep 2
#send "password\r"
#interact

OME/network/admin/listener.ora "
su $oracle_user -c "echo ")" >> $ORACLE_HOME/network/admin/listener.ora "
su $oracle_user -c "echo ")" >> $ORACLE_HOME/network/admin/listener.ora "


su $oracle_user -c "lsnrctl start $ORACLE_SID"
su $oracle_user -c "lsnrctl status $ORACLE_SID"


su $oracle_user -c "sqlplus / as sysdba "<<eof3
select status from v\$instance;
alter system register;
eof3


su $oracle_user -c "lsnrctl status $ORACLE_SID"

 


###su $oracle_user -c "echo "SQLNET.

 

2个rman自动恢复的脚本

标签:manage   password   exit   head   sleep   archive   sed   echo   off   

原文地址:http://www.cnblogs.com/feiyun8616/p/7791769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!