### 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="bpo1.db.os"
export ORACLE_SID=bpo
export ORACLE_HOME=/db/bpo/app/product/11g
export ORACLE_BASE=/db/bpo/app/product

export OLD_ORACLE_HOME=/db/bpo/oracleapp/database/11.2.0/db_1/dbs

export old_log_path="/db/vats/oradata/vats/"
export new_log_path="/db/bpo/data/"

export old_data_path="+DATA_DG/bpo/datafile"
export new_data_path="/db/bpo/data"

##no need change config

export PATH=$PATH:$ORACLE_HOME/bin
--for linux
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`
--for aix
export controlfile=`/usr/openv/netbackup/bin/bplist -C $CLIENT_NAME -S pnbumaster -l -t 4 -s $start_date -e $end_date -R / | grep "cntrl" |awk '{print $8}'| head -1`

export oracle_user=osbpo

###
cd $new_data_path
mkdir archivelog

cd $ORACLE_BASE
mkdir diag diag/rdbms diag/tnslsnr

##for target is already ok ,it meand target db is already started,revert datafile * to new location in target in /tmp/datafile_$ORACLE_SID.sql ,

su $oracle_user -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

##########for target is not ok, it means target db is not started,revert datafile * to new location in source and copy to target

su $oracle_user -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 '||chr(39)||replace(name,'$old_data_path','$new_data_path')||'''||'.dbf;' from v\$datafile;
spool off
db

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

###### begin to restore controlfile

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

su $oracle_user -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
echo "allocate channel c4 type '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/dd/yyyy 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"

##### ,for source log file is in filesytem ,mount db and change logfile to new location in target ,no need , 在rman 调用了switch datafile all, 后 open resetlogs 会自动 clear log and create logfile in new location.

##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_path1','$new_log_path')||''';' from v\$logfile;
#spool off
#eof5

#####for source log file is in asm, mount db and change logfile to new location in target   -no need , 在rman 调用了switch datafile all, 后 open resetlogs 会自动 clear log and create logfile in new location.
##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)||'$new_log_patch'||substr(member,instr(member,'/',-1,1) +1) 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 会在遇到网络中断,异常退出 ,expect is for linux
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.

########for tempfile re-creation

1.观察alter日志,tempfile 是否会自动创建。
check log
cd /db/db1/app/db/diag/rdbms/db1/db11/trace
tail -f alert*.log

2,做10次switch logfile,检查logfile
alter system switch logfile;
3.temp file
理论上会tempfile 会自动创建在相同目录,11g会自动创建临时表空间,每个表空间200M,不会自动扩展,因此需要手工调整临时文件大小

4.undo 也是不会开启自动扩展的

###scripts to produce tempfile production:

select 'ALTER TABLESPACE '||t.name||' add tempfile '||''''||f.name||''''||f.bytes/1024/1024||'M REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;' from v

$tempfile f ,(select ts#,name from v$tablespace t where included_in_database_backup='NO') t
where f.ts#= t.ts#

#####################

ALTER TABLESPACE TEMP ADD TEMPFILE '/db/oracle/oradata/pisa/temp01.dbf'
SIZE 4929M REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;
ALTER TABLESPACE PISA_TEMP ADD TEMPFILE '/db/oracle/oradata/pisa/PISA_TEMP.dbf'
SIZE 32767M REUSE AUTOEXTEND ON NEXT 20971520 MAXSIZE 32767M;
ALTER TABLESPACE PISA_TEMP ADD TEMPFILE '/db/oracle/oradata/pisa/PISA_TEMP01.dbf'
SIZE 32767M REUSE AUTOEXTEND ON NEXT 20971520 MAXSIZE 32767M;
ALTER TABLESPACE ETL_TEMP ADD TEMPFILE '/db/oracle/oradata/pisa/ETL_TEMP.dbf'
SIZE 2000M REUSE AUTOEXTEND ON NEXT 10485760 MAXSIZE 5000M;
ALTER TABLESPACE TS_EAST_TMP ADD TEMPFILE '/db/oracle/oradata/pisa/east_data_tmp01.dbf'
SIZE 2000M REUSE AUTOEXTEND ON NEXT 104857600 MAXSIZE 32767M;
ALTER TABLESPACE TS_EAST_TMP ADD TEMPFILE '/db/oracle/oradata/pisa/east_data_tmp02.dbf'
SIZE 2000M REUSE AUTOEXTEND ON NEXT 104857600 MAXSIZE 32767M;
ALTER TABLESPACE TS_SAFE_TMP ADD TEMPFILE '/db/oracle/oradata/pisa/safe_data_tmp01.dbf'
SIZE 2000M REUSE AUTOEXTEND ON NEXT 104857600 MAXSIZE 32767M;
ALTER TABLESPACE TS_SAFE_TMP ADD TEMPFILE '/db/oracle/oradata/pisa/safe_data_tmp02.dbf'
SIZE 2000M REUSE AUTOEXTEND ON NEXT 104857600 MAXSIZE 32767M;
ALTER TABLESPACE TS_IGRSVISE_TMP drop TEMPFILE '/db/oracle/oradata/pisa/igrsvise_data_tmp01.dbf'
SIZE 32767M REUSE AUTOEXTEND ON NEXT 104857600 MAXSIZE 32767M;
ALTER TABLESPACE TS_IGRSVISE_TMP drop TEMPFILE '/db/oracle/oradata/pisa/igrsvise_data_tmp02.dbf'
SIZE 32767M REUSE AUTOEXTEND ON NEXT 104857600 MAXSIZE 32767M;
ALTER TABLESPACE TS_IGRSFIN_TMP ADD TEMPFILE '/db/oracle/oradata/pisa/igrsfin_data_tmp01.dbf'
SIZE 2000M REUSE AUTOEXTEND ON NEXT 104857600 MAXSIZE 32767M;
ALTER TABLESPACE TS_IGRSFIN_TMP ADD TEMPFILE '/db/oracle/oradata/pisa/igrsfin_data_tmp02.dbf'
SIZE 2000M REUSE AUTOEXTEND ON NEXT 104857600 MAXSIZE 32767M;

---

ALTER TABLESPACE TS_IGRSVISE_TMP ADD TEMPFILE '/db/oracle/oradata/pisa/igrsvise_data_tmp01.dbf'
SIZE 32767M REUSE AUTOEXTEND ON NEXT 104857600 MAXSIZE 32767M;
ALTER TABLESPACE TS_IGRSVISE_TMP ADD TEMPFILE '/db/oracle/oradata/pisa/igrsvise_data_tmp02.dbf'
SIZE 32767M REUSE AUTOEXTEND ON NEXT 104857600 MAXSIZE 32767M;

##select name from v$tempfile;

###alter database tempfile '/db/oracle/oradata/pisa/temp01.dbf' drop

-----step 2 drop old
select 'alter database tempfile '||''''||name||''' drop;' from v$tempfile;

###sample 0:

### scripts 1
##since oracle 11g is version equl cat1log 11g ,so we use man restore connect catalog db
##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,the script is running in root user
##below is running the linux nbu machine
##clearn old .dbf file in other direcotry /os/oradata
###if other db is 12c ,the key word CLIENT_NAME/new_log_path/new_data_path need change following the new db
##add pause to debug to /usr/bin for debug question

hostname=`df -h`
echo "cems total size is 100g, make sure /os/oradata/ have more space than 100g,if not clearn old .dbf file in /os/oradata/“
echo "Please input your biplist query start_date eg 03/17/2019 06:00:00 :"
read start_date

echo "Please input your biplist query end_time eg 03/19/2019 00:00:00 :"
read end_time

echo "Please input your rman restore_date until time eg 03/18/2019 23:00:00 ,and it is between start_time and end_time:"
read restore_date

echo "restore_date=$restore_date,continue to create ? (y/n):"
read continue
if [ "$continue" == "y" ]

then

###### section 1 config env
###config restore date, backup_start_date,end_start_date, nbu_client_name,ORACLE_SID,target ORACLE_HOME,target ORACLE_BASE,and PATH and oracle_user,time is "mm-dd-yyyy hh24:mi;ss"
##START_DATA is checking backup begin data, end_data is checking backup end time
##nbu client is client name, ORACLE_SID is db SID
#export start_date="03/16/2019 06:00:00"
#export end_date="03/19/2019 00:00:00"
#export restore_date="03/16/2019 23:00:00"
export CLIENT_NAME="cems.db.os"
export ORACLE_SID=cems
export ORACLE_HOME=/oracle/product/database/11.2.0
#export ORACLE_BASE= /oracle1/app/product/11.2.0/
export PATH=$PATH:$ORACLE_HOME/bin
export oracle_user=oracle

export new_log_path="/os/oradata/cems"
export new_data_path="/os/oradata/cems"

###export OLD_ORACLE_HOME=/db/bpo/oracleapp/database/11.2.0/db_1/dbs

###create direcotry for db

#mkdir -p /os/oradata/cems/archivelog
#mkdir -p /oracle/admin/cems/adump
#mkdir -p /oracle/diag/rdbms/cems/diag/rdbms
#mkdir -p /oracle/diag/rdbms/cems/diag/tnslsnr

#chmod -R 777 /oracle/admin/cems/adump
#chmod -R 777 /os/oradata/cems/archivelog
#chmod -R 777 /oracle/diag/rdbms/cems/diag
#chmod -R 777 /os/oradata/cems/

###config logfile and datafile change patch , 暂时不需要
##export old_log_path="/db/bcds/oradata/bcds/"
#export new_log_path="/datalv/bcds/"

##export old_data_path="/db/bcds/oradata/bcds"
##export new_data_path="/datalv/bcds"

## config os version is unix or linux

###for linux
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`
##for aix and hp
##export controlfile=`/usr/openv/netbackup/bin/bplist -C $CLIENT_NAME -S pnbumaster -l -t 4 -s $start_date -e $end_date -R / | grep "cntrl" |awk '{print $8}'| head -1`

