Oracle RMAN Database Backup Commands Guide

Affiliate Disclosure: This post contains affiliate links. Oracle Scripts may earn a commission if you buy through them, at no extra cost to you.

Oracle RMAN Database Backup Commands Guide

Purpose

A nightly backup job for an Oracle database must do four things. It must copy the data files, copy the archived redo logs, keep the control file safe, and remove old logs so the disk does not fill. The five RMAN commands below do these jobs. Each one suits a different situation.

The first command backs up the database and all archived logs in one line. The second and third use a RUN block with a manual channel, so you choose the output path and the clean-up rule. The fourth makes a cold backup: it stops the database, mounts it, and backs it up. The fifth backs up the archived logs with a default name and then deletes them.

Work through the commands in order. Then use the format code table to name your backup files in a clear way. The hot and cold backup choice depends on the archive log mode of your database, and the sections below explain how.

Code

1backup database plus archivelog format '/u01/ora_backup/rman/%d_%u_%s';
1run {
2allocate channel t1 type disk;
3backup current controlfile format '/u01/ora_backup/rman/%d_%u_%s';
4backup database format '/u01/ora_backup/rman/%d_%u_%s';
5backup archivelog all delete input format '/u01/ora_backup/rman/arch_%d_%u_%s';
6release channel t1;
7}
1run {
2allocate channel t1 type disk;
3backup archivelog all delete input format '/u01/ora_backup/rman/arch_%d_%u_%s';
4release channel t1;
5}
1run {
2allocate channel t1 type disk;
3shutdown immediate;
4startup mount;
5backup database include current controlfile format '/u01/ora_backup/rman/%d_%u_%s';
6alter database open;
7}
1run {
2     allocate channel  t1 type disk;
3     backup archivelog all delete input;
4}

Code Breakdown

Command 1: database plus archived logs

backup database plus archivelog backs up the database and its archived logs in one command. The plus archivelog clause makes RMAN do five steps. It runs ALTER SYSTEM ARCHIVE LOG CURRENT, runs BACKUP ARCHIVELOG ALL, and backs up the files named in the command. It then runs ALTER SYSTEM ARCHIVE LOG CURRENT a second time and backs up any archived logs that appeared during the backup.

The format clause names the output files. Here %d is the database name, %u is a short unique name, and %s is the backup set number. The format table below gives the full meaning of each code.

Command 2: a RUN block with a manual channel

A RUN block groups commands so RMAN runs them as one job. allocate channel t1 type disk opens a channel. A channel is a connection between RMAN and the database instance. Each channel starts a server session on the database, and that session does the backup work.

The job then runs three backups in order:

  1. backup current controlfile writes a backup of the control file. The control file holds the structure of the database.
  2. backup database backs up the data files.
  3. backup archivelog all delete input backs up every archived log and then deletes the input files. RMAN deletes them only after the backup succeeds.

The last line, release channel t1, frees the channel. You can leave it out, because RMAN releases normal channels by itself when the RUN command ends. Keeping it makes the end of the channel scope easy to see.

Command 3: archived logs only

This job backs up all archived logs and then removes them from disk, subject to the deletion policy described under best practices below. The arch_ prefix in the format string keeps log backups easy to tell apart from data file backups when you list the backup directory.

Command 4: a cold backup

A cold backup stops the database first. shutdown immediate closes the database. startup mount opens the control file but keeps the data files closed. backup database include current controlfile then backs up the data files and puts a snapshot of the control file into one of the backup sets. alter database open returns the database to normal use.

Command 5: archived logs with the default name

This job leaves out the format clause. RMAN then uses %U to name each piece. If a fast recovery area is on, RMAN writes the backup there. If it is off, RMAN writes to a platform-specific default location.

