понедельник, 9 декабря 2013 г.

Recover Tablespace using TSPITR

Sometimes we we lost data in the tablespace due to HARDWARE problems, or just because of mad experiments. There Tablespace point in time recovery(TSPITR) came to help,
it's quite handy because DB will be online during restoration of tablespace.
======================================
Get rid of problematic tablespace:
=====================================
As we restoring default tablespace, we need to change default to another, SYSAUX is on duty today

SQL> alter database default tablespace SYSAUX;

Database altered.

SQL> drop tablespace USERS including contents and datafiles;

Tablespace dropped.

======================================
recover tablespace to the time before the problem:
======================================
RMAN> recover tablespace USERS until time "to_date('09-DEC-2013 11:44:00','DD-MON-YYYY HH24:MI:SS')" AUXILIARY DESTINATION='/u01/recover';

======================================
poststeps
======================================
SQL> alter database default tablespace USERS;
SQL> alter tablespace USERS online;

Backup database via RMAN once restore will be completed!

There can be following problems while using TSPITR:
=======================================
#ISSUE I
########################
During automatic import/export job faced with following:
>>>>>>>>>>>>
~
ORA-01555: snapshot too old: rollback segment number ORA-01555: snapshot too old: rollback segment number with name "" too small
ORA-06512: at "SYS.DBMS_SYS_ERROR", line 95
ORA-06512: at "SYS.KUPW$WORKER", line 8358
~
#SOLUTION I
#########################
Is not easy to detected such solution at all, There component XML DB (XDB) need to be installed to the oracle DB(if not installed before).
So accoiring to the oracle note performed the follwing steps:
============================
catqm.sql XDB_P SYSAUX TEMP
there can be lot of errors related to the object already exists, it can be safely ignored.

#ISSUE II
#########################
ORA-12919: Can not drop the default permanent tablespace

#SOLUTION II
#########################

As we restoring default tablespace need to change it to another, SYSAUX is on duty today
>>>>>>>>>>>>>>>>>>>>>>
alter database default tablespace SYSAUX;
<<<<<<<<<<<<<<<<<<<<<<

then drop tablespace USERS to make fun with TSPITR:
>>>>>>>>>>>>>>>>>>>>>>
drop tablespace USERS including contents and datafiles;
<<<<<<<<<<<<<<<<<<<<<<

пятница, 22 ноября 2013 г.

Recover datafile if corrupted during database open

Recover datafile if corrupted during database open
============
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
ORA-01110: data file 1: '/u02/oradata/TEST/system01.dbf'

Try to recover it via database
=============================
SQL> recover datafile '/u02/oradata/TEST/system01.dbf';
ORA-00283: recovery session canceled due to errors
ORA-01110: data file 1: '/u02/oradata/TEST/system01.dbf'
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
ORA-01110: data file 1: '/u02/oradata/TEST/system01.dbf'

recover via RMAN
=======================
Of cause we taken backup several hours before the corryption happened.
======================

Take datafile offline
======================
SQL> ALTER DATABASE DATAFILE 1 OFFLINE;

Recover corrupted datafile:
===============
RMAN> RESTORE DATAFILE 5;
RMAN> RECOVER DATAFILE 5;

Make datafile online
==============
SQL> ALTER DATABASE DATAFILE 1 ONLINE;
SQL> ALTER DATABASE OPEN;

вторник, 19 ноября 2013 г.

Restore table changes back in time (as of timestamp)

restore table changes back in time
====================================
<><><><><><><><><><><><><><><><><><><><><><><><>
Let me introduce the table called PHONE_BOOK:
<><><><><><><><><><><><><><><><><><><><><><><><>
DESC PHONE_BOOK
Name       Null Type        
---------- ---- -------------
NUM             NUMBER      
NAME            VARCHAR2(100)
PROFESSION      VARCHAR2(100)
PHONE           VARCHAR2(100)
OTHER           VARCHAR2(100)

<><><><><><><><><><><><><><><><><><><><><><><><>
There are following values living
<><><><><><><><><><><><><><><><><><><><><><><><>
select name,other from PHONE_BOOK where  NAME like '%Evge%';
NAME   OTHER
================ ===========
Bashmak Evgeny  222-61-34
Botman Evgeny
Zelipuk Evgeny  299-46-14

<><><><><><><><><><><><><><><><><><><><><><><><>
Let's corrupt selected values
<><><><><><><><><><><><><><><><><><><><><><><><>
update phone_book
set    other = '77777777'
where  NAME like '%Evge%'
/

commit;
<><><><><><><><><><><><><><><><><><><><><><><><>
Haha, values become wrong:
<><><><><><><><><><><><><><><><><><><><><><><><>
select name,other from PHONE_BOOK where  NAME like '%Evge%';

