Thursday 20 February 2014

Manually Corrupting Oracle Data Block

Manually Corrupting Oracle Data Block

When we’re practicing the backup and recovery scenarios, we come-up with the block corruption recovery cases. So how to make the blocks to get corrupt? And how the recoveries can be done? And here is the solution for how to make the Oracle data blocks to get corrupted.
At the same time, this will also help us to recover the corrupted blocks from the production databases.

Before we begin:

This is only for the learning, practicing, and educational purpose, please do not try these methods on the production, testing, and/or development databases.
The way we corrupt the Oracle data blocks that allows the learners’ to practice the scenarios on backup and recoveries. That makes when the actual database block level corruption has occurred, we will be in a position to recover in the block level.
1. For the purpose of this practice, will create a separate tablespace and a new schema user.
a. Create a Tablespace say “test_corrupt”
SQL> create tablespace test_corrupt
2 datafile ‘/u01/app/oracle/oradata/orcl/test_corrupt01.dbf’ size 10m;
Tablespace created.
b. Create an user say “test” and assign quota to the “test_corrupt” tablespace
SQL> create user test identified by test123
2 default tablespace test_corrupt
3 quota unlimited on test_corrupt;
User created.
c. Grant the necessary privileges
SQL> grant create session, resource to test;
Grant succeeded.
2. Create a table as test user
SQL> conn test/test123
Connected.
SQL> create table emp(eno number(7))
2 tablespace test_corrupt;
Table created.
3. Insert some dummy records into the table by writing a simple ‘for loop’ block as shown:
SQL> begin
2 for i in 1..10000
3 loop
4 insert into emp values(i);
5 end loop;
6 end;
8 /
PL/SQL procedure successfully completed.
SQL> select count(*) from emp;
COUNT(*)
———-
10000
4. Take the RMAN backup, either full database or tablespace level backup before corrupting the block.
Connect to the RMAN
$ rman target / catalog scott/tiger@rc
Recovery Manager: Release 11.2.0.1.0 – Production on Mon Mar 28 16:00:39 2011
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1272957858)
connected to recovery catalog database
Report the schema to know about the target database
RMAN> report schema;
Report of database schema for database with db_unique_name ORCL
List of Permanent Datafiles
===========================
File Size(MB) Tablespace RB segs Datafile Name
—- ——– ——————– ——- ————————
1 690 SYSTEM YES /u01/app/oracle/oradata/orcl/system01.dbf
2 540 SYSAUX NO /u01/app/oracle/oradata/orcl/sysaux01.dbf
3 80 UNDOTBS1 YES /u01/app/oracle/oradata/orcl/undotbs01.dbf
4 10 TEST_CORRUPT NO /u01/app/oracle/oradata/orcl/test_corrupt01.dbf
6 20 DEF_PERM NO /u01/app/oracle/oradata/orcl/def_perm01.dbf
List of Temporary Files
=======================
File Size(MB) Tablespace Maxsize(MB) Tempfile Name
—- ——– ——————– ———– ——————–
1 33 TEMP 32767 /u01/app/oracle/oradata/orcl/temp01.dbf
Take the backup of the tablespace
RMAN> backup tablespace test_corrupt;
Starting backup at 28-MAR-11
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00004 name=/u01/app/oracle/oradata/orcl/test_corrupt01.dbf
channel ORA_DISK_1: starting piece 1 at 28-MAR-11
channel ORA_DISK_1: finished piece 1 at 28-MAR-11
piece handle=/backup/rman_backups/rman_ORCL_0um8c6ve tag=TAG20110328T161437 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 28-MAR-11
5. Take the tablespace offline so that we can make changes to the datafile.
SQL> conn / as sysdba
connected
SQL> alter tablespace test1 offline;
Tablespace altered.
6. To corrupt a datafile, there are many freeware editors available. Here we are using UltraEdit GUI editor (Available for both Windows and Linux Versions) to make changes in the datafile.
Download from the websites and install it. Once the installation has been completed, the files can be edited by using UltraEdit with the help of executable called uex
Open the datafile “/u01/app/oracle/oradata/orcl/test_corrupt01.dbf” using UltraEdit
$ uex /u01/app/oracle/oradata/orcl/test_corrupt01.dbf
The screen will look as like below:
Do not try to edit the file in the beginning, because those portion of the file will contain the datafile header information. Modifying the header file will corrupt the whole file itself.
So, scroll down to find out the blocks where the data is present, try to type some blunders in the space wherever the mouse pointer is blinking. Once the change has been completed, save the file.
7. After corrupting the blocks, bring back the tablespace to online
SQL> alter tablespace test online;
Tablespace altered.
You can notice that Oracle doesn’t through any errors when we brought tablespace online, because we had not modified the datafile header. If in case we had done, then that will through an errors while bringing back the tablespace to online. Oracle will through an error only when it tries to access the data from corrupted blocks.
8. To query the data, connect to the “test” user and query the table “emp”
SQL> conn test/test123
Connected
SQL> select * from emp;
ENO
———-
661
662
663
664
7903
7904
7905
ERROR:
ORA-01578: ORACLE data block corrupted (file # 4, block # 142)
ORA-01110: data file 4: ‘/u01/app/oracle/oradata/orcl/test_corrupt01.dbf’
4605 rows selected.
The query returns 4605 records and then complains of block corruption in file 4, and the block numbered 142 is being reported as corrupt.
This is the methods to be followed in order to corrupt the Oracle data blocks.
Lets us see, how to identify the corrupted blocks and recover them.
9. Let us see what are the blocks are corrupted in “test_corrupt01.dbf” datafile by running dbv utility.
$ which dbv
/u01/app/oracle/product/11.2.0/dbhome_1/bin/dbv
$ dbv file=/u01/app/oracle/oradata/orcl/test_corrupt01.dbf
DBVERIFY: Release 11.2.0.1.0 – Production on Mon Mar 28 15:51:01 2011
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
DBVERIFY – Verification starting : FILE = /u01/app/oracle/oradata/orcl/test_corrupt01.dbf
Page 142 is marked corrupt
Corrupt block relative dba: 0x0100008e (file 4, block 142)
Bad check value found during dbv:
Data in bad block:
type: 6 format: 2 rdba: 0x0100008e
last change scn: 0×0000.00134653 seq: 0×1 flg: 0×06
spare1: 0×0 spare2: 0×0 spare3: 0×0
consistency value in tail: 0×46530601
check value in block header: 0xe6bd
computed block checksum: 0xcc0a
DBVERIFY – Verification complete
Total Pages Examined : 1280
Total Pages Processed (Data) : 19
Total Pages Failing (Data) : 0
Total Pages Processed (Index): 0
Total Pages Failing (Index): 0
Total Pages Processed (Other): 131
Total Pages Processed (Seg) : 0
Total Pages Failing (Seg) : 0
Total Pages Empty : 1129
Total Pages Marked Corrupt : 1
Total Pages Influx : 0
Total Pages Encrypted : 0
Highest block SCN : 1263187 (0.1263187)
This utility scans all the blocks in a given datafile and outputs the corrupt blocks. In this case, there was only one block marked as corrupt. Make a note of all the corrupted blocks as we need to recover them to previous state.
10. Start RMAN session and recover all the corrupted blocks.
The beauty of RMAN is that it recovers only corrupted blocks and we need to recover only those corrupt blocks instead of entire datafile.
$ rman target / catalog scott/tiger@rc
Recovery Manager: Release 11.2.0.1.0 – Production on Mon Mar 28 16:00:39 2011
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1272957858)
connected to recovery catalog database
Report the Schema to know the status of target database, gathers the information from control file
RMAN> report schema;
Report of database schema for database with db_unique_name ORCL
List of Permanent Datafiles
===========================
File Size(MB) Tablespace RB segs Datafile Name
—- ——– ——————– ——- ————————
1 690 SYSTEM YES /u01/app/oracle/oradata/orcl/system01.dbf
2 540 SYSAUX NO /u01/app/oracle/oradata/orcl/sysaux01.dbf
3 80 UNDOTBS1 YES /u01/app/oracle/oradata/orcl/undotbs01.dbf
4 10 TEST_CORRUPT NO /u01/app/oracle/oradata/orcl/test_corrupt01.dbf
6 20 DEF_PERM NO /u01/app/oracle/oradata/orcl/def_perm01.dbf
List of Temporary Files
=======================
File Size(MB) Tablespace Maxsize(MB) Tempfile Name
—- ——– ——————– ———– ——————–
1 33 TEMP 32767 /u01/app/oracle/oradata/orcl/temp01.dbf
11. Recover the corrupted blocks using recover command
RMAN> recover datafile 4 block 142;
Starting recover at 28-MAR-11
starting full resync of recovery catalog
full resync complete
using channel ORA_DISK_1
channel ORA_DISK_1: restoring block(s)
channel ORA_DISK_1: specifying block(s) to restore from backup set
restoring blocks of datafile 00004
channel ORA_DISK_1: reading from backup piece /backup/rman_backups/rman_ORCL_0vm8c862
channel ORA_DISK_1: piece handle=/backup/rman_backups/rman_ORCL_0vm8c862 tag=TAG20110328T163513
channel ORA_DISK_1: restored block(s) from backup piece 1
channel ORA_DISK_1: block restore complete, elapsed time: 00:00:01
starting media recovery
media recovery complete, elapsed time: 00:00:01
Finished recover at 28-MAR-11
RMAN reports success of block recovery command.
12. Will query the table again by logging in to the SQL*Plus:
SQL> conn test/test123
Connected.
SQL> select * from emp order by eno;
ENO
———-
1
2
3
9997
9998
9999
10000
10000 rows selected.
SQL> select count(*) from emp;
COUNT(*)
———-
10000
This will executes successfully and the records will be restored back to the table.

