Rman Tutorial
Rman Tutorial
After the database has been registered with the recovery catalog, we can confirm if the
target database structure has been correctly registered in the catalog database.
RMAN> report schema;
Report of database schema
File K-bytes Tablespace
RB segs Datafile Name
---- ---------- -------------------- ------- ------------------1
348160 SYSTEM
YES /opt/oracle/oradata/dba01/system01.dbf
2
204800 UNDOTBS1
YES /opt/oracle/oradata/dba01/undotbs01.dbf
3
20480 DRSYS
NO /opt/oracle/oradata/dba01/drsys01.dbf
4
140160 EXAMPLE
NO /opt/oracle/oradata/dba01/example01.dbf
5
25600 INDX
NO
/opt/oracle/oradata/dba01/indx01.dbf
6
20480 ODM
NO /opt/oracle/oradata/dba01/odm01.dbf
7
10240 TOOLS
NO /opt/oracle/oradata/dba01/tools01.dbf
8
25600 USERS
NO
/opt/oracle/oradata/dba01/users01.dbf
9
39040 XDB
NO
/opt/oracle/oradata/dba01/xdb01.dbf
We can then configure a number of backup parameters so that the same information need
not be provided every time a backup is performed.
In this case we are configuring RMAN so that all database backups (unless specifically
mentioned) go to the common location /opt/oracle/backup with the prefix bkp.
The backup piece name is automatically generated by RMAN because we are using the
%U placeholder.
We are also configuring the control file to be automatically backed up whenever a
BACKUP or RESTORE command is issued as well as when the structure of the database
changes.
RMAN> CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '/opt/oracle/backup/bkp.%U';
RMAN> CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO
'/opt/oracle/backup/bkp_cf_%F';
RMAN> CONFIGURE CONTROLFILE AUTOBACKUP ON;
RMAN> SHOW ALL;
RMAN configuration parameters are:
CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
CONFIGURE BACKUP OPTIMIZATION OFF; # default
Note: The controlfile and Server Parameter File has automatically been backed up by
RMAN as well
We will then query the catalog to list information about this backup.
RMAN> list backup of database summary;
List of Backups
===============
Key TY LV S Device Type Completion Time #Pieces #Copies Tag
TAG20051231T095219
To simulate some backup and recovery scenarios we will create a table in the SYSTEM
schema stored in the USERS tablespace.
We will also create a procedure to load records into this table to simulate database
activity.
create table myobjects
tablespace users
as select * from dba_objects;
create or replace procedure
load_data
is
begin
for i in 1 .. 10
loop
insert into myobjects
select * from sys.dba_objects;
end loop;
commit;
end;
/
Let us now execute the procedure and load data in the table. Records in the table will
vary by database as it depends on the number of database objects. Note that loading data
in the table has generated archive log files as well.
SQL> exec load_data
PL/SQL procedure successfully completed.
SQL> select count(*) from myobjects;
COUNT(*)
---------318746
SQL> archive log list
Database log mode
Archive Mode
Automatic archival
Enabled
Archive destination
/opt/oracle/oradata/dba01/arch
Oldest online log sequence 128
Next log sequence to archive 130
Current log sequence
130
Note: the archive destination can vary based on the physical location of the archive log files
SQL> !ls -l /opt/oracle/oradata/dba01/arch
total 47256
-rw-r----- 1 oracle dba
1045504 Dec 31 10:41 arch100.log
-rw-r----- 1 oracle dba
1045504 Dec 31 10:41 arch101.log
-rw-r----- 1 oracle dba
1047040 Dec 31 10:41 arch102.log
-rw-r----- 1 oracle dba
1047040 Dec 31 10:41 arch103.log
-rw-r----- 1 oracle dba
1046016 Dec 31 10:41 arch104.log
-rw-r----- 1 oracle dba
1046528 Dec 31 10:41 arch105.log
-rw-r----- 1 oracle dba
1046528 Dec 31 10:41 arch106.log
-rw-r----- 1 oracle dba
1046016 Dec 31 10:41 arch107.log
-rw-r----- 1 oracle dba
1045504 Dec 31 10:41 arch108.log
-rw-r----- 1 oracle dba
1045504 Dec 31 10:41 arch109.log
-rw-r----- 1 oracle dba
1046016 Dec 31 10:41 arch110.log
-rw-r----- 1 oracle dba
1044480 Dec 31 10:41 arch111.log
-rw-r----- 1 oracle dba
1046016 Dec 31 10:41 arch112.log
-rw-r----- 1 oracle dba
1044480 Dec 31 10:41 arch113.log
-rw-r----- 1 oracle dba
1047040 Dec 31 10:41 arch114.log
-rw-r----- 1 oracle dba
1047040 Dec 31 10:41 arch115.log
-rw-r----- 1 oracle dba
1043968 Dec 31 10:41 arch116.log
-rw-r----- 1 oracle dba
1047040 Dec 31 10:41 arch117.log
-rw-r----- 1 oracle dba
1046528 Dec 31 10:41 arch118.log
-rw-r----- 1 oracle dba
1047040 Dec 31 10:41 arch119.log
-rw-r----- 1 oracle dba
1047040 Dec 31 10:41 arch120.log
-rw-r----- 1 oracle dba
1047040 Dec 31 10:41 arch121.log
-rw-r----- 1 oracle dba
1045504 Dec 31 10:41 arch122.log
-rw-r----- 1 oracle dba
1043968 Dec 31 10:41 arch123.log
-rw-r----- 1 oracle dba
1047040 Dec 31 10:41 arch124.log
-rw-r----- 1 oracle dba
1045504 Dec 31 10:41 arch125.log
-rw-r----- 1 oracle dba
1047040 Dec 31 10:41 arch126.log
-rw-r----- 1 oracle dba
1043456 Dec 31 10:41 arch127.log
-rw-r----- 1 oracle dba
1044480 Dec 31 10:41 arch128.log
-rw-r-----rw-r-----rw-r-----rw-r-----rw-r-----rw-r-----rw-r-----rw-r-----rw-r-----rw-r-----rw-r-----rw-r-----rw-r-----rw-r-----rw-r-----rw-r-----rw-r-----
1 oracle
1 oracle
1 oracle
1 oracle
1 oracle
1 oracle
1 oracle
1 oracle
1 oracle
1 oracle
1 oracle
1 oracle
1 oracle
1 oracle
1 oracle
1 oracle
1 oracle
dba
dba
dba
dba
dba
dba
dba
dba
dba
dba
dba
dba
dba
dba
dba
dba
dba
We will now take a backup of the archive log files now - after the backup completes, the
files will be physically deleted from disk.
RMAN> sql "ALTER SYSTEM ARCHIVE LOG CURRENT";
sql statement: ALTER SYSTEM ARCHIVE LOG CURRENT
RMAN> BACKUP ARCHIVELOG ALL DELETE INPUT;
dba01:/opt/oracle>ls -lrt /opt/oracle/oradata/dba01/arch
total 0
After the archivelog backup has completed and the files physically deleted from disk, we
will query the catalog to confirm that the record of this archivelog backup does exist.
RMAN> LIST BACKUP OF ARCHIVELOG ALL;
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
172518
175256
177990
180724
183489
186278
189061
189120
189128
189137
190732
190977
190986
190996
191014
191055
191063
191077
191087
191100
191112
191124
191133
191145
191156
191166
191177
191192
191200
191216
191230
191238
191253
191262
191275
191285
191293
191309
191317
191329
191346
191358
191370
191378
191395
192745
31-DEC-05 175256
31-DEC-05 177990
31-DEC-05 180724
31-DEC-05 183489
31-DEC-05 186278
31-DEC-05 189061
31-DEC-05 189120
31-DEC-05 189128
31-DEC-05 189137
31-DEC-05 190732
31-DEC-05 190977
31-DEC-05 190986
31-DEC-05 190996
31-DEC-05 191014
31-DEC-05 191055
31-DEC-05 191063
31-DEC-05 191077
31-DEC-05 191087
31-DEC-05 191100
31-DEC-05 191112
31-DEC-05 191124
31-DEC-05 191133
31-DEC-05 191145
31-DEC-05 191156
31-DEC-05 191166
31-DEC-05 191177
31-DEC-05 191192
31-DEC-05 191200
31-DEC-05 191216
31-DEC-05 191230
31-DEC-05 191238
31-DEC-05 191253
31-DEC-05 191262
31-DEC-05 191275
31-DEC-05 191285
31-DEC-05 191293
31-DEC-05 191309
31-DEC-05 191317
31-DEC-05 191329
31-DEC-05 191346
31-DEC-05 191358
31-DEC-05 191370
31-DEC-05 191378
31-DEC-05 191395
31-DEC-05 192745
31-DEC-05 193033
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
31-DEC-05
We can also run the following command to list all the backups of the archive log files
performed in the past 24 hours.
RMAN> LIST BACKUP OF ARCHIVELOG FROM TIME 'SYSDATE -1';
Please note that the backup location has backup pieces pertaining to the database backup,
the control file autobackup as well as the archive log backups.
dba01:/opt/oracle/backup>ls -lrt
total 496740
-rw-r----- 1 oracle dba 455118848 Dec 31 09:53 bkp.01h7ltaj_1_1
-rw-r----- 1 oracle dba
1892352 Dec 31 09:53 bkp_cf_c-4054272425-20051231-00
-rw-r----- 1 oracle dba 49178112 Dec 31 10:49 bkp.03h7m0mk_1_1
-rw-r----- 1 oracle dba
1957888 Dec 31 10:50 bkp_cf_c-4054272425-20051231-01
We will now take a level 0 incremental backup by using a RMAN command file which
can be invoked from the command line by calling the rman executable .
dba01:/opt/oracle/scripts>rman target / catalog rman/rman@rmanp cmdfile=bkp_inc_lev0.rcv log=bkp_inc_lev0.log
Scenario 1:
Loss of a datafile
dba01:/opt/oracle/oradata/dba01>rm users01.dbf
Because the datafile holding the USERS tablespace has been deleted at the O/S level to
simulate the media failure, the load_data procedure will fail.
SQL> exec load_data
BEGIN load_data; END;
*
ERROR at line 1:
ORA-01116: error in opening database file 8
ORA-01110: data file 8: '/opt/oracle/oradata/dba01/users01.dbf'
ORA-27041: unable to open file
Linux Error: 2: No such file or directory
Additional information: 3
ORA-06512: at "SYSTEM.LOAD_DATA", line 7
ORA-06512: at line 1
We will perform a RMAN datafile recovery by restoring and recovering only one single
datafile that has been affected by the media failure. We need to take the datafile offline
before performing the recovery.
dba01:/opt/oracle/oradata/dba01>rman target / catalog rman/rman@rmanp
Recovery Manager: Release 9.2.0.6.0 - Production
Let us now load data in the table to confirm that our media recovery has worked
SQL> exec load_data
PL/SQL procedure successfully completed.
We can similarly write a RMAN command file to take a level 1 incremental backup.
Note: while doing a restore and recover, RMAN will try to use a Level 1 incremental
backup first over using archive log files.
dba01:/opt/oracle/scripts>cat bkp_inc_lev1.rcv
run
{
Now take a Level 1 backup using the command file listed above in the same way as the
Level 0 backup was taken from the command line.
Based on the retention policy chosen, we can list the backups that are obsolete as well as
delete them physically from disk.
RMAN> report obsolete;
RMAN retention policy will be applied to the command
RMAN retention policy is set to redundancy 1
Report of obsolete backups and copies
Type
Key Completion Time Filename/Handle
-------------------- ------ ------------------ -------------------Backup Set
12888157 31-DEC-05
Backup Piece
12888158 31-DEC-05
/opt/oracle/backup/bkp.01h7ltaj_1_1
Backup Set
12888169 31-DEC-05
Backup Piece
12888170 31-DEC-05
/opt/oracle/backup/bkp_cf_c-4054272425-20051231-00
Backup Set
12889315 31-DEC-05
Backup Piece
12889316 31-DEC-05
/opt/oracle/backup/bkp.03h7m0mk_1_1
Backup Set
12889366 31-DEC-05
Backup Piece
12889367 31-DEC-05
/opt/oracle/backup/bkp_cf_c-4054272425-20051231-01
Backup Set
12889556 31-DEC-05
Backup Piece
12889557 31-DEC-05
/opt/oracle/backup/bkp.05h7m2rs_1_1
Backup Set
12889581 31-DEC-05
Backup Piece
12889582 31-DEC-05
/opt/oracle/backup/bkp_cf_c-4054272425-20051231-02
Backup Set
12896130 31-DEC-05
Backup Piece
12896131 31-DEC-05
/opt/oracle/backup/bkp_cf_c-4054272425-20051231-03
Backup Piece
Backup Set
Backup Piece
12889582 31-DEC-05
12896130 31-DEC-05
12896131 31-DEC-05
/opt/oracle/backup/bkp_cf_c-4054272425-20051231-02
/opt/oracle/backup/bkp_cf_c-4054272425-20051231-03
Do you really want to delete the above objects (enter YES or NO)? YES
deleted backup piece
backup piece handle=/opt/oracle/backup/bkp.01h7ltaj_1_1 recid=1 stamp=578483543
deleted backup piece
backup piece handle=/opt/oracle/backup/bkp_cf_c-4054272425-20051231-00 recid=2 stamp=578483615
deleted backup piece
backup piece handle=/opt/oracle/backup/bkp.03h7m0mk_1_1 recid=3 stamp=578486997
deleted backup piece
backup piece handle=/opt/oracle/backup/bkp_cf_c-4054272425-20051231-01 recid=4 stamp=578487003
deleted backup piece
backup piece handle=/opt/oracle/backup/bkp.05h7m2rs_1_1 recid=5 stamp=578489213
deleted backup piece
backup piece handle=/opt/oracle/backup/bkp_cf_c-4054272425-20051231-02 recid=8 stamp=578489283
deleted backup piece
backup piece handle=/opt/oracle/backup/bkp_cf_c-4054272425-20051231-03 recid=9 stamp=578491304
Deleted 7 objects
We can also run the command to delete obsolete backup pieces without having to answer
the prompt
RMAN > delete noprompt obsolete;
We can query the catalog to confirm that only one level 0 backup and one level 1 backup
exist. The earlier level 0 backup has been deleted because it was found to be obsolete
considering the RMAN retention policy chosen.
RMAN> list backup of database summary;
List of Backups
===============
Key TY LV S Device Type Completion Time #Pieces #Copies Tag
------- -- -- - ----------- --------------- ------- ------- --12889562 B 0 A DISK
31-DEC-05
1
1
TAG20051231T112654
12896580 B 1 A DISK
31-DEC-05
1
1
TAG20051231T121214
Scenario 2:
dba01:/opt/oracle/oradata/dba01>rm *.dbf
Since all the datafiles have been deleted to simulate loss of all datafiles, the procedure to
load data will also fail subsequently.
SQL> exec load_data
BEGIN load_data; END;
*
ERROR at line 1:
Let us verify that the data has been fully restored by querying the MYOBJECTS table
dba01:/opt/oracle/oradata/dba01>sqlplus system/oracle
SQL> select count(*) from myobjects;
COUNT(*)
---------608516
Scenario 3:
Point-In-Time Recovery
We will now drop the table MYOBJECTS and then perform an RMAN point-in-time
recovery to restore the database to a point in time just before the table was dropped. Note
the number of rows in the table.
SQL> select count(*) from myobjects;
COUNT(*)
---------608516
SQL> drop table myobjects;
Table dropped.
Shutdown and then mount the database in preparation for restore and recovery.
Note the incremental backup since available is restored before applying the archive log files. The
archive log files that will be applied are those that have been generated after the Level 1
incremental backup.
Since we are restoring the database to a point in the past, we will have to open the database with
the RESETLOGS option.
RMAN> run {
2> set until time "to_date('12/31/05 12:43:00','mm/dd/yy hh24:mi:ss')";
3> restore database;
4> recover database;
5> }
executing command: SET until clause
Starting restore at 31-DEC-05
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=14 devtype=DISK
channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to /opt/oracle/oradata/dba01/system01.dbf
restoring datafile 00002 to /opt/oracle/oradata/dba01/undotbs01.dbf
restoring datafile 00003 to /opt/oracle/oradata/dba01/drsys01.dbf
restoring datafile 00004 to /opt/oracle/oradata/dba01/example01.dbf
restoring datafile 00005 to /opt/oracle/oradata/dba01/indx01.dbf
restoring datafile 00006 to /opt/oracle/oradata/dba01/odm01.dbf
restoring datafile 00007 to /opt/oracle/oradata/dba01/tools01.dbf
restoring datafile 00008 to /opt/oracle/oradata/dba01/users01.dbf
restoring datafile 00009 to /opt/oracle/oradata/dba01/xdb01.dbf
channel ORA_DISK_1: restored backup piece 1
piece handle=/opt/oracle/backup/bkp.06h7m2ru_1_1 tag=TAG20051231T112654 params=NULL
channel ORA_DISK_1: restore complete
Finished restore at 31-DEC-05
Starting recover at 31-DEC-05
using channel ORA_DISK_1
channel ORA_DISK_1: starting incremental datafile backupset restore
We will now connect to the database to confirm that our table which we had earlier
dropped has been restored as well as the record count in the table matches that taken
before the table drop.
dba01:/opt/oracle/oradata/dba01>sqlplus system/oracle
SQL> select count(*) from myobjects;
COUNT(*)
---------608516
Because we have done a RESETLOGS we need to reset the database in the recovery
catalog.
dba01:/opt/oracle/oradata/dba01>rman target / catalog rman/rman@rmanp
RMAN> reset database;
Scenario 4:
file
Loss of datafiles, control files, online redo log files and server parameter
We will simulate a complete machine failure by deleting all the data files, redo log files
and the control files as well.
dba01:/opt/oracle/oradata/dba01>rm *.*
dba01:/opt/oracle/oradata/dba01> cd $ORACLE_HOME/dbs
dba01:/opt/oracle/oradata/dba01> rm spfiledba01.ora
The data load program will fail as the database has crashed
dba01:/opt/oracle/oradata/dba01>sqlplus system/oracle
SQL> exec load_data
BEGIN load_data; END;
*
ERROR at line 1:
ORA-00604: error occurred at recursive SQL level 1
ORA-01116: error in opening database file 1
ORA-01110: data file 1: '/opt/oracle/oradata/dba01/system01.dbf'
ORA-27041: unable to open file
Linux Error: 2: No such file or directory
Additional information: 3
We need to restore the spfile first from the RMAN backup we start the instance using a
dummy initialization file. Then the control file will be restored followed by the data
files and finally the database will be recovered by applying any archive log files if
required.
Since we are restoring the database using a backup of the control file, we need to open
the database with the RESETLOGS option. Subsequently, the database has also to be
reset in the recovery catalog.
dba01:/opt/oracle/oradata/dba01>rman target / catalog rman/rman@rmanp
RMAN> run {
2> startup nomount force;
3> }
We then need to set the DBID this can be obtained by listing the files generated by the
control file autobackup that had been enabled earlier. The DBID can be found as part of
the file name
dba01:/opt/oracle/product9206/dbs>ls
c-4054272425-20051225-01
Connect to the database and confirm that the MYOBJECTS table has been successfully
restored.
SQL> conn system/oracle
Connected.
SQL> select count(*) from myobjects;
COUNT(*)
---------608516
We will now generate some redo activity to create some archive log files which we will
then take a backup using RMAN
dba01:/opt/oracle/backup>sqlplus system/oracle
SQL> exec load_data
PL/SQL procedure successfully completed.
RMAN> backup archivelog from sequence=1 until sequence=33;
( Please provide sequence numbers pertaining to your database)
Starting backup at 02-JAN-06
starting full resync of recovery catalog
full resync complete
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=17 devtype=DISK
channel ORA_DISK_1: starting archive log backupset
channel ORA_DISK_1: specifying archive log(s) in backup set
input archive log thread=1 sequence=1 recid=267 stamp=578670250
input archive log thread=1 sequence=2 recid=268 stamp=578670251
input archive log thread=1 sequence=3 recid=269 stamp=578670251
input archive log thread=1 sequence=4 recid=270 stamp=578670252
input archive log thread=1 sequence=5 recid=271 stamp=578670252
input archive log thread=1 sequence=6 recid=272 stamp=578670252
input archive log thread=1 sequence=7 recid=273 stamp=578670253
input archive log thread=1 sequence=8 recid=274 stamp=578670253
input archive log thread=1 sequence=9 recid=275 stamp=578670253
input archive log thread=1 sequence=10 recid=276 stamp=578670253
input archive log thread=1 sequence=11 recid=277 stamp=578670253
input archive log thread=1 sequence=12 recid=278 stamp=578670253
input archive log thread=1 sequence=13 recid=279 stamp=578670253
input archive log thread=1 sequence=14 recid=280 stamp=578670253
input archive log thread=1 sequence=15 recid=281 stamp=578670254
input archive log thread=1 sequence=16 recid=282 stamp=578670254
input archive log thread=1 sequence=17 recid=283 stamp=578670254
input archive log thread=1 sequence=18 recid=284 stamp=578670254
input archive log thread=1 sequence=19 recid=285 stamp=578670254
input archive log thread=1 sequence=20 recid=286 stamp=578670255
input archive log thread=1 sequence=21 recid=287 stamp=578670255
input archive log thread=1 sequence=22 recid=288 stamp=578670255
input archive log thread=1 sequence=23 recid=289 stamp=578670255
input archive log thread=1 sequence=24 recid=290 stamp=578670255
input archive log thread=1 sequence=25 recid=291 stamp=578670255
input archive log thread=1 sequence=26 recid=292 stamp=578670256
input archive log thread=1 sequence=27 recid=293 stamp=578670256
input archive log thread=1 sequence=28 recid=294 stamp=578670256
input archive log thread=1 sequence=29 recid=295 stamp=578670256
input archive log thread=1 sequence=30 recid=296 stamp=578670256
input archive log thread=1 sequence=31 recid=297 stamp=578670256
input archive log thread=1 sequence=32 recid=298 stamp=578670257
input archive log thread=1 sequence=33 recid=299 stamp=578670257
channel ORA_DISK_1: starting piece 1 at 02-JAN-06
channel ORA_DISK_1: finished piece 1 at 02-JAN-06
1
1
1
31
32
33
733748
733768
733788
02-JAN-06 733768
02-JAN-06 733788
02-JAN-06 733794
02-JAN-06
02-JAN-06
02-JAN-06