Rman Script for Full Hot Backup Including SPFILE and Controlfile

10 01 2012

Follow script to use in Rman to perform a full hot backup in an Oracle database, including its controlfile and spfile. Hot backup means that the database doesn’t have to be down. Also it must be running in archivelog mode.

Simply log to rman with your database open and run the following (don’t forget to adjust the paths to your own need).

run { 
 allocate channel ch1 type Disk maxpiecesize = 1900M FORMAT '/path/hbk_%t_set%s_piece%p_dbid%I.rman'; 
 backup incremental level 0 
 tag cold_db_f 
 filesperset 1 
 (database); 
 backup archivelog all delete all input FORMAT '/path/hbk_%t_set%s_piece%p_dbid%I.rman'; 
 backup spfile format '/oradata/orcl/rmanb/spfile_%d_%s_%T_dbid%I.rman'; 
 backup current controlfile format '/path/ctl_%t_dbid%I.rman'; 
}




Performing Switchover on Oracle Standby Database

1 01 2012

My purpose on this post is a quick step-by-step on how to perform a switchover on an Oracle physical standby database.

A switchover consists of the change of roles between the primary and standby database. After the switch, the primary will turn into the standby database and the standby into the primary. This operation usually is performed when the primary database must go under maintenance.

First of all you must have a standby database working properly, receiving and applying the redologs from the primary database. The primary db must be in open status and the standby in mounted mode.

To check the database open mode:

SQL> select open_mode from v$database;

Also make sure there’s no other users connected to the primary database but you (as sysdba). If there’s any active connection to the database, you can perform the switch using the “with session shutdown” command to disconnect all active sessions.

Check the Switchover Status for both databases.

SQL> select switchover_status from v$database;

It’s ok to perform the switchover if the primary database has the status of “SESSIONS ACTIVE” or “TO STANDBY” and for the standby database the status “SESSIONS ACTIVE” or “NOT ALLOWED”.

Below a list with the values for the SWITCHOVER_STATUS column:

  • NOT ALLOWED – Either this is a standby database and the primary database has not been switched first, or this is a primary database and there are no standby databases.
  • SESSIONS ACTIVE – Indicates that there are active SQL sessions attached to the primary or standby database that need to be disconnected before the switchover operation is permitted.
  • SWITCHOVER PENDING – This is a standby database and the primary database switchover request has been received but not processed.
  • SWITCHOVER LATENT – The switchover was in pending mode, but did not complete and went back to the primary database.
  • TO PRIMARY – This is a standby database, with no active sessions, that is allowed to switch over to a primary database.
  • TO STANDBY – This is a primary database, with no active sessions, that is allowed to switch over to a standby database.
  • RECOVERY NEEDED – This is a standby database that has not received the switchover request.

1- Make the primary db the standby db (Do that on the primary database)

SQL> alter database commit to switchover to standby with session shutdown;

2- Shutdown and start the former primary database in standby mode.

SQL> shutdown immediate;
SQL> startup nomount;
SQL> alter database mount standby database;

3- Turn the standby database into the new primary;

SQL> alter database commit to switchover to primary;

4- Shutdown and startup open the new primary database

SQL> shutdown immediate;
SQL> startup;

5- On the new standby database, start the redo log application

SQL> recover managed standby database disconnect from session;

When done with the steps above the switchover_status column from v$database should be in one of the acceptable status as mentioned earlier.

Double check if the archive is being applied correctly

SQL> select sequence#, applied from v$archived_log order by sequence#;

For more information about creating a standby database check out my post by clicking here.

References:

- Performing Switchover in a Data Guard Configuration by Ayyu





Creating Oracle Standby Database using RMAN

29 12 2011

I have created the guide below to help all the ones who need to setup a Standby database using RMAN.

I’m using Oracle 11g Release 2 on two Linux CentOS 6. I’m also assuming that the database binaries are already installed in both servers and working properly.

For a convention I have called the primary db as ‘primary’ adn the standby db as ‘standby’ (nothing more obvious.. hehe).

Steps for the Pimary Database

1- Enable Force Logging

To make sure all data are sent over to the standby database, we need to force oracle to generate log for all changes to the database.

SQL> ALTER DATABASE FORCE LOGGING;

2 – Setup Standby Redo Log

The purpose of standby redo logs is high availability since the actual redo logs are copied to the standby redo logs (in the primary database) to assure that full recovery can be performed in case of db crash.

Make sure the stand by logs have the exactly size of the db redo logs. It’s also recommended that you create at least one more standby redo log then you have normal redo logs. For example if you have 3 redo log groups, create 4 standby redo log groups as follow.SQL

SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 11  '/oradata/prim/redo/sbredo11.log' SIZE 100M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 12  '/oradata/prim/redo/sbredo12.log' SIZE 100M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 13  '/oradata/prim/redo/sbredo13.log' SIZE 100M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 14  '/oradata/prim/redo/sbredo14.log' SIZE 100M;

3 – Create the Password File for the Primary Database

You have to create the password file since the user sys will be used by oracle to access the standby db and also be accessed by the standby db. Make sure the password used for SYS on the primary database is the same used on the standby database.

on the OS prompt line do the following:

# orapwd file=orapwSID password=your_pwd entries=5

4 – Setup instance parameters for the primary database.

You can do that in your PFILE or SPFILE.

Below are the required parameters, i’m not showing the others.

 *.DB_UNIQUE_NAME='primary'
 *.DB_NAME='primary'
 *.INSTANCE_NAME='primary'
 *.LOG_ARCHIVE_CONFIG='DG_CONFIG=(primary,standby)'
 *.LOG_ARCHIVE_DEST_1='LOCATION=/oradata/orcl/archive/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=primary'
 *.LOG_ARCHIVE_DEST_2='SERVICE=standby VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=standby'
 *.LOG_ARCHIVE_DEST_STATE_1='ENABLE'
 *.LOG_ARCHIVE_DEST_STATE_2='ENABLE'
 *.LOG_ARCHIVE_FORMAT='arch_%r_%t_%s.arc'
 *.REMOTE_LOGIN_PASSWORDFILE='EXCLUSIVE'
 *.SERVICE_NAMES='primary'
 *.STANDBY_FILE_MANAGEMENT='AUTO'
 *.DB_FILE_NAME_CONVERT='standby','primary' --Only use this parameter if you have different path in the standby server
 *.FAL_SERVER='standby'
 *.FAL_CLIENT='primary'

Shutdown the database and start it up so the new parameters take effect.

5 – Archive Must be Enabled

If your database is not already running in archivelog, do the following

SQL> shutdown immediate;
SQL> startup mount;
SQL> alter database archivelog;
SQL> alter database open;

6 – Create a Copy of the PFILE

Create a copy of the primary database PFILE. It’ll be used on the standby db with some changes

7 – Backup Primary Database, Controlfile and Archivelogs

It’s necessary to create a full backup of the database with its archivelogs and also a backup of the controlfile for standby.

- Full database backup using RMAN
rman connect target /
run {  allocate channel ch1 type Disk maxpiecesize = 1900M
FORMAT '/oradata/rmanbkp/hbk_%t_set%s_piece%p.rman';
backup full tag cold_db_f
filesperset 1
(database);
release channel ch1;
}

- Controlfile backup for Standby

RMAN> BACKUP CURRENT CONTROLFILE FOR STANDBY;

- Archivelog

RMAN> BACKUP ARCHIVELOG ALL;

8 – Move Files to Standby Server

On the standby server create ALL directories using the same name convention and paths used in the primary server.

Move the PFILE, the standby controlfile, the archivelogs and the primary database backup to the same path in the standby server (do the file movement the best way it fits for you, FTP, SCP and so on).

Steps for Both Databases

9 – Setup Oracle Net

In order to have the redo logs being applied on the standby database, it’s necessary to have the network setup working properly. Let’s setup the listener for both database.

- Listener.ora on primary database
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP) (HOST = hostname_primary)(PORT = 1521))
)
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC))
)
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /usr/app/oracle/product/10.2.0)
(PROGRAM = extproc)

   )
(SID_DESC =
(GLOBAL_DBNAME = primary)
(ORACLE_HOME = /usr/app/oracle/product/10.2.0)
(SID_NAME = primary)
)
)

- Listener.ora on standby database

LISTENER = 
(DESCRIPTION_LIST = 
 (DESCRIPTION = 
 (ADDRESS_LIST = 
 (ADDRESS = (PROTOCOL = TCP)
 (HOST = hostname_secondary)(PORT = 1521)) 
 ) 
 (ADDRESS_LIST = 
 (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC)) 
 ) 
 ) 
) 
SID_LIST_LISTENER = 
(SID_LIST = 
 (SID_DESC = 
 (SID_NAME = PLSExtProc) 
 (ORACLE_HOME = /usr/app/oracle/product/10.2.0) (PROGRAM = extproc) 
 ) 
 (SID_DESC = 
 (GLOBAL_DBNAME = secondary) 
 (ORACLE_HOME = /usr/app/oracle/product/10.2.0) 
 (SID_NAME = secondary) 
 )

- tnsnames.ora for both databases
primary=
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostname_primary)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = primary)
)
)
secondary=
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostname_secondary)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = secondary)
)
)

