RMAN Commands

Simple tape backup command.
dbref:PRODDB:/backup/PRODDB
$rman target / catalog rman/rman@rman

Recovery Manager: Release 10.2.0.4.0 - Production on Fri Mar 18 13:24:45 2011

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

connected to target database: PRODDB(DBID=3914890523)
connected to recovery catalog database

run {
 allocate channel d1 type disk;
 allocate channel d2 type disk;
 allocate channel d3 type disk;
 backup
 incremental level 0
 tag hot_db_bk_level0
 filesperset 5
 # recommended format
format '/backup/bk_%s_%p_%t'
 (database);
 sql 'alter system archive log current';
  # backup all archive logs
 backup
 filesperset 20
 format '/backup/al_%s_%p_%t'
 (archivelog all
 delete all input);
 }


dbref:PRODDB:/backup/PRODDB
$rman target / catalog rman/rman@rman

Recovery Manager: Release 10.2.0.4.0 - Production on Fri Mar 18 13:24:45 2011

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

connected to target database: PRODDB(DBID=3914890523)
connected to recovery catalog database

RMAN> run {
# Hot database level 0 whole backup
allocate channel t1 type 'SBT_TAPE';
2> 3> 4> allocate channel t2 type 'SBT_TAPE';
5> backup
6> incremental level 0
7> tag hot_db_bk_level0
8> filesperset 5
9> # recommended format
10> format 'bk_%s_%p_%t'
11> (database);
12> sql 'alter system archive log current';
13>   # backup all archive logs
14> backup
15> filesperset 20
16> format 'al_%s_%p_%t'
17> (archivelog all
18> delete all input);
19> }



RMAN> connect target / ;

2> connect catalog rman/rman@rman

3> sql "alter session set optimizer_mode=RULE";

4> allocate channel for maintenance device type disk;

5> allocate channel for maintenance device type SBT;

6> crosscheck backup;

7> crosscheck archivelog all;

8> delete noprompt expired backup;

9> delete noprompt expired archivelog all;

10> delete force noprompt obsolete;

11> release channel;

12>



52> connect target /

53> connect catalog rman/rman@RMAN

54>

55> run {

56> # Hot database level 1 cumulative incremental backup

57> allocate channel t1 type 'SBT_TAPE';

58> allocate channel t2 type 'SBT_TAPE';

59> backup

60>   incremental level 1

61>   cumulative

62>   tag hot_db_bk_level1_cum

63>   filesperset 5

64>   # recommended format

65>   format 'bk_%s_%p_%t'

66>     (database);

67>   sql 'alter system archive log current';

68>   # backup all archive logs

69>   backup

70>    filesperset 20

71>    format 'al_%s_%p_%t'

72>    (archivelog all

73>     delete input);

74> }

75>


Restore control file.

connect target /
connect catalog rman/rman@rman

set dbid = 1111111111
run {
# Restore  controlfile
set until time = "to_date('26-NOV-2000 09:00:14', 'dd-MON-yyyy hh24:mi:ss')";
allocate channel t1 type 'SBT_TAPE';
allocate channel t2 type 'SBT_TAPE';
allocate channel t3 type 'SBT_TAPE';
restore controlfile;
sql "alter database mount";
}

Restore from tape backup for a given time
connect target /
connect catalog rman/rman@rman
#set dbid = 1111111111
run {
allocate channel t1 type 'SBT_TAPE';
allocate channel t2 type 'SBT_TAPE';
restore database until time "to_date('15-NOV-2000 09:00:14', 'DD:MON-YYYY HH24:MI:SS')";
recover database until time "to_date('15-AUG-2000 09:00:14', 'DD:MON-YYYY HH24:MI:SS')";
}

Recover database for a given time.

connect target /
connect catalog rman/rman@rman
#set dbid = 1111111111
run {
allocate channel t1 type 'SBT_TAPE';
allocate channel t2 type 'SBT_TAPE';
recover database until time "to_date('15-NOV-2000 09:00:14', 'DD:MON-YYYY HH24:MI:SS')";
}

How to monitor RMAN progress?

Using the following SQL you can monitor the progress of RMAN process, as channels completes creating the backpiece new sessions are created so overall progress can be monitored by looking at where context = 3 as in the case below it shows the overall progress is 38.05% complete.

– works in 8i/9i/10g.

SQL> SELECT sid, serial#, context, sofar, totalwork,
round(sofar/totalwork*100,2) “% Complete”
FROM v$session_longops
WHERE totalwork 0
and upper(opname) like ‘%RMAN%’
and sofar totalwork;

SID SERIAL# CONTEXT SOFAR TOTALWORK % Complete
1026 26544 1 2081106 10752000 19.36
1002 28050 3 102401569 269149445 38.05
1037 16054 1 9125198 10854400 84.07
962 18556 1 9226142 10854400 85
1


### this script should be used to recover the database to an incarnation prior to the current one

connect target /
connect catalog rman/rman@rman