Wednesday 19 February 2014

Easy Clone / Duplicate Oracle 10G database DBCA AND using OEM



Today I had a task to clone or duplicate a Oracle 10g database. Being a novice, I took to google and looked for answers. After some research came with a few ways:
  1. Using EM :: Maintenance :: Deployments :: Clone Database option : The problem with it is its needs "An open database in ARCHIVELOG mode" which I dont have. Hence it requires a restart as mentioned in here. Hence I left this option here itself.
  2. Duplicate database using RMAN : A nice article about it is available in here. I tried it and then spend another 2-3 hours to connect to the newly created database. Thereafter I left this method to.
  3. The command line way : Its all using sqlplus and is pretty technical. Details are available here. It works but looked for an easy alternative.
  4. The DBCA way : This I found to be the easiest and the fastest option but sadly it not available in the top results of google. Looks like its one of the least used way to duplicate an Oracle DB but its the best one. To promote it I write this post.


Here's how its done:

part-I
  1. Start the Database Configuration Assistant (DBCA). Found at All Programs :: Oracle - OracleDb10g_home1 :: Configuration and Migration Tools :: Database Configuration Assistant. The same can be invoked by typing in "dbca" at the command prompt.
  2. On the "Welcome" screen click the "Next" button.
  3. On the "Operations" screen select the "Manage Templates" option and click the "Next" button.
  4. On the "Template Management" screen select the "Create a database template" option and select the "From and existing database (structure as well as data)" sub-option then click the "Next" button.
  5. On the "Source database" screen select the relevant database instance and click the "Next" button.
  6. On the "Template properties" screen enter a suitable name and description for the template, confirm the location for the template files and click the "Next" button.
  7. On the "Location of database related files" screen choose either to maintain the file locations or to convert to OFA structure (recommended) and click the "Finish" button.
  8. On the "Confirmation" screen click the "OK" button.
  9. Wait while the Database Configuration Assistant progress screen gathers information about the source database, backs up the database and creates the template.