echo "##########section 1 is ok###############"

### mkdir
#cd $new_data_path
#mkdir archivelog

#cd $ORACLE_BASE
#mkdir diag diag/rdbms diag/tnslsnr

###### section 2 switch to oracle1 and keep root profile and begin to restore controlfile and mount db,如果碰到ORA-27101: shared memory realm does not exist,可以忽略

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

###set dbid= 89095998

su $oracle_user -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 $oracle_user -c "sqlplus / as sysdba "<<eof2
select status from v\$instance;
alter database mount;
eof2

echo echo "##########section 2 is ok###############"

### section 3 produce log file and data file change file

su $oracle_user -c "sqlplus / as sysdba "<<eof2
set linesize 999 pagesize 0 head off feedback off echo off verify off trimspool on
spool /tmp/logpatch_$ORACLE_SID.sql
select replace(member,'/redo01.log','') from v\$logfile where GROUP# = 1;
spool off
eof2

export old_log_path=`grep os /tmp/logpatch_$ORACLE_SID.sql`
export old_data_path=`grep os /tmp/logpatch_$ORACLE_SID.sql`

su $oracle_user -c "sqlplus / as sysdba "<<db
set escape on
set linesize 999 pagesize 0 head off feedback off echo off verify off trimspool on
spool /tmp/datafile_$ORACLE_SID.sql
select 'set newname for datafile '||FILE#||' to '||''''||replace(name,'$old_data_path','$new_data_path')||''''||';' from v\$datafile;
spool off
db