NAME   OTHER
================ ===========
Bashmak Evgeny  77777777
Botman Evgeny  77777777
Zelipuk Evgeny  77777777

<><><><><><><><><><><><><><><><><><><><><><><><>
No worries, there is backup oportunity in the 11g
<><><><><><><><><><><><><><><><><><><><><><><><>
update phone_book
set    (other) =
          (select other
              from   phone_book as of timestamp systimestamp - 1/24 history
              where  history.NAME = phone_book.NAME
             )
/

commit;

<><><><><><><><><><><><><><><><><><><><><><><><>
Values back to the right values
<><><><><><><><><><><><><><><><><><><><><><><><>
select name,other from PHONE_BOOK where  NAME like '%Evge%';
NAME   OTHER
================ ===========
Bashmak Evgeny  222-61-34
Botman Evgeny
Zelipuk Evgeny  299-46-14

понедельник, 18 ноября 2013 г.

Script to start/stop oracle services

#!/bin/bash
echo "startall"
echo "start_db_ln"
echo "stopall"
echo "start_db"
echo "start_dbconsole"
echo "start_ln"
echo -n "Order please : "
read order
case $order in

startall)
echo starting all services           
. /home/oracle/test.env
lsnrctl start TEST
sqlplus "/as sysdba" <startup;
exit;
EOF
emctl start dbconsole
;;
   
stopall)
echo stop all services
. /home/oracle/test.env
sqlplus "/as sysdba" <shutdown immediate;
exit;
EOF
lsnrctl stop TEST
emctl stop dbconsole
;;

   
start_db)
echo startup database only 
. /home/oracle/test.env
sqlplus "/as sysdba" <startup;
exit;
EOF
;;


start_ln)
echo starting listener           
. /home/oracle/test.env
lsnrctl start TEST
;;

start_db_ln)
echo starting listener and database          
. /home/oracle/test.env
lsnrctl start TEST
sqlplus "/as sysdba" <startup;
exit;
EOF
;;

start_dbconsole)
echo starting dbconsole        
. /home/oracle/test.env
emctl start dbconsole
;;
   
*) echo  nothing ordered                   ;;

    esac

пятница, 15 ноября 2013 г.

Create simple table


To create simple table:
=====================
CREATE TABLE phone_book
(
    num                 number,
    Name             varchar2(100),
    Profession           varchar2(100),
    Phone        varchar2(100),
    Other                varchar2(100)
    )
    TABLESPACE USERS
    ;

Where varchar2(100) to keep 100 bytes for the each
and number to avoid any characters except numbers

понедельник, 11 ноября 2013 г.

Template RMAN backup script:
===================
CONFIGURE DEFAULT DEVICE TYPE TO disk;
CONFIGURE DEVICE TYPE disk PARALLELISM 2;
CONFIGURE CHANNEL 1 DEVICE TYPE DISK FORMAT '/u02/RMAN/full_%U';
CONFIGURE CHANNEL 2 DEVICE TYPE DISK FORMAT '/u02/RMAN/full_%U';
run {
backup device type disk tag 'TEST_full' database;
backup device type disk tag 'TEST_full' archivelog all not backed up delete all input;
backup tag 'TEST_full' current controlfile;
}
allocate channel for maintenance type disk;
delete noprompt obsolete device type disk;
release channel;
Enable Archive log for oracle database:
====================
SQL> startup mount
ORACLE instance started.

Total System Global Area  184549376 bytes
Fixed Size                  1300928 bytes
Variable Size             157820480 bytes
Database Buffers           25165824 bytes
Redo Buffers                 262144 bytes
Database mounted.

SQL> alter database archivelog;
Database altered.

SQL> alter database open;
Database altered.
Issie with oracle EM 11g, it refused to start right after installation. Performed following steps to resolve an issue:
1) Dropped repository setup configured during the installation
=============
  • cd $ORACLE_HOME/bin
  • ./emca -deconfig dbcontrol db -repos drop
2) set TZ for the console to avoid issue wit wrong time zone. TZ was set to Moscow time. Changed it via linux GUI to GMT+5 (US/Eastern), as well set local alias for the console as following:
==============
export TZ=US/Eastern

as well added value to the env file. so the environment currently looking like following
>>>>>>>
ORACLE_HOME=/u01/db/11.2.0/dbhome_1
ORACLE_SID=TEST
ORACLE_UNQNAME=TEST
TZ=US/Eastern
export ORACLE_HOME ORACLE_SID ORACLE_UNQNAME TZ
PATH=$PATH:/usr/local/bin:$ORACLE_HOME/bin
export PATH
<<<<<<<<
3) Recreated EM repository
==============
emca -config dbcontrol db -repos create

Repository recreated with no errors. EM started automatically. Login to the Web interface successful.