now we have to using dbca create new database there we are going to give creating templete:
  1. Depending upon the size of the database it will take some time. For my 8 Gig database, it took like 8 mins. Now we have a template created and we will use to create our new database.
  2. Click on "Next Operation".
  3. Select "Create a Database" option and click "Next".
  4. In "Select a template from the following list to create a database" - select the template name which you provided in Step 6 and click "Next".
  5. Provide the new Service Name for the new database. The SID will automatically be set to the service name entered above. Click "Next".
  6. Let the "Configure the Database with Enterprise Manager" remain checked and "Use Database Control for Database Management" remain checked. Click "Next".
  7. Provide the sys password and click "Next".
  8. Let the "File System" option remain checked unless you want to use ASM or raw for your new database.
  9. Let the "Use Database File Locations from Template remain checked. This is important. Click "Next".
  10. Let the default values for Flash Recover Area remain as they are and click "Next".
  11. Let the "No Scripts to run" remain checked an click "Next".
  12. You can keep the default values for Memory and Sizing over here or change it as per your need and Click "Next".
  13. You are now at the final screen wherein you can all your configurations and verify that they are correct. Clicking next, DBCA will do all your job and your DB should be up and running in next 15-20 mins.
  14. Finally before logging in to the new DB using EM, check the tnsnames.ora and see an entry is created for the new database else add one. You can add a new listenere too in you listener.ora if you want and the do a "lsnrctl reload" to reload the listeners.
  15. Finally do a tnsping on your new database to check all's fine.
  16. Log in using EM and you should have you DB ready in Open mode.
  17. Note all user accounts besides the system account are locked and expired so you need to unlock them to allow users to connect to the new DB.