sed '1d;$d' /tmp/datafile_$ORACLE_SID.sql > /tmp/datafile_path_$ORACLE_SID.sql

echo "##clean FRA AREA#####"
su $oracle_user -c "rman target / "<<eof2
crosscheck backup;
delete noprompt force backup;
delete noprompt force archivelog;
eof2

###add fix about RMAN-20207: INCARNATION

su $oracle_user -c "sqlplus / as sysdba "<<db
set escape on
set linesize 999 pagesize 0 head off feedback off echo off verify off trimspool on
spool /tmp/scn_$ORACLE_SID.sql
select max(INCARNATION#) from v\$database_incarnation where RESETLOGS_TIME < to_date('$restore_date','mm/dd/yyyy hh24:mi:ss');
spool off
db

sed '1d;$d' /tmp/scn_$ORACLE_SID.sql > /tmp/scn_start_$ORACLE_SID.sql
INCARNATION_NO=`cat /tmp/scn_start_$ORACLE_SID.sql`

####produce restore and recover command
echo "reset database to incarnation $INCARNATION_NO;" > /tmp/restore_`date '+%y%m%d'`_$ORACLE_SID.sql
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
echo "allocate channel c4 type '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_path_$ORACLE_SID.sql >> /tmp/restore_`date '+%y%m%d'`_$ORACLE_SID.sql
echo "set until time \"to_date('$restore_date','mm/dd/yyyy 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

echo "##########section 3 is ok###############"

### section 4 begin to restore and recover
su $oracle_user -c "rman target / catalog rman11g/rman11g@cat11g cmdfile=/tmp/restore_`date '+%y%m%d'`_$ORACLE_SID.sql"

echo "##########section 4 is ok###############"

### section 5 begin to rename logfile and resetlogs open

su $oracle_user -c "sqlplus / as sysdba "<<eof5
set escape on
set linesize 999 pagesize 0 head off feedback off echo off verify off trimspool on
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;
select 'alter database clear logfile group '|| GROUP#||';' from v\$logfile;
spool off
eof5

sed '/^SQL/d' /tmp/logfile_$ORACLE_SID.sql > /tmp/logfile_path_$ORACLE_SID.sql

##sed '1d;$d' /tmp/logfile_$ORACLE_SID.sql > /tmp/logfile_path_$ORACLE_SID.sql
su $oracle_user -c "sqlplus / as sysdba "<<eof6
select status from v\$instance;
@/tmp/logfile_path_$ORACLE_SID.sql
alter database open RESETLOGS;
eof6

echo echo "##########section 5 is ok finish ###############"

########for tempfile re-creation produce create new tempfile and drop old tempfile ,and execute it.

##export old_data_path=/tmp

su $oracle_user -c "sqlplus / as sysdba "<<db4
set linesize 32760 pagesize 0 head off feedback off echo off verify off trimspool on
spool /tmp/tempfile_$ORACLE_SID.sql
select 'ALTER TABLESPACE '||t.name||' add tempfile '||''''||replace(f.name,'$old_data_path','$new_data_path') ||''''||' size 10M REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;' from v\$tempfile f ,
(select ts#,name from v\$tablespace t where included_in_database_backup='NO') t where f.ts#= t.ts#;
spool off
db4

sed '1d;2d;$d' /tmp/tempfile_$ORACLE_SID.sql > /tmp/tempfile_add_$ORACLE_SID.sql

su $oracle_user -c "sqlplus / as sysdba "<<db5
set linesize 32760 pagesize 0 head off feedback off echo off verify off trimspool on
spool /tmp/tempfile_$ORACLE_SID.sql
select 'alter tablespace '||t.name||' drop tempfile '||''''||f.name||''''||';' from v\$tempfile f ,(select ts#,name from v\$tablespace t where included_in_database_backup='NO') t
where f.ts#= t.ts#;
spool off
db5

sed '1d;2d;$d' /tmp/tempfile_$ORACLE_SID.sql > /tmp/tempfile_drop_$ORACLE_SID.sql

su $oracle_user -c "sqlplus / as sysdba "<<db6
set linesize 32760 pagesize 0 head off feedback off echo off verify off trimspool on
spool /tmp/tempfile_$ORACLE_SID.sql
@/tmp/tempfile_add_$ORACLE_SID.sql
@/tmp/tempfile_drop_$ORACLE_SID.sql
spool off
db6

echo "##########section 6 is ok finish ###############"

###sample 1:

##准备阶段

#switch restore user profile to target database profile in nbu test machine
su - oracle1
cp profile_bcds .profile

####2 检查uid 和所有的gid 跟生产一致

usermod -u 1001 oracle1
groupmod -g 1001 dba
groupmod -g 1001 oinstall

### 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,the script is running in root user

###### section 1 config env
###config restore date, backup_start_date,end_start_date, nbu_client_name,ORACLE_SID,target ORACLE_HOME,target ORACLE_BASE,and PATH and oracle_user,time is "mm-dd-yyyy hh24:mi;ss"
export start_date="06/28/2018 06:00:00"
export end_date="06/29/2018 00:00:00"
export restore_date="06/28/2018 23:00:00"
export CLIENT_NAME="bcdp.db.os"
export ORACLE_SID=bcds
export ORACLE_HOME=/oracle1/app/product/11.2.0/dbhome_1
export ORACLE_BASE= /oracle1/app/product/11.2.0/
export PATH=$PATH:$ORACLE_HOME/bin
export oracle_user=oracle1

###export OLD_ORACLE_HOME=/db/bpo/oracleapp/database/11.2.0/db_1/dbs

###config logfile and datafile change patch
export old_log_path="/db/bcds/oradata/bcds/"
export new_log_path="/datalv/bcds/"

export old_data_path="/db/bcds/oradata/bcds"
export new_data_path="/datalv/bcds"

## config os version is unix or linux

###for linux
##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`
##for aix and hp
export controlfile=`/usr/openv/netbackup/bin/bplist -C $CLIENT_NAME -S pnbumaster -l -t 4 -s $start_date -e $end_date -R / | grep "cntrl" |awk '{print $8}'| head -1`

echo "##########section 1 is ok###############"

### mkdir
#cd $new_data_path
#mkdir archivelog

#cd $ORACLE_BASE
#mkdir diag diag/rdbms diag/tnslsnr

###### section 2 switch to oracle1 and keep root profile and begin to restore controlfile and mount db

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

su $oracle_user -c "rman target / catalog rman11g/rman11g@cat11g" <<eof1
set dbid= 89095998
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 $oracle_user -c "sqlplus / as sysdba "<<eof2
select status from v\$instance;
alter database mount;
eof2

echo echo "##########section 2 is ok###############"

### section 3 produce log file and data file change file

##for target is already ok ,it meand target db is already started,revert datafile * to new location in target in /tmp/datafile_$ORACLE_SID.sql ,

### produce data file change file
su $oracle_user -c "sqlplus / as sysdba "<<db
set escape on
set linesize 999 pagesize 0 head off feedback off echo off verify off trimspool on
spool /tmp/datafile_$ORACLE_SID.sql
select 'set newname for datafile '||FILE#||' to '||''''||replace(name,'$old_data_path','$new_data_path')||''''||';' from v\$datafile;
spool off
db

sed '1d;$d' /tmp/datafile_$ORACLE_SID.sql > /tmp/datafile_path_$ORACLE_SID.sql

####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
echo "allocate channel c4 type '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_path_$ORACLE_SID.sql >> /tmp/restore_`date '+%y%m%d'`_$ORACLE_SID.sql
echo "set until time \"to_date('$restore_date','mm/dd/yyyy 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

echo "##########section 3 is ok###############"

### section 4 begin to restore and recover
su $oracle_user -c "rman target / catalog rman11g/rman11g@cat11g cmdfile=/tmp/restore_`date '+%y%m%d'`_$ORACLE_SID.sql"

echo "##########section 4 is ok###############"

### section 5 begin to rename logfile and resetlogs open

su $oracle_user -c "sqlplus / as sysdba "<<eof5
set escape on
set linesize 999 pagesize 0 head off feedback off echo off verify off trimspool on
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;
select 'alter database clear logfile group '|| GROUP#||';' from v\$logfile;
spool off
eof5

sed '/^SQL/d' /tmp/logfile_$ORACLE_SID.sql > /tmp/logfile_path_$ORACLE_SID.sql

##sed '1d;$d' /tmp/logfile_$ORACLE_SID.sql > /tmp/logfile_path_$ORACLE_SID.sql

su $oracle_user -c "sqlplus / as sysdba "<<eof6
select status from v\$instance;
@/tmp/logfile_path_$ORACLE_SID.sql
alter database open RESETLOGS;
eof6

echo echo "##########section 5 is ok finish ###############"

##准备阶段

#switch restore user profile to target database profile in nbu test machine
su - oracle1
cp profile_bcds .profile

####2 检查uid 和所有的gid 跟生产一致

usermod -u 1001 oracle1
groupmod -g 1001 dba
groupmod -g 1001 oinstall

### 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,the script is running in root user

###### section 1 config env
###config restore date, backup_start_date,end_start_date, nbu_client_name,ORACLE_SID,target ORACLE_HOME,target ORACLE_BASE,and PATH and oracle_user,time is "mm-dd-yyyy hh24:mi;ss"
export start_date="06/28/2018 06:00:00"
export end_date="06/29/2018 00:00:00"
export restore_date="06/28/2018 23:00:00"
export CLIENT_NAME="bcdp.db.os"
export ORACLE_SID=bcds
export ORACLE_HOME=/oracle1/app/product/11.2.0/dbhome_1
export ORACLE_BASE= /oracle1/app/product/11.2.0/
export PATH=$PATH:$ORACLE_HOME/bin
export oracle_user=oracle1

###export OLD_ORACLE_HOME=/db/bpo/oracleapp/database/11.2.0/db_1/dbs

###config logfile and datafile change patch
export old_log_path="/db/bcds/oradata/bcds/"
export new_log_path="/datalv/bcds/"

export old_data_path="/db/bcds/oradata/bcds"
export new_data_path="/datalv/bcds"

## config os version is unix or linux

###for linux
##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`
##for aix and hp
export controlfile=`/usr/openv/netbackup/bin/bplist -C $CLIENT_NAME -S pnbumaster -l -t 4 -s $start_date -e $end_date -R / | grep "cntrl" |awk '{print $8}'| head -1`

echo "##########section 1 is ok###############"

### mkdir
#cd $new_data_path
#mkdir archivelog

#cd $ORACLE_BASE
#mkdir diag diag/rdbms diag/tnslsnr

##########for target is not ok, it means target db is not started,revert datafile * to new location in source and copy to target

#su $oracle_user -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 '||chr(39)||replace(name,'$old_data_path','$new_data_path')||'''||'.dbf;' from v\$datafile;
#spool off
#db

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

###### section 2 switch to oracle1 and keep root profile and begin to restore controlfile and mount db

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

su $oracle_user -c "rman target / catalog rman11g/rman11g@cat11g" <<eof1
set dbid= 89095998
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 $oracle_user -c "sqlplus / as sysdba "<<eof2
select status from v\$instance;
alter database mount;
eof2

echo echo "##########section 2 is ok###############"

### section 3 produce log file and data file change file

##for target is already ok ,it meand target db is already started,revert datafile * to new location in target in /tmp/datafile_$ORACLE_SID.sql ,

### produce data file change file
su $oracle_user -c "sqlplus / as sysdba "<<db
set escape on
set linesize 999 pagesize 0 head off feedback off echo off verify off trimspool on
spool /tmp/datafile_$ORACLE_SID.sql
select 'set newname for datafile '||FILE#||' to '||''''||replace(name,'$old_data_path','$new_data_path')||''''||';' from v\$datafile;
spool off
db

sed '1d;$d' /tmp/datafile_$ORACLE_SID.sql > /tmp/datafile_path_$ORACLE_SID.sql

####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
echo "allocate channel c4 type '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_path_$ORACLE_SID.sql >> /tmp/restore_`date '+%y%m%d'`_$ORACLE_SID.sql
echo "set until time \"to_date('$restore_date','mm/dd/yyyy 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

echo "##########section 3 is ok###############"

### section 4 begin to restore and recover
su $oracle_user -c "rman target / catalog rman11g/rman11g@cat11g cmdfile=/tmp/restore_`date '+%y%m%d'`_$ORACLE_SID.sql"

echo "##########section 4 is ok###############"

### section 5 begin to rename logfile and resetlogs open

su $oracle_user -c "sqlplus / as sysdba "<<eof5
set escape on
set linesize 999 pagesize 0 head off feedback off echo off verify off trimspool on
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;
select 'alter database clear logfile group '|| GROUP#||';' from v\$logfile;
spool off
eof5

sed '/^SQL/d' /tmp/logfile_$ORACLE_SID.sql > /tmp/logfile_path_$ORACLE_SID.sql

##sed '1d;$d' /tmp/logfile_$ORACLE_SID.sql > /tmp/logfile_path_$ORACLE_SID.sql

su $oracle_user -c "sqlplus / as sysdba "<<eof6
select status from v\$instance;
@/tmp/logfile_path_$ORACLE_SID.sql
alter database open RESETLOGS;
eof6

echo "##########section 5 is ok finish ###############"

### section 6 begin to rename logfile and resetlogs open

########for tempfile re-creation produce create new tempfile and drop old tempfile ,and execute it.

export old_data_path=/tmp

su $oracle_user -c "sqlplus / as sysdba "<<db4
set linesize 32760 pagesize 0 head off feedback off echo off verify off trimspool on
spool /tmp/tempfile_$ORACLE_SID.sql
select 'ALTER TABLESPACE '||t.name||' add tempfile '||''''||replace(f.name,'$old_data_path','$new_data_path') ||''''||' size '||f.bytes/1024/1024||'M REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;' from v\$tempfile f ,
(select ts#,name from v\$tablespace t where included_in_database_backup='NO') t where f.ts#= t.ts#;
spool off
db4

sed '1d;2d;$d' /tmp/tempfile_$ORACLE_SID.sql > /tmp/tempfile_add_$ORACLE_SID.sql

su $oracle_user -c "sqlplus / as sysdba "<<db5
set linesize 32760 pagesize 0 head off feedback off echo off verify off trimspool on
spool /tmp/tempfile_$ORACLE_SID.sql
select 'alter tablespace '||t.name||' drop tempfile '||''''||f.name||''''||';' from v\$tempfile f ,(select ts#,name from v\$tablespace t where included_in_database_backup='NO') t
where f.ts#= t.ts#;
spool off
db5

sed '1d;2d;$d' /tmp/tempfile_$ORACLE_SID.sql > /tmp/tempfile_drop_$ORACLE_SID.sql

su $oracle_user -c "sqlplus / as sysdba "<<db6
set linesize 32760 pagesize 0 head off feedback off echo off verify off trimspool on
spool /tmp/tempfile_$ORACLE_SID.sql
@/tmp/tempfile_add_$ORACLE_SID.sql
@/tmp/tempfile_drop_$ORACLE_SID.sql
spool off
db6

echo "##########section 6 is ok finish ###############"

###new for debug tool pasue

1. 打开文本编辑器,在 /home/cheng/bin  目录中创建一名为pause的文本文件。

2.在pause文件中写入以下内容:

#!/bin/bash
get_char()
{
SAVEDSTTY=`stty -g`
stty -echo
stty raw
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty $SAVEDSTTY
}

if [ -z "$1" ]; then
echo '请按任意键继续...'
else
echo -e "$1"
fi

get_char

保存并退出文本编辑器。

3.打开终端,执行以下命令:

chmod 0755 /home/cheng/bin/pause
---------------------
作者:Colin91
来源:CSDN
原文:https://blog.csdn.net/colin91/article/details/9119031
版权声明:本文为博主原创文章,转载请附上博文链接!

#############issue 1  https://blog.csdn.net/lldustc_blog/article/details/78348140

RMAN中通过时间点不完全恢复报ORA-01841的解决办法

环境

  • 操作系统 oracle linux 6.5
  • 数据库 oracle 11.2.0.4

执行脚本

run {
allocate channel c1 type disk;
sql 'alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss";
set until time='2017-10-8 00:00:00';
restore database;
recover database;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

错误及分析

执行脚本后报错

RMAN-03002: failure of recover command at …….. 
RMAN-11003: failure during parse/execution of SQL ……. 
ORA-01841: (full) year must be between -4713 and +9999, and not be 0

此处为貌似是我们的语句写错了,其实是oracle的bug 
貌似有两个问题

  1. 需设置NLS_LANG环境变量

    在执行rman命令前先执行export NLS_LANG=AMERICAN_AMERICA.AL32UTF8

  2. 此处不能将restore database和recover database放在一个run块里,需在单独的run块中完成

run {
allocate channel c1 type disk;
sql 'alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss";
set until time='2017-10-8 00:00:00';
restore database;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
run {
allocate channel c1 type disk;
sql 'alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss";
set until time='2017-10-8 00:00:00';
recover database;
}

最新文章

  1. iOS - 对UIColor颜色反差
  2. 【BZOJ】3832: [Poi2014]Rally
  3. java中打印变量地址
  4. C# Rotating Oval
  5. PHP下拉框内容随单选框内容变化
  6. TC79
  7. Spring常用的接口和类(一)
  8. URAL 2070 Interesting Numbers (找规律)
  9. Java中执行外部命令
  10. 把AS代码链接到fla文件
  11. [置顶] ./build_native 时出现please define NDK_ROOT
  12. Struts2.3.16.3 基本9个jar包
  13. 【SQL语句】 - Ctrl+3 查询表属性的存储过程 [sp_select_talberowName]
  14. Win7+花生壳6.0+tomcat打做自己的web服务器(搭建自己的网站)(参考)
  15. 设置MySQL最大连接数
  16. c 语言typedef 和 define的使用和区别
  17. [Swift]LeetCode951. 翻转等价二叉树 | Flip Equivalent Binary Trees
  18. Redis Cluster [WARNING] Node 127.0.0.1:7003 has slots in migrating state (15495).
  19. Zookeeper客户端Curator---Getting Started
  20. GCC制作动态库导出符号表【转】

热门文章

  1. windows下的host文件在哪里,有什么作用?
  2. laravel基础课程---2、Laravel配置文件、路由及php artisan(php artisan是什么)
  3. 脚踏实地学C#1-基元类型
  4. hdu 1753 大明A+B(大数)
  5. 迁移学习算法之TrAdaBoost ——本质上是在用不同分布的训练数据,训练出一个分类器
  6. ARM版本系列及家族成员梳理
  7. 【旧文章搬运】关于在指定进程调用KeUserModeCallback的问题
  8. hibernate Criteria中or和and的用法
  9. org.apache.catalina.LifecycleException报错解决方法
  10. TypeScript完全解读(26课时)_1.TypeScript完全解读-开发环境搭建