Monday, May 30, 2011

dataguard check report

# Below are Data Guard Reports --------------
0 */2 * * * /opt/oracle/DBAENGG/scripts/ofr_dg_check_nw.ksh 2>&1


pbpmccdbaora1:/home/oracle>>cat /opt/oracle/DBAENGG/scripts/ofr_dg_check_nw.ksh
#!/usr/bin/ksh
. $HOME/.setup_PMCGRD20.ksh
#export ORACLE_HOME=/opt/oracle/PWHRMN20/product/dbms/10.2.0
export PATH=$ORACLE_HOME/bin:$PATH
#export TNS_ADMIN=/opt/oracle/TNS
SCRIPTDIR=`dirname "$0"`
FILENAME=`basename "$0"`
ORALOGIN=sys
ORAPASSWD=ic0lumbus#
EORAPASSWD=pd0jpm412
InValidDB=0
#MAILLIST="na_pb_bd_eq_fo_oracle_team@jpmorgan.com,ib.core.svcs.oracle.mbox@jpmorgan.com"
MAILLIST1="ib.core.svcs.oracle.mbox@jpmorgan.com"
MAILLIST="ib_global_dba@jpmchase.com"
LOG_FILE=/tmp/ofr_dbstdby.txt
LOG_FILE2=/tmp/ofr_dbstdby2.txt
LOG_TMP=/tmp/stdby_nw.999
LOG_TMP1=/tmp/stdby_nw.8888
>$LOG_TMP
>$LOG_TMP1
>$LOG_FILE
>$LOG_FILE2
for ORATABLN in `cat /opt/oracle/DBAENGG/dbmap/oratab.stdby`
do
  case $ORATABLN in
        \#*)            ;;      #comment-line
          *)
     unset DB_NAME DB_STDBY ORAPASSWD APP
     if [ "`echo $ORATABLN | awk -F: '{print $5}' -`" = "Y" ] ; then
            DB_NAME=`echo $ORATABLN | awk -F: '{print $1}' -`
            DB_STDBY=`echo $ORATABLN | awk -F: '{print $2}' -`
            LOB=`echo $ORATABLN | awk -F: '{print $3}' -`
            ORAPASSWD=`echo $ORATABLN | awk -F: '{print $4}' -`
            APP=`echo $ORATABLN | awk -F: '{print $6}' -`