The whole process took some 30-35 mins and it was all Gui and no scripts or errors. Seem to be the best way out to duplicate an Oracle 10g database :)

differnt using DBCA & OEM:

When we using dbca via cloning it will create all OEM and network it will start all lsnrctl and tnsping all so its good way create cloning

when using cloning OEM via its not create oem automatically and all services we have to start maunally

=============================================================================

RMAN Cloning with OEM





RMAN Cloning Database Using Enterprise Manager

It is possible to clone the database directly from Oracle Enterprise Manager (OEM). By using the OEM cloning feature, this makes it possible to clone the database with some simple mouse clicks without running any RMAN script. Take a backup of the database to the same host using Enterprise Manager.
Open OEM and switch to the Maintenance tab and in the Data Movement section, select the Clone Database link:
Figure 5.1:  Selection in the Maintenance Tab

As a source type, select A running database instance and click Continue:
Figure 5.2:  Choice in Clone Database for Source Type

In this step, provide the location for the working directory. The backup of the database will be stored in this directory. If the working directory needs to be deleted after the clone operation, select the first option. Below that, enter the username and password of the user who owns the Oracle software and click Next.
Figure 5.3:  Clone Database:  Source Working Directory Screen

In next screen, enter the name of the new clone database and click Next:
Figure 5.4:  Clone Database:  Select Destination Screen

Now provide the destination for the database files of the clone database. If a different directory needs to be given, then click the Customize button.  Next, select the network configuration file location and provide passwords for specific users.
Figure 5.5:  Clone Database:  Destination Options Screen

By clicking the Customize button, a different directory for every file of the database will be able to be provided as follows:
Figure 5.6:  Customize Destination Options Screen

Here, Oracle schedules a job to clone the database. This job can be run immediately or be made to run anytime that is desired. Just click Next to make it run immediately.
Figure 5.7:  Clone Database:  Schedule

This is the last page of configuring the clone of the database. Just click the Submit Job button and the clone processwill begin:
Figure 5.8:  Reviewing the Clone Database Process

Here, a message that cloning has been started will appear:
Figure 5.9:  Clone Database Confirmation Message

The status of the clone operation can be viewed by clicking on the View Status button:

Figure 5.10:  Viewing the Status of the Clone Operation

Click on the name of the job to get more information about the job:

Figure 5.11:  Job Information Screen

Click on the status of the job:
Figure 5.12:  Summary Status of the Job

As can be seen, the clone operation has started and this shows that the steps that were performed were successful. Now press F5 to refresh the pages and see that the steps are performing automatically. At last, the final summary will appear and the cloning process ends.
Figure 5.13:  Final Summary of the Clone Process

Now connect to the db2 database and check the status of the instance to be sure that it was cloned successfully.
$ export ORACLE_SID=db2
$ sqlplus "/ as sysdba"
sys@DB2>
select
status
from
 v$instance;
status
------------
OPEN
It can now be seen that the production database was successfully cloned.
Pre & Post Cloning Changes
·         To avoid the confusion between the production and test/development databases, do not set the clone database name as a production database name. 
·         It is not mandatory to have the initialization parameter's value of the cloned instancesimilar to the production instance.  Of course, the source, i.e. the production database, must be in archivelog mode for the duration of the cloning.
·         It is not compulsory to have the cloned instance in archivelog mode because unnecessary archivelog files are generated which consume hard disk space.
·         If the cloned instance crashes and needs to be recovered, it can easily be cloned again from the production database.
·         After the clone, change the system users' passwords, i.e. SYS and SYSTEM, and any critical and application users' passwords. 
·         Disable the jobs which are not required to be run in the cloned instance. 
·         Change any application users' tables from the cloned database that are still referring the production database, i.e. server IP, port details, printer details and such.

Post cloning:

        Here i m facing not tnsname and listener than oem started so i have create manually.

>tnsping orcl1