- Restart the listener in both servers

# lsnrctl stop;
# lsnrctl start;

Steps for Standby Database

10 – Setup instance parameters for the standby database.

Make a copy of you PFILE to $ORACLE_HOME/dbs and save as initsecondary.ora. Edit it with the parameters below:
*.DB_UNIQUE_NAME='standby'
*.DB_NAME='primary'
*.INSTANCE_NAME='standby'
*.LOG_ARCHIVE_CONFIG='DG_CONFIG=(primary,standby)'
*.LOG_ARCHIVE_DEST_1='LOCATION=/oradata/orcl/archive/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=standby'
*.LOG_ARCHIVE_DEST_2='SERVICE=primary VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=primary'
*.LOG_ARCHIVE_DEST_STATE_1='ENABLE'
*.LOG_ARCHIVE_DEST_STATE_2='ENABLE'
*.LOG_ARCHIVE_FORMAT='arch_%r_%t_%s.arc'
*.REMOTE_LOGIN_PASSWORDFILE='EXCLUSIVE'
*.SERVICE_NAMES='standby'
*.STANDBY_FILE_MANAGEMENT='AUTO'
*.DB_FILE_NAME_CONVERT='primary','standby'
*.FAL_SERVER='primary'
*.FAL_CLIENT='standby'

11- Create password file for Standby database

Follow the same steps from bullet 3 to create the orapwstandby password file for the standby database.

12 – Startup nomount the standby database

Set your ORACLE_SID to standby and start it up in nomount mode using the new PFILE.

SQL> startup nomount;

13 – Recover database using RMAN

Since all the backup files are already in the same location within the standby database connect to RMAN, connect to both databases and perform the recovery as follows.

RMAN> connect target sys/pwd@primary;
RMAN> connect aixiliary sys/pwd@standby;
RMAN> duplicate target database for standby nofilenamecheck;

This command will automatically mount the database in standby mode.

Start the automatically redo apply

SQL> alter database recover managed standby database disconnect from session;

14 – Checking if Standby database is working properly

To check if the database is working correctly, the current log sequence must be the same in both databases.

- Primary database

SQL> archive log list
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /oradata/orcl/archive/
Oldest online log sequence     941
Next log sequence to archive   943
Current log sequence           943

- Standby database

SQL> archive log list
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /oradata/orcl/archive/
Oldest online log sequence     941
Next log sequence to archive   0
Current log sequence           943

Also check if logs are being applied

SQL> select sequence#, applied from v$archived_log order by sequence#;

SEQUENCE# APP
—————- —–
935                   YES
936                   YES
937                   YES
938                   YES
939                   YES
940                   YES
941                    YES
942                   YES





Upgrading from Oracle 10g to 11g R2

14 12 2011

Upgrading to Oracle 11g is a quite easy task, we are going to do that in some few steps.

The way I’m doing that is by installing the new 11g binaries in a new oracle_home. I’m also performing the upgrade on a Linux CentOS operating system. You can do basically the same on different OS just make sure to read the specific preinstallation guide.

1. Make sure you read the Oracle 11g R2 preinstallation guide so all the prerequisites are fulfilled.

- Download the PDF guide it he link below:
http://docs.oracle.com/cd/E11882_01/install.112/e24321/pre_install.htm#BABFDGHJ

2. Creating operating system groups for Database administrators.

- For a complete guidance on creating groups for all levels of administration and ASM refer to the pdf above. On this post I’m just creating the groups and user I’m going to work with.

- Create groups oinstall (used to be the oracle inventory owner) and dba (used for database administrations)

# groupadd oinstall
# groupadd dba

- Create user oracle

# useradd oracle and set a password for it # passwd your_pwd

- Add user oracle to dba and oinstall groups

# usermod -g dba oracle
# usermod -g oinstall oracle

For more about user creation click here and groups here.

Perform the coming steps logged as user oracle.

3. Download the binaries for 11gR2 on the link below

http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html?ssSourceSiteId=ocomen

4. Unzip the packages

# unzip file_name.zip

5. Performing Oracle 11g installation in silent mode

I have chosen to perform the installation in silent mode since I’m not using any GUI you can do that the way it fits for you.

- After filling out the response file (will talk about it in a later post) where you set the new ORACLE_HOME, the SYSDBA groups and so on, run the following command from the ‘database’ folder

# . runInstaller -silent -responseFile /home/downloads/database/response/my_response.rsp (change the path and file name to your own)

