Skip to content
Commitpedia
Log in Join & edit

RMAN backup basics: ARCHIVELOG mode, a nightly backup script and restore

Enable ARCHIVELOG mode, set an RMAN retention policy, run a nightly full backup with archived logs from cron, and learn the restore and recover sequence.

Drafted with AI from official documentation. Scripts were run in a test environment and repository links verified. Awaiting a human check. Did it work for you? Last edited Outdated?

Edit history of RMAN backup basics: ARCHIVELOG mode, a nightly backup script and restore: 1 recorded edit
Edit history · 1 edit
Type
Script
Edits
1
Editors
1
First issued
15 Oct 2026
Last edit
15 Oct 2026
Language
bash
Licence
CC0-1.0

What this changes

Runs a full RMAN backup of the database and archived redo logs; DELETE NOPROMPT OBSOLETE then permanently removes backups outside the retention policy. Writes a log in /home/oracle/rman-logs and deletes logs older than 30 days.

How to undo it

Removed obsolete backups cannot be restored, so check REPORT OBSOLETE; and the retention policy before the first run. Extra new backups need no undo; later runs prune them.

RMAN (Recovery Manager) is Oracle’s built-in backup tool. It reads data files block by block, records every backup in the control file (or a recovery catalog) and knows which backups it needs for a restore. Commands below are from the Oracle Database 19c documentation and run the same way in later releases.

1. Check and enable ARCHIVELOG mode

To back up an open database and recover to any point in time, the database must run in ARCHIVELOG mode, where filled online redo logs are copied to archived logs. In NOARCHIVELOG mode you can only back up a closed, consistent database.

sqlplus / as sysdba
SQL> ARCHIVE LOG LIST
SQL> SELECT log_mode FROM v$database;

Warning: switching modes needs a restart, so plan downtime. Make sure archived logs have a destination with enough space (a fast recovery area or LOG_ARCHIVE_DEST_1). The database must be mounted but not open:

SQL> SHUTDOWN IMMEDIATE
SQL> STARTUP MOUNT
SQL> ALTER DATABASE ARCHIVELOG;
SQL> ALTER DATABASE OPEN;
SQL> ARCHIVE LOG LIST

Take a full backup right afterwards: the documentation states that backups taken in NOARCHIVELOG mode cannot be used together with the new archived logs. In a CDB this is done once for the whole CDB, connected to the root.

2. One-time RMAN configuration

rman target /

RMAN> SHOW ALL;
RMAN> CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
RMAN> CONFIGURE CONTROLFILE AUTOBACKUP ON;

A recovery window of 7 days keeps whatever is needed to recover to any time in the last week; the default policy is REDUNDANCY 1. Control file autobackup is on by default for CDBs with COMPATIBLE 12.2 or higher, but setting it explicitly does no harm: without a control file backup, a restore onto a new server is much harder. Backups go to the fast recovery area unless you configure another disk location with CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT ....

3. The nightly script

The script writes an RMAN command file, runs it with operating-system authentication and keeps a log per run. Run it as the Oracle software owner on the database server.

#!/usr/bin/env bash
# rman-backup.sh - nightly RMAN backup of the whole CDB plus archived redo logs.
# Run as the oracle software owner. License: CC0-1.0
set -euo pipefail

export ORACLE_SID="${ORACLE_SID:-ORCLCDB}"
export ORACLE_HOME="${ORACLE_HOME:?set ORACLE_HOME}"
export PATH="$ORACLE_HOME/bin:$PATH"
export NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'

LOG_DIR="${LOG_DIR:-/home/oracle/rman-logs}"
KEEP_LOG_DAYS="${KEEP_LOG_DAYS:-30}"

mkdir -p "$LOG_DIR"
stamp="$(date +%Y%m%d-%H%M%S)"
logfile="$LOG_DIR/rman-${ORACLE_SID}-${stamp}.log"
cmdfile="$(mktemp)"
trap 'rm -f "$cmdfile"' EXIT

cat > "$cmdfile" <<'EOF'
CONNECT TARGET /
SHOW RETENTION POLICY;
BACKUP DATABASE PLUS ARCHIVELOG;
CROSSCHECK BACKUP;
DELETE NOPROMPT OBSOLETE;
LIST BACKUP SUMMARY;
EXIT;
EOF

if rman @"$cmdfile" > "$logfile" 2>&1 && ! grep -q 'RMAN-' "$logfile"; then
  echo "RMAN backup OK: $logfile"
  status=0
