Oracle RMAN Database Restore and Recovery Guide
Oracle RMAN Database Restore and Recovery Guide
Purpose
A restore that fails at the first step is usually a startup problem, not a backup problem. RMAN needs the instance in the right state for each command. A lost controlfile needs a started instance with no mounted database. A lost datafile needs a mounted database. Each script below begins with the startup mode that fits its case.
The four RMAN scripts cover the four cases a DBA meets most. The first is full restore and recovery after a complete loss. The second is point-in-time recovery when archived logs must also come back from backup. The third is point-in-time recovery when the controlfile and archived logs are still on disk. The fourth is a plain recovery of datafiles that are already in place. A short SQL query at the end lists which datafiles went into which backup set, so you can plan a restore before you start it.
Restore and recovery are the subject here. For the backup side, read the RMAN backup commands guide.
Code
1-- 1. Full restore and recovery
2startup nomount;
3run {
4 allocate channel t1 type disk;
5 allocate channel t2 type disk;
6 allocate channel t3 type disk;
7 allocate channel t4 type disk;
8 restore controlfile;
9 restore archivelog all;
10 alter database mount;
11 restore database;
12 recover database;
13}
14sql 'alter database open resetlogs';
15
16-- 2. Point-in-time recovery, archived logs restored from backup
17startup nomount;
18run {
19 set until time ="to_date('30/08/2006 12:00','dd/mm/yyyy hh24:mi')";
20 allocate channel t1 type disk;
21 allocate channel t2 type disk;
22 allocate channel t3 type disk;
23 allocate channel t4 type disk;
24 restore controlfile;
25 restore archivelog all;
26 alter database mount;
27 restore database;
28 recover database;
29}
30sql 'alter database open resetlogs';
31
32-- 3. Point-in-time recovery, controlfile and archived logs already on disk
33startup mount;
34run {
35 set until time ="to_date('08/02/2007 14:00','dd/mm/yyyy hh24:mi')";
36 allocate channel t1 type disk;
37 allocate channel t2 type disk;
38 allocate channel t3 type disk;
39 allocate channel t4 type disk;
40 restore database;
41 recover database;
42}
43sql 'alter database open resetlogs';
44
45-- 4. Simple recovery of datafiles already in place
46startup mount;
47run {
48 allocate channel t1 type disk;
49 recover database;
50}
1-- 5. Which datafiles are in which backup set, and when did each finish
2set pages 999 lines 100
3col name format a60
4break on set_stamp skip 1
5select set_stamp
6,to_char(ba.completion_time, 'HH24:MI DD/MM/YY') finish_time
7,df.name
8from v$datafile df
9,v$backup_datafile ba
10where df.file# = ba.file#
11and ba.file# != 0
12order by set_stamp, ba.file#
13/
Code Breakdown
Script 1: full restore and recovery
startup nomountstarts the instance and does not mount the database. Use it when the controlfile itself must be restored, because there is no usable controlfile to mount.allocate channel t1 type disk(t1 to t4) opens four channels. A channel is a connection between RMAN and a database instance. Each connection starts a database server session, and that session does the work of backing up, restoring or recovering. Four channels let one job read four backup sets at the same time.restore controlfilerestores the controlfile. Read the note below this list before you run it with no recovery catalog.restore archivelog allrestores archived redo log files from backup. See the Key Points for when this step is needed.alter database mountmounts the database with the restored controlfile. The steps after it run against the mounted database.restore databaserestores the datafiles. It does not restore the controlfile or the server parameter file. That is why the script restores the controlfile as a separate step.recover databaseapplies redo to the restored datafiles. For complete recovery, RMAN applies archived redo log files and online redo logs.sql 'alter database open resetlogs'opens the database with a new log sequence. It is required after a restore with a backup controlfile and after incomplete recovery.
Controlfile restore with no catalog. When the database is not mounted and RMAN is not connected to a recovery catalog, a plain restore controlfile does not work. You must name the autobackup. The version of script 1 for this case is:
1startup nomount;
2set dbid 1234567890; -- replace with the DBID of your database
3restore controlfile from autobackup;
RMAN displays the DBID each time you connect to the target database, so write it down before you need it. The same clause also works with the server parameter file.
Script 2: point-in-time recovery
set until timesets the recovery target for therestoreandrecovercommands that follow it in the samerunblock. The time here is an example from the original scripts. Replace it with the time just before the damage.- The position matters. Put
set untilbefore bothrestoreandrecover. If you set the time after the restore, the restored files may have time stamps later than the target time. You then may not be able to recover to that time. - Everything after the target is lost. All changes made after the target time are discarded when you open with
resetlogs.
Script 3: point-in-time recovery with files already on disk
startup mountis used because the controlfile is good. No controlfile restore is needed, so the script is shorter.- No
restore archivelogis in this script, because the archived logs are already in the archive destination. RMAN also restores archived logs by itself whenrecoverneeds them, so you seldom need to restore logs by hand.
Script 4: simple recovery
recover databasealone is enough when the datafiles are in place and only redo must be applied. This script recovers the whole database, so it mounts the database first. To recover single datafiles or tablespaces while the database is open, take those files offline before you runrecover.- No
resetlogsis needed, because the script does not recover to a point in time and does not use a backup controlfile. Open the database with a normalalter database open.
Script 5: datafile backup check
v$backup_datafileshows control files and datafiles in backup sets, read from the control file.set_stampis the backup set stamp. Rows with the same stamp are in the same backup set, so thebreakcommand prints a blank line between sets.file#is the datafile number. It is set to 0 for the control file, soba.file# != 0leaves the control file out of the report.completion_timeis the time the backup finished. Theto_charcall prints it as hour, minute, day, month and year.v$datafilesupplies the file name, joined onfile#.
Key Points
- The startup mode follows the failure. A restore of the controlfile starts from
nomount. A restore of datafiles needs a mounted database. Choose the script by what you have lost. - Plain
restore controlfilehas a limit. With no mounted database and no catalog, you must usefrom autobackup. If the database is mounted or open, you must use theto filenameclause instead. restore databaseis not the whole job. It leaves out the controlfile and the server parameter file. Restore them with their own commands.- After a restored controlfile, always run
recover. You must runrecoverafter you mount a restored controlfile, and you must open withresetlogs. - Incomplete recovery needs
resetlogs. A restore with a backup controlfile and any point-in-time recovery both need it. - Archived log restore before the mount. The database can be started, mounted or open for
restore archivelog, so scripts 1 and 2 run it before the mount. That rule does not say how RMAN finds the log backups when there is no catalog and no mounted control file. If the step fails in that case, move it afteralter database mount. Therecovercommand also restores archived redo log files as needed, so you seldom need to restore logs manually. - Point-in-time recovery needs archive log mode. It also needs backups of all datafiles from before the target, and archived logs for the period between those backups and the target.
Insights and Best Practices
Run the backup check first
Run script 5 before you decide anything. It shows which datafiles each backup set holds and when each set finished. If a datafile has no row, the control file holds no backup set record for it, and you should find out why before you rely on the restore.
Use set until as a unit
For point-in-time recovery, enter set until before both restore and recover in one run block. The time then applies to both. This keeps the restored datafiles old enough for the recover step to use them. A set command inside a run block stays in force until the block ends or another set changes the same value.
Mind the incarnation
Each resetlogs starts a new incarnation. RMAN can recover through a resetlogs operation when the datafiles to be recovered are from a parent incarnation. A target time that is not in the current incarnation is a special case. You must run reset database to the incarnation that was current at the target, and you must restore a controlfile from that incarnation.
Look at the data before you commit
When you recover with a backup controlfile, you can open the database read-only in SQL*Plus before the resetlogs open. Run your queries and confirm the damage is gone. Open with resetlogs only when the results are right. After resetlogs opens the database, the changes after the target are abandoned.
Set the channel count by measurement
You control the degree of parallelism in a job by the number of channels you allocate, up to 255. Each channel works on one backup set or image copy at a time. Channels beyond the number of backup sets have nothing to read. Start with the number of disks or tape drives that can read at once, measure the restore time, and change the count from there.
Back up again after the open
A new backup after resetlogs is still good practice. It is not a strict need, because RMAN can recover through resetlogs from a parent incarnation. A fresh backup gives you a clean start in the new incarnation. The steps are in the backup commands guide.
Related books recommended
Links go to Amazon. As an Amazon Associate, Oracle Scripts earns from qualifying purchases; your price is unchanged.
- Book: Oracle PL/SQL Programming, by Steven Feuerstein. The standard PL/SQL reference. (View on Amazon)
- Book: Expert Oracle Database Architecture, by Thomas Kyte. How the database really works, for DBAs. (View on Amazon)
- Book: Oracle Database DBA Handbook. Day-to-day administration reference. (View on Amazon)
When to Run This
- Script 1: every copy of the controlfile is lost, or the whole server is rebuilt and the database must come back from backup.
- Script 2: a bad change, such as a wrong delete or a faulty batch job, must be undone, and the archived logs are not on disk.
- Script 3: the same bad change, but the controlfile and archived logs are still available.
- Script 4: the datafiles are in place and only redo must be applied to them.
- Script 5: before any restore, and during recovery drills, to check what each backup set holds.
Troubleshooting Common Issues
- RMAN error on
restore controlfilein nomount. You have no catalog and nofrom autobackupclause. Set the DBID withset dbidand userestore controlfile from autobackup. If RMAN finds no autobackup, it signals an error and stops. The search starts on the current day, or on theset untilday, and goes back up tomaxdaysdays. The default is 7 and the maximum is 366. - Recovery asks for an unknown log. RMAN reports an error such as
RMAN-06054: media recovery requesting unknown log. If you did not give anuntiltime, use thecatalogcommand to add the missing redo logs to the repository. Then runrecoveragain. RMAN also searches for logs that are not in its records and catalogs any it finds. - Recover cannot reach the target time. The restored files may be newer than the target. This can happen when
set untilcame after the restore. Runset untilfirst, then restore again. open resetlogsfails. The command fails if a datafile is offline, unless the file went offline normally or is read-only. Bring the file online or deal with it first.- A restore command is refused. Each restore command has its own rule for the database state, as the Key Points show for
restore controlfile. Check which state the instance is in and compare it with the startup line of the script. - Script 5 returns no rows. The view is read from the control file, so that control file holds no backup set records. A controlfile restored from an older backup may be the cause. Check which control file is mounted before you trust an empty result.
References
- RESTORE - Oracle Database 19c Backup and Recovery Reference - Rules for
restore controlfile,from autobackup,restore archivelogand the database state each command needs - RECOVER - Oracle Database 19c Backup and Recovery Reference - Complete and incomplete recovery, the
untilclause and whenresetlogsis required - ALLOCATE CHANNEL - Oracle Database 19c Backup and Recovery Reference - What a channel is, how channels set parallelism and the 255 channel limit
- SET - Oracle Database 19c Backup and Recovery Reference -
set dbid, theuntilclause and how long asetstays in force inside arunblock - Advanced RMAN Recovery Scenarios - Oracle Database 19c Backup and Recovery User's Guide - Restoring a controlfile from autobackup with
set dbid, backup controlfile recovery and missing log errors - Performing Database Point-in-Time Recovery - Oracle Database 19c Backup and Recovery User's Guide - Prerequisites, the order of
set until,restore,recoverand theresetlogsopen - V$BACKUP_DATAFILE - Oracle Database 11.2 Reference - Columns of the view used in the datafile backup check, including
set_stampandfile# - V$DATAFILE - Oracle Database 19c Reference - Columns of the datafile view joined in the check query
- ORACLE-BASE - Recovery Manager (RMAN) in Oracle 8i - A short community walkthrough of restore, recover and incomplete recovery with
resetlogs
Posts in this series
- How to Create Oracle RMAN Recovery Catalog Step by Step
- How to Register an Oracle Database with RMAN Catalog
- Oracle RMAN Unregister Database from Recovery Catalog
- Oracle RMAN Reset Database After RESETLOGS Recovery
- Oracle RMAN CROSSCHECK BACKUP Command Explained
- Oracle RMAN Resync Catalog Command Guide
- Oracle RMAN: Delete Backup Pieces with Manual Channel
- Oracle RMAN Database Backup Commands Guide
- Oracle RMAN Database Restore and Recovery Guide
- Delete Archive Log Older Than 5 Days in Oracle RMAN
- Oracle RMAN Crosscheck Archivelog - Fix RMAN-06059 Error
- Oracle RMAN List Backupset Command Guide
- Oracle RMAN List Backup of Database Command
- List Backup of Archivelog All: Oracle RMAN Command Guide
- Oracle RMAN Report Obsolete Command Guide
- Oracle RMAN Report Obsolete Redundancy Command Guide
- Oracle RMAN Delete Obsolete - Remove Unneeded Backups
- Oracle RMAN RESTORE DATABASE VALIDATE Backup Check
- Oracle RMAN REPORT SCHEMA Command: Display Database Files
- Oracle RMAN Delete Expired Backup: Repository Cleanup
- LIST BACKUPSET OF DATABASE: Oracle RMAN Command Guide
- Oracle RMAN Delete Obsolete Backups with Maintenance Channel
- Find Oracle Archive Logs Older Than N Days with find -mtime
- Audit RMAN Backup Job History with V$RMAN_BACKUP_JOB_DETAILS