Oracle RMAN Backup and Restore Tutorial: Full Backup, Archived Logs and Recovering from Total Loss

RMAN (Recovery Manager) is the backup and recovery tool that ships with every Oracle Database edition at no extra license cost. Because RMAN talks to the database itself, it knows which blocks are in use, never needs tablespaces put in backup mode, records every backup in the control file, and can rebuild a database from nothing but its backup pieces and a parameter file. This tutorial runs a full cycle: take a backup, list and delete backups, back up with archived logs, then simulate a total loss by deleting every data file and control file and recover the database with RMAN. It finishes with the settings you should use on Oracle Database 19c and later, on Linux or Windows.

The example database is called FINANCE, running Oracle Database 11g on Windows, so paths look like C:\oracle\app\product\11.1.0\db_1. Every RMAN command shown works the same on Oracle Linux, RHEL, AlmaLinux or Rocky Linux and on current releases; only the operating system commands differ, and the Linux equivalents are given alongside.

Prerequisites

  • Oracle Database installed, with an OS account in the ORA_DBA group (Windows) or dba group (Linux) so that / OS authentication works.
  • The database in ARCHIVELOG mode if you want to back it up while it is open (online backups). In NOARCHIVELOG mode RMAN can only take consistent backups with the database mounted, not open.
  • Enough disk for the backup pieces. By default, with no recovery area configured, RMAN on Windows writes them to %ORACLE_HOME%\database and on Linux to $ORACLE_HOME/dbs. That is fine for a lab; use a Fast Recovery Area or a dedicated backup mount in production.
  • A copy of the parameter file (spfile or pfile). It is needed to start the instance when everything else is gone.
  • Never practice the restore section on a database you care about. It deletes the database files on purpose.

Step 1: Set ORACLE_SID and Connect to RMAN

RMAN connects to whichever instance ORACLE_SID points at. On Windows:

C:\> cd C:\oracle\app\product\11.1.0\db_1\BIN
C:\oracle\app\product\11.1.0\db_1\BIN> set ORACLE_SID=finance
C:\oracle\app\product\11.1.0\db_1\BIN> echo %ORACLE_SID%
finance
C:\oracle\app\product\11.1.0\db_1\BIN> rman target /

Recovery Manager: Release 11.1.0.6.0 - Production

connected to target database: FINANCE (DBID=2572548424)

RMAN>

On Linux, use . oraenv or export ORACLE_SID=finance and then rman target /. Notice there is no as sysdba: RMAN always connects to the target with SYSDBA (or, from 12c on, SYSBACKUP) privilege, so it is implied. The DBID shown in the banner is worth writing down; you need it if you ever restore a control file from an autobackup.

Step 2: Back Up the Database

RMAN> backup database;

Starting backup at 31-MAY-11
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
input datafile file number=00001 name=C:\DATABASE\ORADATA\FINANCE\SYSTEM01.DBF
input datafile file number=00003 name=C:\DATABASE\ORADATA\FINANCE\UNDOTBS1.DBF
input datafile file number=00002 name=C:\DATABASE\ORADATA\FINANCE\SYSAUX01.DBF
piece handle=C:\ORACLE\APP\PRODUCT\11.1.0\DB_1\DATABASE\01MDOAB8_1_1 tag=TAG20110531T224704
channel ORA_DISK_1: starting full datafile backup set
including current control file in backup set
piece handle=C:\ORACLE\APP\PRODUCT\11.1.0\DB_1\DATABASE\02MDOAC4_1_1 tag=TAG20110531T224704
Finished backup at 31-MAY-11

That single command is a complete backup. RMAN reads the control file to find every data file and writes them into backup sets. Each backup set is stored in one or more backup pieces, the physical files RMAN names with a generated 8-character string plus _1_1 (the %U format). Backing up data file 1 (SYSTEM) also includes the current control file (and the spfile, if the instance uses one) in a second backup set. The line using target database control file instead of recovery catalog just means the backup history is kept in the control file, which is normal when you have no separate recovery catalog database.

Backup pieces are in RMAN’s own binary format. Only RMAN can read them, so do not try to copy data files out of them by hand.

Step 3: List and Delete Backups