6. Run the pre-upgrade script utlu112i.sql

- From the 10g database, login as sysdba and run script utlu112i.sql

SQL> @Oracle_home11g/rdbms/admin/utlu112i.sql

This script will check any possible problems in the database to be upgraded and also update some system tables with information related to the new time zone patch and some more information.

Read carefully all the warnings and advises returned by the script, try to fix all of them. Below some of the most common ones:

Update Parameters: [Update Oracle Database 11.2 init.ora or spfile]
**********************************************************************
Obsolete/Deprecated Parameters: [Update Oracle Database 11.2 init.ora or spfile]
**********************************************************************
--> background_dump_dest         11.1       DEPRECATED   replaced by "diagnostic_dest"
--> user_dump_dest               11.1       DEPRECATED   replaced by "diagnostic_dest"
--> core_dump_dest               11.1       DEPRECATED   replaced by "diagnostic_dest"

The above warning will show up normally just as a reminder for the deprecated parameters change. I’ll discuss it in the next bullet.

WARNING: --> Database contains schemas with stale optimizer statistics. -DONE
.... Refer to the Upgrade Guide for instructions to update
.... schema statistics prior to upgrading the database.
.... Component Schemas with stale statistics:
....   SYS"

To fix that up simply run the following in your 10g oracle as SYSDBA:

SQL>  exec dbms_stats.gather_schema_stats('SYS',options=>'GATHER STALE',estimate_percent=>DBMS_STATS.AUTO_SAMPLE_SIZE, method_opt=>'FOR ALL COLUMNS SIZE AUTO', cascade=>TRUE);

7. Creating the new pfile and upgrading the database

- Copy the pfile from your 10g db to the 11g path (Oracle_home_11g/dbs). If you are working with a spfile create a pfile

SQL> create pfile from spfile;

- comment or remove the deprecated parameters

  • background_dump_dest
  • user_dump_dest
  • core_dump_dest

- add parameter “diagnostic_dest” and set the path you want to. Oracle will crate all the required directories and files under that path

- change parameter “compatible” to “11.2.0″

8. Stopping Oracle 10g and starting 11g

- Shutdown your Oracle 10g and change your ORACLE_HOME variable to the 11g one. Also reexport the PATH variable using the new ORACLE_HOME/bin

- export ORACLE_SID to the 11g db name (probably the same)

- startup database in upgrade mode

SQL> startup upgrade;

- Run the catupgrd.sql script

SQL> @?/rdbms/admin/catupgrd.sql; (This script will automatically shut the database down)

- startup database normally and run the remaining upgrade scripts

  1. @?/rdbms/admin/utlu112s.sql (performs an overall post upgrade check)
  2. @?/rdbms/admin/catuppst.sql (some more changes to the database system)
  3. @?/rdbms/admin/utlrp.sql (recompile invalid objects)

- Move the tnsnames.ora and listener.ora from the 10g to the 11g location

- Make sure all the components are ok in the database

SQL> select comp_name, status, version from dba_registry;

9. Backup your database

I hope it helps, if you have any questions just post a comment





Script to Create a New Oracle Database

23 12 2009

If you need to create a database in Oracle 10g but doesn´t want to use the DBCA feature or are unable to use it, you could simply follow the following steps to get it created using some scripts.

Before you run the script you must have an Oracle Database Installed and an instance already set up.

To set up the instance just have its PFILE created on $ORACLE_HOME/dbs/initSID.ora

1- Set you $ORACLE_SID

export ORACLE_SID=oracle_sid_name

2-Start the Instance in NOMOUNT mode

In order to create the database the instance should be started in NOMOUNT mode as follows:

sqlplus / as sysdba

startup nomount

3-Run Create Database Script

With the instance started run the script below

create database orcl
logfile group 1 ('/path/orcl/redolog/log1/redo1.log') size 50M,
            group 2 ('/path/orcl/redolog/log2/redo2.log') size 50M,
            group 3 ('/path/orcl/redolog/log3/redo3.log') size 100M
character set WE8ISO8859P1
national character set utf8
datafile '/path/orcl/data/orcl_sys0.dbf' size 300M autoextend off extent management local
sysaux datafile '/path/orcl/data/sysaux.dbf' size 100M autoextend off
undo tablespace undotbs0 datafile '/pathorcl/data/orcl_und0.dbf' size 50M
default temporary tablespace temp tempfile '/path/orcl/data/orcl_tmp0.dbf' size 60M;

Where:

LOGFILE – defines the paths and how many log groups will be created and its size;

CHARACTER SET – defines the type of character set according to the region the database is and the characters used. Is used for CHAR, VARCHAR2, CLOB columns;