Key Points

  • Hot backups need the archived logs. A backup made while the database is open is inconsistent. You must apply redo after you restore it. If you have the archived logs, an open database backup protects data as well as a consistent one.
  • Open backups need ARCHIVELOG mode. If the database runs in ARCHIVELOG mode, you can back it up while it is open. In NOARCHIVELOG mode, the only valid database backup is a consistent one.
  • A cold backup needs a clean shutdown. The shutdown is clean only if you use the NORMAL, IMMEDIATE or TRANSACTIONAL option. You cannot back up a NOARCHIVELOG database with RMAN after an instance failure or after SHUTDOWN ABORT.
  • Format codes must make unique names. The %s counter lives in the control file. It starts at 1 and is unique for the lifetime of that control file, and a restored backup control file can bring back old numbers.
  • delete input removes the files after backup. It acts like running the DELETE command on the input files. Without the ALL keyword, RMAN deletes only the one copy it backed up.

Insights and Best Practices

Choose hot or cold from the log mode

Start with the archive log mode of the database. In ARCHIVELOG mode, the database can be mounted or open during the backup, so command 1 or command 2 is the normal choice. In NOARCHIVELOG mode, the database must be mounted after a clean shutdown, so command 4 is the pattern to use. A cold backup gives a consistent copy. You can restore it without redo, but you lose any work done after the backup. If you have archived logs, you can also apply them to a consistent backup to cut data loss.

Prefer automatic channels for daily jobs

Oracle recommends automatic channels over manual ones. You set automatic channels with CONFIGURE, and they persist across RMAN sessions. By default, parallelism is set to 1. If you set parallelism for disk to 2 and the default device is disk, RMAN allocates two disk channels when you run BACKUP DATABASE. A manual channel exists only in the block where you issue the command. Once you allocate a manual channel, you cannot use BACKUP DEVICE TYPE to reach the automatic channels in that block.

Back up archived logs often

Back up archived redo logs on a regular schedule. The delete input clause deletes logs after they are backed up, and RMAN uses the configured archived log deletion policy (CONFIGURE ARCHIVELOG DELETION POLICY TO BACKED UP) to decide whether a log can go. Without ALL, RMAN deletes only the copy it backed up, so with several archive destinations the other copies stay on disk and need their own clean-up. Run the log job often enough that your archive destination never runs out of space.

Use a CDB with care

In a container database, you can back up archived logs only when you connect to the root as a common user with the SYSDBA or SYSBACKUP privilege. RMAN can back up a whole CDB, the root, one or more PDBs, or tablespaces in a PDB.

RMAN Format Codes

CodeMeaning
%dName of the database
%u8-character name made from compressed forms of the backup set number and the creation time
%sBackup set number, a counter in the control file
%tBackup set time stamp, a 4-byte value in seconds since a fixed reference time
%TYear, month and day in the format YYYYMMDD
%pPiece number inside the backup set, starting at 1
%USystem-generated unique name (default). For a backup piece it is short for %u_%p_%c

When to Run This

  • Run command 1 for a simple full backup of a database in ARCHIVELOG mode.
  • Run command 2 when you want the control file, data files and logs written with different names in one job.
  • Run command 3 or command 5 on a short schedule to keep archived logs from filling the disk.
  • Run command 4 inside a maintenance window for a consistent copy, or when the database is in NOARCHIVELOG mode.
  • Run any of them before a risky change, such as a patch or a storage move.

Troubleshooting Common Issues

If a backup piece name repeats, check your format string. The %s counter restarts at 1 when you run CREATE CONTROLFILE, and a restored backup control file can produce repeat numbers. Use %U, or add %t to the name. A mix of %s and %t forms a unique name for the backup set.

If the cold backup fails after a crash, check how the database stopped. RMAN cannot back up a NOARCHIVELOG database after SHUTDOWN ABORT or an instance failure. Open the database, shut it down cleanly, and run the job again.

If you want the control file inside the same backup as the data files, add include current controlfile. The reference describes this clause as the way to force the current control file into the backup when CONTROLFILE AUTOBACKUP is ON.

If the log job reports no logs to back up, RMAN does not treat it as an error. That result usually means no new logs appeared since the last BACKUP ARCHIVELOG ALL DELETE INPUT run.

Related books recommended

Links go to Amazon. As an Amazon Associate, Oracle Scripts earns from qualifying purchases; your price is unchanged.

References

Posts in this series