run {
#set until time 'Jul 8 2000 07:55:00'; # set time to just before data was lost
set until scn ; # get the scn from the alert_log
# the line will read something like RESETLOGS after incomplete recovery UNTIL CHANGE 1111111111
allocate channel t1 type 'SBT_TAPE';
allocate channel t2 type 'SBT_TAPE';
allocate channel t3 type 'SBT_TAPE';
sql "shutdown abort";
sql "startup nomount";
restore controlfile;
sql "alter database mount"; # mount database after restoring control file
restore database;
recover database;
sql "alter database open resetlogs"; # this command automatically resets the database
# so that this incarnation is the new incarnation
}

--- Restore tablespace

connect target /
connect catalog rman/rman@rmandb

run {
set until time "to_date('2000-01-15:11:00:00','YYYY-MM-DD:HH24:MI:SS')";
# recover read/write tablespaces
allocate channel t1 type 'SBT_TAPE';
allocate channel t2 type 'SBT_TAPE';
allocate channel t3 type 'SBT_TAPE';
#restore tablespace ts_arbinet_data;
restore controlfile from autobackup;
restore tablespace ts_dbaref_index;
recover tablespace ts_dbaref_data;
}

--get_rman_sid.sql
SELECT s.SID, s.serial#, p.SPID, s.CLIENT_INFO
FROM V$PROCESS p, V$SESSION s
WHERE p.ADDR = s.PADDR
AND CLIENT_INFO LIKE 'rman%'
/


 SPOOL LOG to 'list.txt'

From the machine where are performing the restore connect to the target
which is in nomount and to the catalog and upload the results of the
following rman commands.

% rman target / catalog rman/rman@rmandb
RMAN > set dbid = 3443740097;
RMAN > list backup of controlfile;
RMAN > show all;

-- get media.sql

select handle,media,completion_time,status from rc_backup_piece rbp, rc_database rd
where rd.name='DBAREF'
and rd.dbid = rbp.db_id
and trunc(completion_time) < to_date('11-01-07','MM-DD-YY')
order by trunc(completion_time)

get_cf_lock.sql
conn / as sysdba

spo kill_cf_lock.sql
SELECT 'alter system kill session '''||s.sid||','||s.serial#||''';'
FROM v$session s, v$enqueue_lock l
WHERE l.sid = s.sid and l.type = 'CF' AND l.id1 = 0 and l.id2 = 2
/
spo off
set echo on timing on time on
spo kill_cf_lock.log
@@kill_cf_lock.sql
spo off


Some useful commands in RMAN:

1. Starting a database from RMAN.

RMAN> startup mount pfile=/u01/app/oracle/product/8.1.6/dbs/init<SID>.ora

2. To see a database schema:

RMAN> report schema;

3. To copy a Datafile to say another disk.

RMAN> run {
Allocate channel c1 type disk ;
Copy Datafile 1 to '/u02/oradata/backup/datafile1.bak';
}

4. To backup a tablespace :

RMAN> run {
Allocate channel c1 type 'SBT_TAPE';
Backup tablespace system ;
}

5. Listing the backups :

RMAN> list backup;


6. Validate the backup, so that it does a test run and checks if
the backup can be restored successfully. Run the LIST command to find
the primary key for the backup set.

RMAN> list backup;
RMAN> run {
Allocate channel c1 type 'SBT_TAPE';
Validate backup set 3; # This validates the entire backup set
Restore controlfile validate; # This does for a control file
Restore tablespace system validate; # for a tablespace "SYSTEM"
Restore archivelog all validate; # For all archivelog
}

7. Use the list and report commands for querying the recovery catalog.

LIST – This command queries the catalog / control file to produce a
listing of its contents.

$ rman target sys/pass@PROD1 catalog rman/rman@RCAT1
RMAN> list copy of database archivelog all;
RMAN> list backup;

For particular objects :
RMAN> list backup of database; # all files in the database
RMAN> list copy of Datafile '/u01/oracle/dbs/tbs1.dbf';
RMAN> list copy of archivelog all;
RMAN> list backup of tablespace 'SYSTEM';
RMAN> list backup of control file;

REPORT – This command is used for getting a detailed information from
the catalog like

a. Which files need a backup
b. Which files haven't been backed up recently
c. Which files are listed unrecoverable
d. Which backup sets are obsolete and can be deleted
e. Which copies are on disk and which are on tape

To identify which datafiles need a backup :

RMAN> report need backup days = 7 database;
RMAN> report need backup days = 30 tablespace system;

Where days = x , is the number of days of archive logs required to
recover the database safely.


Types of Oracle backups :

1. Full -> It backs up all the used datablocks in a Datafile. It's
not the same as a Incremental Level 0 backup.

2. Incremental -> It backs up only all the data blocks that have
changed since the last incremental backup.
During a restore we need to have a level-0 backup also.
3. Open -> Backup of any part of target database when it is open

4. Closed -> When the database is mounted, but closed.

5. Consistent -> The database is mounted but not open. The
database must have been shutdown cleanly before this step.
The checkpoint SCNs in the datafile headers must match the
corresponding SCNs in the datafile records in the control file,
and the datafiles must not be fuzzy..
This backup can be restored without recovery.

6. Inconsistent -> The backup of the database when it is open, when it crashed
lasttime, when it was mounted or after a shutdown abort
before backup. It requires a database recovery.




Comments