RMAN> list backup;

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
1       Full    249.52M    DISK        00:00:27     31-MAY-11
        BP Key: 1   Status: AVAILABLE  Compressed: NO  Tag: TAG20110531T224704
        Piece Name: C:\ORACLE\APP\PRODUCT\11.1.0\DB_1\DATABASE\01MDOAB8_1_1
  List of Datafiles in backup set 1
  File LV Type Ckp SCN    Ckp Time  Name
  ---- -- ---- ---------- --------- ----
  1       Full 735187     31-MAY-11 C:\DATABASE\ORADATA\FINANCE\SYSTEM01.DBF
  2       Full 735187     31-MAY-11 C:\DATABASE\ORADATA\FINANCE\SYSAUX01.DBF
  3       Full 735187     31-MAY-11 C:\DATABASE\ORADATA\FINANCE\UNDOTBS1.DBF

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
2       Full    9.55M      DISK        00:00:10     31-MAY-11
        BP Key: 2   Status: AVAILABLE  Compressed: NO  Tag: TAG20110531T224704
        Piece Name: C:\ORACLE\APP\PRODUCT\11.1.0\DB_1\DATABASE\02MDOAC4_1_1
  Control File Included: Ckp SCN: 735195       Ckp time: 31-MAY-11

list backup reads the RMAN section of the control file. Use list backup summary for a one-line-per-set view. To remove backups:

RMAN> delete backup;
...
Do you really want to delete the above objects (enter YES or NO)? yes
deleted backup piece
backup piece handle=...\01MDOAB8_1_1 RECID=1 STAMP=752626028
deleted backup piece
backup piece handle=...\02MDOAC4_1_1 RECID=2 STAMP=752626062
Deleted 2 objects

delete backup removes both the record in the control file and the files on disk. In real life you would rarely delete everything; use a retention policy and delete obsolete instead (see the modern section).

Step 4: Back Up the Database Plus Archived Logs

For a backup you can recover to the present, include the archived redo logs. If the database is not yet in ARCHIVELOG mode, enable it first (this needs a short outage):

SQL> shutdown immediate
SQL> startup mount
SQL> alter database archivelog;
SQL> alter database open;
SQL> archive log list

Then in RMAN:

RMAN> backup database plus archivelog;

Starting backup at 31-MAY-11
current log archived
channel ORA_DISK_1: starting archived log backup set
input archived log thread=1 sequence=1 ...
input archived log thread=1 sequence=2 ...
piece handle=...\03MDOAKH_1_1
channel ORA_DISK_1: starting full datafile backup set
piece handle=...\04MDOAKL_1_1
including current control file in backup set
piece handle=...\05MDOALH_1_1
current log archived
channel ORA_DISK_1: starting archived log backup set
input archived log thread=1 sequence=3 ...
piece handle=...\06MDOALO_1_1
Finished backup at 31-MAY-11

RMAN switches and archives the current log, backs up the existing archived logs, backs up the data files, backs up the control file, then switches logs again and backs up the newest archived log, so the backup is self-consistent. Here that produced four pieces: archived logs (03MDOAKH_1_1), data files (04MDOAKL_1_1), the control file (05MDOALH_1_1) and the last archived log (06MDOALO_1_1). Note the name of the control file piece; it is needed below.

Step 5: Simulate a Total Loss

On a lab database only: abort the instance and delete every data file, online redo log and control file.

SQL> shutdown abort
C:\database\oradata\finance> del *
C:\database\oradata\finance\*, Are you sure (Y/N)? y

SQL> startup
ORACLE instance started.
...
ORA-00205: error in identifying control file, check alert log for more info
SQL> shutdown abort

The instance starts (it only needs the parameter file and memory), but it cannot mount because the control files are gone.

Step 6: Restore the Control File

C:\> rman target /
connected to target database (not started)

RMAN> startup nomount;
Oracle instance started

RMAN> restore controlfile from 'C:\oracle\app\product\11.1.0\db_1\database\05MDOALH_1_1';

channel ORA_DISK_1: restoring control file
output file name=C:\DATABASE\ORADATA\FINANCE\CONTROL01.CTL
output file name=C:\DATABASE\ORADATA\FINANCE\CONTROL02.CTL
output file name=C:\DATABASE\ORADATA\FINANCE\CONTROL03.CTL
Finished restore at 31-MAY-11

RMAN can start the instance in NOMOUNT itself, so there is no need to switch to SQL*Plus. With no control file there is no backup catalog to read yet, so the first restore must name the backup piece explicitly. RMAN writes as many copies as the control_files parameter lists, three here, even though only one was backed up.

Step 7: Mount, Restore and Recover the Database

RMAN> alter database mount;
database mounted

RMAN> restore database;
channel ORA_DISK_1: restoring datafile 00001 to C:\DATABASE\ORADATA\FINANCE\SYSTEM01.DBF
channel ORA_DISK_1: restoring datafile 00002 to C:\DATABASE\ORADATA\FINANCE\SYSAUX01.DBF
channel ORA_DISK_1: restoring datafile 00003 to C:\DATABASE\ORADATA\FINANCE\UNDOTBS1.DBF
channel ORA_DISK_1: reading from backup piece ...\04MDOAKL_1_1
Finished restore at 31-MAY-11