NATIONAL CHARACTER SET – also defines the type of character and is used for NCHAR, NVARCHAR2, NCLOB columns;

Fist datafile - defines the path whete tablespace SYSTEM will be created, autoextend was disabled for better DBA management;

SYSAUX DATAFILE - defines the path and size for tablespace SYSAUX;

UNDO TABLESPACE - defines the undo tablespace name and DATAFILE its path;

DEFAULT TEMPORARY TABLESPACE - defines the name for the temporary tablespace and TEMPFILE its path.

Obs.: The script above aims the creation of a simple database. There are several others options and parameters to set up.

4-Run Catalog and Catproc

Since database is created you should run the scripts catalog and catproc (both provided by oracle). Their path is $ORACLE_HOME/rdbms/admin. Just run as follows:

@?/rdbms/admin/catalog.sql

@?/rdbms/admin/catproc.sql

Hope this is useful for you. Any doubts please comment.





Upgrading from Oracle 10.2.0.1 to 10.2.0.4

29 07 2009

On this post I’d like to share with you how to install and setup the Oracle 10.2.0.4 patch. This guide also applies for other patches and versions.


1 – Downloading the patch

Go to Oracle Metalink website (metalink.oracle.com) and login.

Click on “Patches & Updates” tab and search for patch number 6810189 as you can see below:

The search should bring the following result:

If your search retrieves too many results try filtering by the platform you’re looking for.

Download the file (if you are downloading from windows transfer it via FTP to the Linux OS you will perform the upgrade. For more details on how to FTP files check on this post http://dbaseworld.wordpress.com/2009/07/25/how-to-ftp-and-command-list/).


2 – Unzipping and Installing Patch

Since you have the file in the server you need, unzip the package as follows

unzip p6810189_10204_Linux-x86.zip

After unzipping the package, go to the directory Disk1/response and edit the response file patchset.rsp. Setup the ORACLE_HOME to your actual value and the OUI variables to False since it’s a silent installation.

Run the command below from the Disk1 directory:

. runInstaller -silent -responseFile Disk1/response/patchset.rsp


3 – Startup database and running scripts

After installation completion login the database as sysdba (sqlplus / as sysdba) and startup the database in upgrade mode

SQL> startup upgrade

The database should start and you can run the upgrade script. This may take several minutes to run.

@$ORACLE_HOME/rdbms/admin/catupgrd.sql

When finished shutdown the database and start it normally (startup command) and then run the script below to check if there’s any inactive object.

@/oracle/SID/102_64/rdbms/admin/utlrp.sql

When the script completes the upgrade is done.

To check if the new patch is up and running when you login in sqlplus you should see something like this:

SQL*Plus: Release 10.2.0.4.0

That’s all. If you have any doubts please comment.

Thanks





Setting up Oracle Instant Client

1 06 2009

Hey everyone!

Since I had to set up Oracle Client in my workstation, I created a step by step tutorial to get it done. Hope you like it.

- First of all download the appropriate instant client packages for your platform from http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/winsoft.html

All instalation require BASIC package and for this post downlaod the packages below:

- instantclient-basic_version_releasedate.zip
- instantclient-jdbc_version_releasedate.zip
- instantclient-odbc_version_releasedate.zip
- instantclient-sdk_version_releasedate.zip
- instantclient-sqlplus_version_releasedate.zip

- Unzip the files inthe order above (They will be unziped in the same folder “instantclientX_X”).

- Create directory ‘C:\Oracle\product\x.x.x\Client’ on Windows or /usr/app/oracle/product/x.x.x on Unix (x.x.x is the version you are workin with).

- Create the environment variable ORACLE_HOME and set it to the path created in the step above.

- Create directory ‘C:\Oracle\product\x.x.x\Client\network\admin’ on Windows or ‘/usr/app/oracle/product/x.x.x/network/admin’ on Unix.

- Create environment variable TNS_ADMIN and set it ‘C:\Oracle\product\x.x.x\Client\network\admin’ or ‘/usr/app/oracle/product/x.x.x/network/admin’ regarding your operational system.

- Include in the PATH environment variable the ORACLE_HOME.

- Copy the file ‘tnsnames.ora’ in the ‘$ORACLE_HOME/network/admin’ from your database server you want to connect to and paste it in the $ORACLE_HOME/network/admin.

Now the Oracle Instant Client is set up. To test the connection follow the steps below:

- Open the command window and type: “sqlplus username@instancename” the password will be required then. If the connection works fine you should see something like the screen below:

prompt_ss








Follow

Get every new post delivered to your Inbox.