else
  echo "RMAN backup FAILED, see $logfile" >&2
  grep -E 'RMAN-|ORA-' "$logfile" | head -n 20 >&2 || true
  status=1
fi

find "$LOG_DIR" -maxdepth 1 -type f -name 'rman-*.log' -mtime +"$KEEP_LOG_DAYS" -delete
exit "$status"
  • BACKUP DATABASE PLUS ARCHIVELOG backs up all data files plus the archived redo logs, so the backup can be made consistent during recovery. Run against the root of a CDB, it covers the root and all PDBs.
  • CROSSCHECK BACKUP marks backups whose files have disappeared as EXPIRED.
  • DELETE NOPROMPT OBSOLETE removes backups the retention policy no longer needs. NOPROMPT skips the confirmation question.
  • The script treats any RMAN- message in the log as a failure, which can include warnings. Review those and adjust the check if a warning is expected in your setup.
# crontab of the oracle user
0 1 * * * ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1 ORACLE_SID=ORCLCDB /home/oracle/bin/rman-backup.sh

Archived logs pile up between full backups. On busy systems add a separate job several times a day with BACKUP ARCHIVELOG ALL DELETE INPUT;, which backs up archived logs and then deletes the copies it backed up. For a single PDB, connect to the root and run BACKUP PLUGGABLE DATABASE orclpdb1;. For large databases, look at incremental backups (BACKUP INCREMENTAL LEVEL 0 DATABASE once, then level 1); note that a plain full backup cannot serve as the level 0 base.

4. Check that backups are usable

RMAN> LIST BACKUP SUMMARY;
RMAN> REPORT OBSOLETE;
RMAN> RESTORE DATABASE PREVIEW SUMMARY;
RMAN> RESTORE DATABASE VALIDATE;

RESTORE ... VALIDATE reads the backups as if restoring them, without writing any data files. Run it regularly; a backup that has never been tested is a hope, not a backup.

5. Restore and recover (concept)

Warning: STARTUP FORCE MOUNT aborts the running instance, and RESTORE DATABASE overwrites the current data files. Practise on a test server, and before touching production check with RESTORE DATABASE PREVIEW which backups would be used.

rman target /

RMAN> STARTUP FORCE MOUNT;
RMAN> RESTORE DATABASE;
RMAN> RECOVER DATABASE;
RMAN> ALTER DATABASE OPEN;

RESTORE copies data files back from the backup; RECOVER applies archived and online redo to bring them forward to the latest committed state. Recovering to an earlier time (point-in-time recovery) and restoring single tablespaces or PDBs follow the same restore-then-recover idea with extra clauses described in the Backup and Recovery User’s Guide.

Official documentation

Did this work for you?

No votes yet. Be the first.

If it did not work, the report form below tells the editors what went wrong.

See also

  • 0222 Connecting to Oracle: SQL*Plus, SQLcl, EZConnect, tnsnames.ora and the listener Clients reach an Oracle database through the listener, using an Easy Connect string or a tnsnames.ora alias. Here are the formats, the… Intermediate · Cheat sheet · Linux, macOS, Windows Article Administration · Concepts & tools 15 Oct 2026
  • 0223 Oracle users, roles and tablespaces: setup script and usage report (SQL) A SQL*Plus/SQLcl script that creates a tablespace, a role and an application user inside a PDB, then reports tablespace usage from the… Intermediate · Step by step · Any system Script Administration · Security & access 15 Oct 2026
  • 0225 Oracle Data Pump: export and import schemas with expdp and impdp Data Pump moves schemas, tables or whole databases between Oracle databases as dump files. Set up a directory object, export with expdp… Intermediate · Step by step · Linux, Windows Article Administration · Backup with RMAN 15 Oct 2026
  • 0226 Fix: ORA-12541: TNS:no listener The client reached the host but nothing was listening on that port. Start or fix the Oracle listener, or correct the host… Intermediate · Fixes an error · Linux, Windows Article Administration · Concepts & tools 15 Oct 2026
  • 0227 Fix: ORA-01017: invalid username/password; logon denied Oracle rejected the login. Check the password (case-sensitive), that you connect to the right PDB service, and whether the account is locked… Intermediate · Fixes an error · Any system Article Administration · Security & access 15 Oct 2026

History

Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.

  1. Dominik +6,047 chars

Report a problem

Something wrong, unsafe or copied? Tell the editors.
What is wrong?