RMAN> recover database;
starting media recovery
archived log file name=...\ARC00003_0752625569.001 thread=1 sequence=3
unable to find archived log
archived log thread=1 sequence=4
RMAN-03002: failure of recover command
RMAN-06054: media recovery requesting unknown archived log for thread 1 with sequence 4 and starting SCN of 735346

RMAN> alter database open resetlogs;
database opened

Once the control file is mounted, RMAN knows every backup, so restore database needs no file names; it just needs the backup pieces to still be where the control file says they are. recover database applies archived logs until it runs out. Because the online redo logs were deleted too, RMAN-06054 here is expected: it is RMAN saying there is no more redo to apply. Opening with RESETLOGS is mandatory after restoring a backup control file; it creates new online redo logs and starts a new incarnation of the database.

Take a fresh full backup right after any RESETLOGS open. Older backups still work for the previous incarnation, but a new baseline keeps future recoveries simple.

Step 8: Verify

SQL> select name from v$database;
NAME
---------
FINANCE

SQL> select open_mode from v$database;
OPEN_MODE
----------
READ WRITE

SQL> select tablespace_name from dba_tablespaces;
TABLESPACE_NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS1
TEMP

The TEMP tablespace is not backed up by RMAN; its temp files are recreated automatically when the database opens. Also check the alert log for errors and run a quick application query.

Troubleshooting

ErrorWhat to do
ORA-00205 at startupControl files missing or unreadable. Restore them with restore controlfile as in Step 6.
RMAN-06054 during recoverNormal after a full loss: no more archived or online redo exists. Open with RESETLOGS. If you expected more logs, catalog them first with catalog start with '/path/'.
RMAN-06023: no backup or copy of datafile foundThe pieces have been moved or deleted. Run crosscheck backup; and catalog start with the new location.
ORA-19602 or backup fails while openThe database is in NOARCHIVELOG mode. Enable ARCHIVELOG mode, or back up with the database mounted.
ORA-01113 / ORA-01110 at openA data file needs media recovery. Run recover database before opening.
RMAN-06172 on restore controlfile from autobackupRMAN could not find an autobackup. Use set dbid first and check the autobackup format and location, or restore from a named piece.

Modern Notes: Oracle Database 19c and Later

Oracle Database 19c is the long-term support release most shops run, and Oracle AI Database 26ai (the successor to 23ai) is the newest. Everything above still works unchanged; these are the settings to add for a real backup strategy, on Oracle Linux, RHEL, AlmaLinux or Rocky Linux 8 to 10 or on Windows Server:

RMAN> show all;
RMAN> configure controlfile autobackup on;
RMAN> configure retention policy to recovery window of 14 days;
RMAN> configure device type disk parallelism 2 backup type to compressed backupset;
RMAN> configure archivelog deletion policy to backed up 1 times to disk;

RMAN> backup incremental level 0 database plus archivelog;
RMAN> backup incremental level 1 database plus archivelog;
RMAN> list backup summary;
RMAN> report obsolete;
RMAN> crosscheck backup;
RMAN> delete noprompt obsolete;
  • Control file autobackup is on by default from 12.2. With it, a lost control file is restored with set dbid 2572548424; restore controlfile from autobackup; instead of hunting for the piece name.
  • Fast Recovery Area: set db_recovery_file_dest and db_recovery_file_dest_size so backups, archived logs and autobackups go to one managed location instead of $ORACLE_HOME/dbs.
  • Test without restoring: restore database validate; and backup validate check logical database; read the backups or files and report corruption without writing anything.
  • Multitenant: in a CDB, rman target / connects to the root, and backup database backs up the CDB and every PDB. Use backup pluggable database pdb1; or restore pluggable database pdb1; for one PDB.
  • Block change tracking (alter database enable block change tracking;, Enterprise Edition) makes level 1 incrementals much faster.
  • Copies off the server: a backup on the same disks as the database is not a backup. Send pieces to NFS, tape, ZFS Storage Appliance, ZDLRA or object storage with the Oracle Database Cloud Backup Module.
  • Scheduling on Linux: run a command file with rman target / cmdfile=/home/oracle/scripts/level0.rman log=/home/oracle/logs/level0.log from cron or a systemd timer as the oracle user.

For the storage side of an Oracle Linux database server, see Persistent Device Naming for Oracle ASM on Oracle Linux 9.

Based on a recorded RMAN demonstration on Oracle Database 11g for Windows, expanded with explanations, troubleshooting and current-release practices.

Leave a Reply

Your email address will not be published. Required fields are marked *