if [ "${DB_NAME}" = "PWHGRD20" ] ; then
ORALOGIN=dba_bear
ORAPASSWD=ydarw1n#
else
ORALOGIN=sys
#if [ "${LOB}" = "PB" ]; then
#ORAPASSWD=ic0lumbus#
#else
#ORAPASSWD=pd0jpm412
#fi
fi
if [ "${DB_NAME}" = "PCRDAR20" ] ; then
ORALOGIN=sys
ORAPASSWD=ic0lumbus#
fi
if [ "${LOB}" = "APACEQ" ]; then
ORALOGIN=dbsnmp
sqlplus -s -L $ORALOGIN/$ORAPASSWD@$DB_NAME <<EOF01A
set heading off feedback off
set newpage none
set line  132
spool $LOG_TMP
select rtrim(ltrim(thread#))||':'||ltrim(rtrim(max(sequence#)))||':'
from v\$log where ARCHIVED='YES' group by  thread#;
spool off
exit;
EOF01A
elif [ "${DB_NAME}" = "IFXP01" ] ; then
ORALOGIN=dbsnmp
sqlplus -s -L $ORALOGIN/$ORAPASSWD@$DB_NAME <<EOF01A
set heading off feedback off
set newpage none
set line  132
spool $LOG_TMP
select rtrim(ltrim(thread#))||':'||ltrim(rtrim(max(sequence#)))||':'
from v\$log where ARCHIVED='YES' group by  thread#;
spool off
exit;
EOF01A
elif [ "${DB_NAME}" = "PGPSP01" ] ; then
ORALOGIN=dbsnmp
sqlplus -s -L $ORALOGIN/$ORAPASSWD@$DB_NAME <<EOF01A
set heading off feedback off
set newpage none
set line  132
spool $LOG_TMP
select rtrim(ltrim(thread#))||':'||ltrim(rtrim(max(sequence#)))||':'
from v\$log where ARCHIVED='YES' group by  thread#;
spool off
exit;
EOF01A
else
sqlplus -s -L $ORALOGIN/$ORAPASSWD@$DB_NAME as sysdba <<EOF01A
set heading off feedback off
set newpage none
set line  132
spool $LOG_TMP
select rtrim(ltrim(thread#))||':'||ltrim(rtrim(max(sequence#)))||':'
from v\$log where ARCHIVED='YES' group by  thread#;
spool off
exit;
EOF01A
fi
if [ $? -ne 0 ] ; then
 echo $DB_NAME ':' $DB_STDBY ':' 'Error Primary' >> $LOG_TMP1
else
for standbyln in `cat $LOG_TMP`
do
   prim_thread=`echo $standbyln | awk -F: '{print $1}' -`
   prim_sequence=`echo $standbyln | awk -F: '{print $2}' -`
sqlplus -s -L $ORALOGIN/$ORAPASSWD@$DB_STDBY as sysdba <<EOF01B
set heading off feedback off
set linesize 150
set pagesize 200
column LOB format a8 head 'LOB#'
column APP format a35 head 'APP#'
column pthread format a10 head 'Thread#'
column dname format a20 head 'Primary'
column sname format a20 head 'Standby'
column pseq format a10 head 'Prim_Seq#'
column dseq format 999999999 head 'Stdby_Seq#'
column diff format 999999999 head 'Diff'
spool $LOG_TMP1 append
select '$LOB' LOB,'$APP' APP,'$DB_NAME' dname,'$DB_STDBY' sname,'$prim_thread' pthread,rtrim('$prim_sequence') pseq,max(sequence#) dseq ,
'$prim_sequence' -  max(sequence#) diff from v\$archived_log stdby where applied='YES' and  stdby.thread#='$prim_thread'
group by '$LOB',':','$APP',':','$DB_NAME',':','$DB_STDBY',':','$prim_thread',':','$prim_sequence';
spool off
exit;
EOF01B
if [ $? -ne 0 ] ; then
 echo $DB_NAME ':' $DB_STDBY ':' 'Error Connecting Standby' >> $LOG_TMP1
fi
done
fi
     fi;;
  esac
done
echo " ------------------------------------------------------------------------------------------------"> $LOG_FILE
echo -e "| Oracle Standby Check Report \t \t  `date '+%Y/%m/%d %H:%M:%S'`           |">> $LOG_FILE
echo " ------------------------------------------------------------------------------------------------">> $LOG_FILE
echo -e "LOB  \tAPP                   \t   Primary       \t Standby     \t  Thread# \t Prim.Seq# \t Stdby.Seq# \t Diff* \n">> $LOG_FILE
cat $LOG_TMP1 >> $LOG_FILE
echo " =================================================================================================">> $LOG_FILE
echo Script Name :  $SCRIPTDIR/$FILENAME >> $LOG_FILE
if [ $InValidDB -gt 0 ]; then
   echo 'Use Following Sql Scripts For More Details : ' >> $LOG_FILE
   echo '   1) /opt/oracle/DBATOOLS/sql/dataguard/show_phy_apply.sql' >> $LOG_FILE
   mail -s "Oracle:Report Standby Database Check" $MAILLIST < $LOG_FILE
 else
   mail -s "ORA Exception REPORT: Standby Database Check" $MAILLIST1 -c $MAILLIST < $LOG_FILE
fi
exit 0


pbpmccdbaora1:/home/oracle>>cat /opt/oracle/DBAENGG/dbmap/oratab.stdby
PMCRCS20:BCRRCS20:PB:0R4JpmPB:Y:RACS2
BCRGDW20:PMCGDW20:PB:0R4JpmPB:Y:GCRS
PMCBVD20:BCRBVD20:PB:0R4JpmPB:Y:GENEVA
PMCPRV20:BCRPRV20:PB:0R4JpmPB:Y:CSR_DASHBOARD
PMCPRV30:BCRPRV30:PB:0R4JpmPB:Y:CSR_DASHBOARD
PMCMRG20:BCRMRG20:PB:0R4JpmPB:Y:GPB_MARGIN
PMCPBS20:BCRPBS20:PB:0R4JpmPB:Y:GENEVA_PBSD
PCRODS20:PMCODS20:PB:0R4JpmPB:Y:GENEVA
PCRSBL20:BMCSBL20:PB:0R4JpmPB:Y:SBL
PCRSBL30:BMCSBL30:PB:0R4JpmPB:Y:SBL
PMCSBL20:BCRSBL20:PB:0R4JpmPB:Y:SBL
<primary>:<standby>:<LOB>:<sys/dbsnmp password>:<Applicationname>




cpu session

column program format a30
column username format a18
--set pagesize 0
Prompt ===========================================================
Prompt ** Sessions with CPU utlzn, Disk io details
Prompt ** file: sess_cpu.sql tms 10/jun/99
Prompt ===========================================================
set heading on
select
 p.spid thread,
 s.sid,
 substr(s.terminal,1,15) terminal ,
 substr(s.username,1,18) UserName ,
 decode(nvl(p.background,0),1,bg.description, s.program ) program,
 ss.value * 100   CPU_secs,
 physical_reads disk_io
 from v$process p,
        v$session s,
        v$sesstat ss,
        v$sess_io si,
        v$bgprocess bg
 where s.paddr=p.addr
   and ss.sid=s.sid
   and ss.statistic#=12
   and si.sid=s.sid
   and bg.paddr(+)=p.addr
   order by ss.value desc
/

asmsize


pbpmccracora1:/home/oracle>>cat .profile
# Purpose : Generic environment variable setup
# Created : 04-15-2007
########################################################
# Set up the Unix variables:
export EDITOR=vi
umask 022
set -o vi
alias ls='/bin/ls'
alias pso='ps -fu oracle'
alias asmsize='/opt/oracle/DBATOOLS/sql/asm/get_asmgroup_info.ksh'
# Setup Prompt
PS1=`uname -n`:'$PWD>>'

# . $HOME/.setup_ASM.ksh
. $HOME/.setup_PMCRCS20.ksh
# Setup all local environment here
export TNS_ADMIN=$ORACLE_HOME/network/admin
export ENGG=/opt/oracle/DBAENGG/scripts
cd /opt/oracle/DBATOOLS/sql/general/
export TZ=EST5EDT
export COLUMNS=200
export SNACS=/opt/oracle/DBAENGG/scripts/CHECK_SNACID/
export AUDIT=/opt/oracle/DBAENGG/scripts/DBAUDIT/
export STANDBY=/opt/oracle/DBAENGG/scripts/STANDBY/
export TBS=/opt/oracle/DBAENGG/scripts/TBSCOLLECT/
export LOAD=/opt/oracle/DBAENGG/loadtest3/
export LOAD2=/opt/oracle/DBAENGG/LOADTEST2/
export LOAD4=/opt/oracle/DBAENGG/dboBENCH/
export ASH=/opt/oracle/DBAENGG/ASHWORK/
export SWING=/opt/oracle/SWINGB/swingbench/bin
if [ -t 0 ]; then
   stty intr ^C
fi
pbpmccracora1:/home/oracle>>ls -l /opt/oracle/DBATOOLS/sql/asm/get_asmgroup_info.ksh
-rwxr-xr-x 1 oracle oinstall 567 Mar 13  2007 /opt/oracle/DBATOOLS/sql/asm/get_asmgroup_info.ksh


pbpmccracora1:/home/oracle>>cat /opt/oracle/DBATOOLS/sql/asm/get_asmgroup_info.ksh
#!/bin/ksh
# It requires the .setup_<<DBANEM>>.ksh file
# so that ASM_SID and ORACLE_HOME of ASM exectable is set up properly ..
# ir read ASM_SID and ASM_HOME variables to make use ..
. ~/.profile
export ORACLE_SID=$ASM_SID
export ORACLE_HOME=$ASM_HOME
echo "======== On HostName: `uname -n` ========\n"
$ORACLE_HOME/bin/sqlplus -s /nolog  << EOF1
connect / as sysdba
 spool /tmp/asm_disks.log
 @/opt/oracle/DBATOOLS/sql/asm/show_asm_diskgroups.sql
 spool off
EOF1
# mail -s "Oracle:MIS ASM-DiskGroup for RMAN Registered db(s) " $MAILLIST < /tmp/asm_disks.log


pbpmccracora1:/home/oracle>>cat /opt/oracle/DBATOOLS/sql/asm/show_asm_diskgroups.sql
col name for a20 head 'ASM_DISK|Name'
col g_name for a12 head 'ASM_Group|Name'
col path for a20 head 'Disk|Path'
col DISK_NUMBER head 'DISK|NUM#'   for 99999
col GROUP_NUMBER head 'Grp|NUM' for 999
col HEADER_STATUS head 'HEADER|STATUS'
col MOUNT_STATUS head 'MOUNT|STATUS' for a8
col TOTAL_MB for 999,999,999
col FREE_MB for 999,999,999
col ALLOC for 99999 head 'Alloc|Unit_KB'
col SECTOR_SIZE head 'Sector|Size'
col Block_Size head 'Block|Size'
col offline_disks head 'Offline|Disks'
set linesize 140
set pagesize 140
PROMPT ***** LIST DISKS with Disk Groups under ASM control  ****
select A.NAME G_NAME, A.GROUP_NUMBER, b.DISK_NUMBER,
b.MOUNT_STATUS, b.HEADER_STATUS,
b.STATE, b.PATH, b.NAME, b.TOTAL_MB, b.FREE_MB, round(b.FREE_MB/b.TOTAL_MB * 100,1) PCT_FREE
from v$asm_disk B, v$asm_diskgroup A
where A.GROUP_NUMBER = B.GROUP_NUMBER
order by A.NAME
/
PROMPT ***** Disk Group Details under ASM control  ****
select NAME g_name, TYPE, TOTAL_MB, FREE_MB, round(FREE_MB/TOTAL_MB * 100,1) PCT_FREE,
BLOCK_SIZE, SECTOR_SIZe, OFFLINE_DISKS, ALLOCATION_UNIT_SIZE/1024 Alloc,
STATE from v$asm_diskgroup
/




ASM COPY TO ANOTHER DISK

There are multiple examples: discussed here
a) Database level copy
b) TBS level copy
c) Data File level copy
d) COnvert command option
===========================================================

****** All Data Files in a DATABASE copy *****
a) First place the database in mount mode
SQL>startup mount ;
Then switch to RMAN ...
Use following rman option to move all datafiles under ASM to new diskgroup.
RMAN> backup as copy database format '+DATADG2' ;
RMAN> switch database to copy ;
*** Next  SQL*plus session:
SQL>startup mount ;
SQL>recover database ;
SQL>alter database open ;

****** Tablespace level copy *****
Go to SQL*Plus Session:
.....................
create tablespace TEST_TBS1 datafile '/opt/oracle/work/TEST_TBS1.dbf' size 50M ;
create user test1 identified by test1 default tablespace test_tbs1 ;
STEP(1)
SQL> select file#, name from v$datafile ;
 FILE# NAME
------ --------------------------------------------------
     1 +DATADG1/dwhwfl20/datafile/system.256.587509305
     2 +DATADG1/dwhwfl20/datafile/undotbs1.258.587509305
     3 +DATADG1/dwhwfl20/datafile/sysaux.257.587509305
     4 +DATADG1/dwhwfl20/datafile/users.259.587509305
     5 +DATADG1/dwhwfl20/datafile/undotbs2.264.587509403
     6 +DATADG1/dwhwfl20/datafile/bearmon.277.587648019
     7 +DATADG1/dwhwfl20/datafile/tsproc.278.587649873
     8 +DATADG1/dwhwfl20/datafile/tsperf.279.587650003
     9 /opt/oracle/work/TEST_TBS1.dbf
9 rows selected.
col ts# for 9999
col file# for 99999
col TBS_NAME for a20
col FILE_NAME for a70
select  a.ts#, a.name TBS_NAME, b.FILE#, b.name FILE_NAME from v$tablespace a,  v$datafile b
where b.ts# = a.ts#
/
******************** Go to RMAN to copy the file ******** (without control file) *********
********** Below example is basically copy at TBS level .........................) *********
whibmdev51-h:/opt/oracle>>rman target /
Recovery Manager: Release 10.2.0.1.0 - Production on Sun Apr 16 21:57:06 2006
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
connected to target database: DWHWFL20 (DBID=2046196098)
RMAN> sql "alter tablespace TEST_TBS1 offline" ;
using target database control file instead of recovery catalog
sql statement: alter tablespace TEST_TBS1 offline
RMAN> backup as copy tablespace TEST_TBS1 format '+DATADG1' ;
Starting backup at 16-APR-06
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=117 instance=DWHWFL21 devtype=DISK
channel ORA_DISK_1: starting datafile copy
input datafile fno=00009 name=/opt/oracle/work/TEST_TBS1.dbf
output filename=+DATADG1/dwhwfl20/datafile/test_tbs1.280.587944749 tag=TAG20060416T215908 recid=1 stamp=587944749
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 16-APR-06
RMAN> switch datafile 9 to copy ;
using target database control file instead of recovery catalog
datafile 9 switched to datafile copy "+DATADG1/dwhwfl20/datafile/test_tbs1.280.587944749"

RMAN> report schema ;
Report of database schema
List of Permanent Datafiles
===========================
File Size(MB) Tablespace           RB segs Datafile Name
---- -------- -------------------- ------- ------------------------
1    490      SYSTEM               ***     +DATADG1/dwhwfl20/datafile/system.256.587509305
2    35       UNDOTBS1             ***     +DATADG1/dwhwfl20/datafile/undotbs1.258.587509305
3    380      SYSAUX               ***     +DATADG1/dwhwfl20/datafile/sysaux.257.587509305
4    5        USERS                ***     +DATADG1/dwhwfl20/datafile/users.259.587509305
5    25       UNDOTBS2             ***     +DATADG1/dwhwfl20/datafile/undotbs2.264.587509403
6    500      BEARMON              ***     +DATADG1/dwhwfl20/datafile/bearmon.277.587648019
7    5120     TSPROC               ***     +DATADG1/dwhwfl20/datafile/tsproc.278.587649873
8    5120     TSPERF               ***     +DATADG1/dwhwfl20/datafile/tsperf.279.587650003
9    0        TEST_TBS1            ***     +DATADG1/dwhwfl20/datafile/test_tbs1.280.587944749
List of Temporary Files
=======================
File Size(MB) Tablespace           Maxsize(MB) Tempfile Name
---- -------- -------------------- ----------- --------------------
1    22       TEMP                 32767       +DATADG1/dwhwfl20/tempfile/temp.263.587509383
RMAN>
***** Now come out of
SQL> connect / as sysdba
Connected.
SQL>  alter tablespace TEST_TBS1 online ;
Tablespace altered.
SQL> select * from test1.ny1 ;

************************ NEXT EXAMPLE (Sepcific Data File) *********************
2nd  EXAMPLE ..SPECIFIC DATAFILE you want to move to another disk group .. *****
a) OFFLINE the tablespace
SQL>  alter tablespace TEST_TBS1 offline ;
Tablespace altered.

b) In RMAN do the following .. !!
RMAN> COPY DATAFILE '+DATADG1/dwhwfl20/datafile/test_tbs1.280.587944749' TO '+DATADG2';
Starting backup at 16-APR-06
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=127 instance=DWHWFL21 devtype=DISK
channel ORA_DISK_1: starting datafile copy
input datafile fno=00009 name=+DATADG1/dwhwfl20/datafile/test_tbs1.280.587944749
output filename=+DATADG2/dwhwfl20/datafile/test_tbs1.256.587946275 tag=TAG20060416T222434 recid=3 stamp=587946275
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 16-APR-06
RMAN>
c) Switch to SQLPLUS and Rename the file to point to new location.
SQL>  ALTER DATABASE RENAME FILE '+DATADG1/dwhwfl20/datafile/test_tbs1.280.587944749' TO
  2  '+DATADG2/dwhwfl20/datafile/test_tbs1.256.587946275' ;
Database altered.
SQL> RECOVER DATAFILE '+DATADG2/dwhwfl20/datafile/test_tbs1.256.587946275' ;
Media recovery complete.
SQL>
SQL> alter tablespace test_tbs1 online ;
Tablespace altered.
SQL>  select * from test1.ny1 ;

SQL> select file#, name from v$datafile ;
     FILE# NAME
---------- --------------------------------------------------
         1 +DATADG1/dwhwfl20/datafile/system.256.587509305
         2 +DATADG1/dwhwfl20/datafile/undotbs1.258.587509305
         3 +DATADG1/dwhwfl20/datafile/sysaux.257.587509305
         4 +DATADG1/dwhwfl20/datafile/users.259.587509305
         5 +DATADG1/dwhwfl20/datafile/undotbs2.264.587509403
         6 +DATADG1/dwhwfl20/datafile/bearmon.277.587648019
         7 +DATADG1/dwhwfl20/datafile/tsproc.278.587649873
         8 +DATADG1/dwhwfl20/datafile/tsperf.279.587650003
         9 +DATADG2/dwhwfl20/datafile/test_tbs1.256.587946275
9 rows selected.

*******************************  NOW to ASMCMD *************************
NOW switch to ASM instance
whibmdev51-h:/opt/oracle>>asmcmd
ASMCMD> cd +DATADG1/DWHWFL20/DATAFILE
ASMCMD> ls -ltr
Type      Redund  Striped  Time             Sys  Name
DATAFILE  UNPROT  COARSE   APR 12 14:00:00  Y    UNDOTBS1.258.587509305
DATAFILE  UNPROT  COARSE   APR 12 14:00:00  Y    UNDOTBS2.264.587509403
DATAFILE  UNPROT  COARSE   APR 12 14:00:00  Y    USERS.259.587509305
DATAFILE  UNPROT  COARSE   APR 13 11:00:00  Y    BEARMON.277.587648019
DATAFILE  UNPROT  COARSE   APR 13 12:00:00  Y    TSPERF.279.587650003
DATAFILE  UNPROT  COARSE   APR 13 12:00:00  Y    TSPROC.278.587649873
DATAFILE  UNPROT  COARSE   APR 15 22:00:00  Y    SYSTEM.256.587509305
DATAFILE  UNPROT  COARSE   APR 16 20:00:00  Y    SYSAUX.257.587509305
ASMCMD>


==========================================================================
Copying Tablespaces From ASM With CONVERT TABLESPACE: Example
==========================================================================
In this example, the datafiles of tablespace tbs_2 (stored in ASM) are
copied from an ASM storage location to non-ASM storage using the CONVERT TABLESPACE command:
RMAN> convert tablespace tbs_2 format '/tmp/tbs_2_%U.df';
Starting backup at 03-JUN-05
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=20 devtype=DISK
channel ORA_DISK_1: starting datafile conversion
input datafile fno=00006 name=+DATAFILE/tbs_21.f
converted datafile=/tmp/tbs_2_data_D-L2_I-2786301554_TS-TBS_2_FNO-6_11gm2fq9.df
channel ORA_DISK_1: datafile conversion complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting datafile conversion
input datafile fno=00007 name=+DATAFILE/tbs_22.f
converted datafile=/tmp/tbs_2_data_D-L2_I-2786301554_TS-TBS_2_FNO-7_12gm2fqa.df
channel ORA_DISK_1: datafile conversion complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting datafile conversion
input datafile fno=00019 name=+DATAFILE/tbs_25.f
converted datafile=/tmp/tbs_2_data_D-L2_I-2786301554_TS-TBS_2_FNO-19_13gm2fqb.df
channel ORA_DISK_1: datafile conversion complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting datafile conversion
input datafile fno=00009 name=+DATAFILE/tbs_23.f
converted datafile=/tmp/tbs_2_data_D-L2_I-2786301554_TS-TBS_2_FNO-9_14gm2fqc.df
channel ORA_DISK_1: datafile conversion complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting datafile conversion
input datafile fno=00010 name=+DATAFILE/tbs_24.f
converted datafile=/tmp/tbs_2_data_D-L2_I-2786301554_TS-TBS_2_FNO-10_15gm2fqd.df
channel ORA_DISK_1: datafile conversion complete, elapsed time: 00:00:01
Finished backup at 03-JUN-05

ANOTHER example:
Copying Datafiles From ASM Using CONVERT DATAFILE: Example
This example illustrates copying a single datafile out of ASM storage using CONVERT DATAFILE:
RMAN> convert datafile "+DATAFILE/tbs_21.f" format "/tmp/conv_df_%U";
Starting backup at 03-JUN-05
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile conversion
input filename=+DATAFILE/tbs_21.f
converted datafile=/tmp/conv_df_data_D-L2_I-2786301554_TS-TBS_2_FNO-6_18gm2hft
channel ORA_DISK_1: datafile conversion complete, elapsed time: 00:00:02
Finished backup at 03-JUN-05

About Stats


You might encounter scenarios where your queries execution plan changed. Eg : On Monday your query was running in less than a minute and on Wednesday your query started running for an hour (The query was exactly the same, query criteria (Including bind variables) were the same and the table data has not changed too much). One of the reasons for the query performing badly now, could be that the execution plan has changed.
One of the reasons that the execution plan has changed, could be that the statistics on the table has changed (Between monday and wednesday, gather stats could have run on the table). Wont it be nice if you were able to restore the old stats on the table, which was helping the query run faster ?
There might be a way in Oracle 10g and Higher.
Whenever oracle collects stats on a table using gather_table_stats, oracle stores away the existing stats on the table before updating the table with the newly collected stats. So there is also a mechanism to restore this stats that oracle backed up.
We can use the following steps to restore this stats (Which was good)
    * Let us say that SCOTT.DEPT is the table in question
    * First of all, find out, when the table stats were modified
      select stats_update_time from user_tab_stats_history where table_name = ‘DEPT’;
    * Create a stats table in the schema of the table owner (This will serve as the temporary holder of the current statistics, if you ever have to restore this)
      exec dbms_stats.create_stat_table ( -
      ‘SCOTT’, -
      ’stattab_new’);
    * Export the existing table statistics to this temporary table (stattab_new)
      exec dbms_stats.export_table_stats ( -
      ‘SCOTT’, -
      ‘DEPT’, -
      null, -
      ’stattab_new’, -
      null, -
      true, -
      ‘SCOTT’);
    * Restore the old stats, which used to give you a better execution plan
      exec dbms_stats.restore_table_stats ( -
      ‘SCOTT’, -
      ‘DEPT’, -
      ‘21-JAN-09 11.00.00.000000 AM -05:00');
The third argument you give is the time upto which you want to restore the stats. Once you query the table user_tab_stats_history, determine a time when the stats would have been the good statistics (So pick a time in between the analyze which had the good stats and the analyze that had the bad stats). Use that time as the third argument.
Now if you get the query to reparse it should pick up the updated good statistics, use the good execution plan and execute with good performance.
Note : Try and perfect this technique on test databases before running this on production.

===========
Statistics History
One of the complications that can occur during optimizer statistics collection is changed execution plans—that is, the old optimization works fine until the statistics are collected, but thereafter, the queries suddenly go awry due to bad plans generated by the newly collected statistics. This is a not infrequent problem.
To protect against such mishaps, the statistics collection saves the present statistics before gathering the new ones. In the event of a problem, you can always go back to the old statistics, or at least examine the differences between them to get a handle on the problem.
For example, let's imagine that at 10:00PM on May 31 the statistics collection job on the table REVENUE is run, and that subsequently the queries perform badly. The old statistics are saved by Oracle, which you can retrieve by issuing:
begin
   dbms_stats.restore_table_stats (
      'ARUP',
      'REVENUE',
      '31-MAY-04 10.00.00.000000000 PM -04:00');
end;
This command restores the statistics as of 10:00PM of May 31, given in the TIMESTAMP datatype. You just immediately undid the changes made by the new statistics gathering program.
The length of the period that you can restore is determined by the retention parameter. To check the current retention, use the query:
SQL> select DBMS_STATS.GET_STATS_HISTORY_RETENTION from dual;
GET_STATS_HISTORY_RETENTION
---------------------------
                         31
which in this case shows that 31 days worth of statistics can be saved but not guaranteed. To discover the exact time and date to which the statistics extend, simply use the query:
SQL> select DBMS_STATS.GET_STATS_HISTORY_AVAILABILITY from dual;
GET_STATS_HISTORY_AVAILABILITY
---------------------------------------------------------------------
17-MAY-04 03.21.33.594053000 PM -04:00
which reveals that the oldest available statistics date to 3:21AM on May 17.
You can set the retention period to a different value by executing a built-in function. For example, to set it to 45 days, issue:
execute DBMS_STATS.ALTER_STATS_HISTORY_RETENTION (45)

AO_backupevery hour

##00 * * * * /backup/oracle/PMCRCS20/rman/rman_AO_bkp_EveryHr.sh /tmp/rman_AO_bkp_EveryHr.log 2>&1

pbpmccracora1:/home/oracle>>cat /backup/oracle/PMCRCS20/rman/rman_AO_bkp_EveryHr.sh
export ORACLE_SID=PMCRCS21; export ORACLE_HOME=/home/oracle/PMCRCS20/product/dbms/10G; export PATH=$ORACLE_HOME/bin:$PATH
NOW=$(date +"%m-%d-%T")
rman target / cmdfile /backup/oracle/PMCRCS20/rman/rman_AO_bkp_EveryHr.rcv log /backup/oracle/PMCRCS20/rman/log/rman_AO_bkp_$NOW.log
pbpmccracora1:/home/oracle>>cat /backup/oracle/PMCRCS20/rman/rman_AO_bkp_EveryHr.rcv
run
{
CONFIGURE RETENTION POLICY TO REDUNDANCY 1 ;
CONFIGURE BACKUP OPTIMIZATION ON ;
CONFIGURE CONTROLFILE AUTOBACKUP OFF ;
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 ;
CONFIGURE CHANNEL DEVICE TYPE DISK MAXPIECESIZE 8G ;
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1;
SHOW ALL;
crosscheck archivelog from time 'sysdate - 4' ;
BACKUP ARCHIVELOG ALL DELETE INPUT
format '/backup/oracle/PMCRCS20/rman/%d_%s_%T_%c_ARCH' tag "ARCH_ONLY" ;
}

tablespace_report_databases

00 01,06,18,23 * * * /home/oracle/DBAENGG/scripts/ofr_tbs.ksh >/tmp/tbs_space_check.log

pbpmccdbaora2:/opt/oracle/DBATOOLS/sql/general>>cat /home/oracle/DBAENGG/scripts/ofr_tbs.ksh
#!/bin/ksh
# set -vx
# Placed this script to collect critical TBS SPACE USAGE information
. $HOME/.setup_PMCGRD20.ksh
# export TNS_ADMIN=/opt/oracle/PWHGRD20/product/dbms/10.2.0/network/admin
# MAILLIST="nitesh.x.sharma@jpmchase.com"
# MAILLIST="sathish.panjala@jpmchase.com"
MAILLIST1="ib.core.svcs.oracle.mbox@jpmorgan.com"
MAILLIST="ib_global_dba@jpmchase.com"

DIR=/opt/oracle/DBAENGG/scripts
OUTTBS=$DIR/tbs_space.dat
log1=$DIR/tbs_spacemon_run_all.log
>$OUTTBS
echo "=== Oracle Tablespace space usage information  ====" > $log1
echo '**  Run Date: ' `date` >> $log1
echo " -----------------------------------------------------------------------------------------------------------------">> $log1
echo " Database         Tablespace                      Size (MB)    Free (MB)   Free %" >> $log1
echo " -----------------------------------------------------------------------------------------------------------------">> $log1
v_gdb=0
v_bdb=0
for ORATABLN in `cat /opt/oracle/DBAENGG/dbmap/oratab.database.tbs`
do
dbname=`echo $ORATABLN | awk -F: '{print $1}' -`
abc=$(sqlplus -s -L dbsnmp/umagellan1#@$dbname <<EOF
spool $DIR/tbs_spacemon.out
@$DIR/tbs_mon.sql
spool off
exit;
EOF)
if [ $? -ne 0 ] ; then
 NOW=`date '+%H:%M:%S'`
 echo 'Check At ' $NOW ':' 'Error Connect to ' $dbname' Database ..' >> $log1
 echo '** Error info : '$abc '..' >> $log1
 v_bdb=`expr $v_bdb + 1`
 else
# cat $DIR/tbs_spacemon.out >> $OUTTBS
 cat $DIR/tbs_spacemon.out >> $log1
 v_gdb=`expr $v_gdb + 1`
fi
done
echo " -------------------------------------------------------------------------">> $log1
echo "| Total Database Scanned:" $v_gdb '                                             |' >>  $log1
echo "| Total Database Not Scanned:" $v_bdb '                                                  |' >>  $log1
echo " -------------------------------------------------------------------------">> $log1
# send MAIL
mail -s "ORA Exception REPORT: Tablespace Space Check" $MAILLIST1 -c $MAILLIST < $log1


pbpmccdbaora2:/opt/oracle/DBATOOLS/sql/general>>cat /opt/oracle/DBAENGG/dbmap/oratab.database.tbs
CVPROD
NEOTKP
NEOKRP
NEOKRP-krlneop2
krneop
HKNEO
CERDCORP
NEOHKP
SGNEOP
INNEOP
NEOTWP
TWNEOU

pbpmccdbaora2:/opt/oracle/DBATOOLS/sql/general>>cat /opt/oracle/DBAENGG/scripts/tbs_mon.sql
set feedback off
set heading off
set verify off
set trimspool on
set linesize 150
set pagesize 500
col tablespace_name FORMAT A25
col "Size (MB)" for a12
col "Max Size(MB)" for a12
column LOB format a8 head 'LOB#'
column APP format a35 head 'APP#'
column pthread format a10 head 'Thread#'
column dname format a20 head 'Primary'
column sname format a20 head 'Standby'
break on name
SELECT d.name, F.tablespace_name "Tablespace", TO_CHAR(NVL(a.bytes / 1024 / 1024, 0),'99G999G990') "Size (MB)", TO_CHAR(NVL(NVL(f.bytes, 0),0)/1024/1024, '99G999G990') "Free (MB)", TO_CHAR(NVL((NVL(f.bytes, 0)) / a.bytes * 100, 0), '990D00') "Free %" FROM (select tablespace_name, sum(bytes) bytes from dba_data_files group by tablespace_name) a, (select tablespace_name, sum(bytes) bytes from dba_free_space group by tablespace_name) f, v$database d WHERE f.tablespace_name=a.tablespace_name and F.tablespace_name  in (select b.tablespace_name
 from (select tablespace_name, sum(bytes)/1024/1024 free
        from dba_free_space
       group by tablespace_name) a,
     (select tablespace_name, sum(bytes)/1024/1024 total,
             sum(decode(autoextensible,'YES',maxbytes,bytes))/1024/1024 maxgrowth
        from dba_data_files
       group by tablespace_name) b
 where a.tablespace_name(+) = b.tablespace_name and (((total-nvl(free,0))/maxgrowth)*100) >85
and a.tablespace_name not like 'UNDO%' and a.tablespace_name not like 'TEMP%'
and (((total-nvl(free,0))/total)*100) >85);