RMAN Hot Backup Script
RMAN Hot Backup Script
The following unix shell script will do a full RMAN hot backup to your database and will copy the backup files compressed to the directory you will specify. This is a hot-backup, and the database must to be in ARCHIVELOG mode for this to work.
A backup retention policy with a recovery window of 2 days is defined in this script. With this retention policy RMAN will keep archive logs and backup files necessary to recover to any point of time within those 2 days in the recovery window. Older backups and archivelogs not needed to satisfy this retention policy will be automatically deleted by this script. No RECOVERY CATALOG is being used with this script, instead database controlfiles are used to record the RMAN repository information.
Make sure you set the rman CONFIGURE CONTROLFILE AUTOBACKUP parameter to ON in in RMAN in order to take extra backups of the controlfile and spfile (RMAN will not backup init.ora files) as extra protection.
This script will delete obsolete backups and not needed archive logs from the disks only after a successful backup. This means you don’t need to set up cronjobs or manually delete not needed backups. This is the beauty of using RMAN. It does this automatically with the commands: “…DELETE NOPROMPT OBSOLETE;…” and “…DELETE NOPROMPT EXPIRED BACKUP…” See last lines in the script.
The RMAN hot backup script rman_backup.sh
# Declare your ORACLE environment variables
export ORACLE_SID= (put your SID here)
export ORACLE_BASE= (put your ORACLE BASE here)
export ORACLE_HOME= (put your ORACLE_HOME here)
export PATH=$PATH:${ORACLE_HOME}/bin
# Start the rman commands
rman target=/ << EOF
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/u01/autobackup_control_file%F';
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 2 DAYS;
run {
ALLOCATE CHANNEL RMAN_BACK_CH01 TYPE DISK;
CROSSCHECK BACKUP;
BACKUP AS COMPRESSED BACKUPSET DATABASE FORMAT '/u01/databasefiles_%d_%u_%s_%T';
sql ‘ALTER SYSTEM ARCHIVE LOG CURRENT’;
BACKUP AS COMPRESSED BACKUPSET ARCHIVELOG ALL FORMAT '/u01/archivelogs_%d_%u_%s_%T' DELETE INPUT;
BACKUP AS COMPRESSED BACKUPSET CURRENT CONTROLFILE FORMAT '/u01/controlfile_%d_%u_%s_%T';
CROSSCHECK BACKUP;
DELETE NOPROMPT OBSOLETE;
DELETE NOPROMPT EXPIRED BACKUP;
RELEASE CHANNEL RMAN_BACK_CH01;
}
EXIT;
EOF
#
Note: If you run this script on a database which was being backed up using OS methods and user managed backups, initially it will fail as it will look to satisfy the ‘… ARCHIVELOG ALL…” clause. That is it will try to backup archivelogs since day 1. Well, unless you have lots of money to allocate for storage, we know that is not practical to keep all archivelogs and with OS backups usually we delete them manually according to the chosen backup retention pollicy. The script once run initially will fail like this:
oracle@localhost.localdomain:/u01 [TESTDB] >tail -f nohup.out
Starting backup at 24-FEB-17
channel RMAN_BACK_CH01: starting compressed full datafile backup set
channel RMAN_BACK_CH01: specifying datafile(s) in backup set
input datafile file number=00001 name=/u01/app/oracle/oradata/TESTDB/system01.dbf
input datafile file number=00002 name=/u01/app/oracle/oradata/TESTDB/sysaux01.dbf
input datafile file number=00005 name=/u01/app/oracle/oradata/TESTDB/example01.dbf
input datafile file number=00003 name=/u01/app/oracle/oradata/TESTDB/undotbs01.dbf
input datafile file number=00004 name=/u01/app/oracle/oradata/TESTDB/users01.dbf
channel RMAN_BACK_CH01: starting piece 1 at 24-FEB-17
channel RMAN_BACK_CH01: finished piece 1 at 24-FEB-17
piece handle=/u01/databasefiles_TESTDB_08rtdekk_8_20170224 tag=TAG20170224T193547 comment=NONE
channel RMAN_BACK_CH01: backup set complete, elapsed time: 00:01:25
Finished backup at 24-FEB-17
Starting Control File and SPFILE Autobackup at 24-FEB-17
piece handle=/u01/autobackup_control_filec-2720074507-20170224-01 comment=NONE
Finished Control File and SPFILE Autobackup at 24-FEB-17
sql statement: ALTER SYSTEM ARCHIVE LOG CURRENT
Starting backup at 24-FEB-17
current log archived
channel RMAN_BACK_CH01: starting compressed archived log backup set
channel RMAN_BACK_CH01: specifying archived log(s) in backup set
input archived log thread=1 sequence=4 RECID=1 STAMP=936819434
input archived log thread=1 sequence=5 RECID=2 STAMP=936819434
channel RMAN_BACK_CH01: starting piece 1 at 24-FEB-17
channel RMAN_BACK_CH01: finished piece 1 at 24-FEB-17
piece handle=/u01/archivelogs_TESTDB_0artdenb_10_20170224 tag=TAG20170224T193714 comment=NONE
channel RMAN_BACK_CH01: backup set complete, elapsed time: 00:00:01
channel RMAN_BACK_CH01: deleting archived log(s)
archived log file name=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_4_936746381.dbf RECID=1 STAMP=936819434
archived log file name=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_5_936746381.dbf RECID=2 STAMP=936819434
Finished backup at 24-FEB-17
Starting backup at 24-FEB-17
channel RMAN_BACK_CH01: starting compressed full datafile backup set
channel RMAN_BACK_CH01: specifying datafile(s) in backup set
including current control file in backup set
channel RMAN_BACK_CH01: starting piece 1 at 24-FEB-17
channel RMAN_BACK_CH01: finished piece 1 at 24-FEB-17
piece handle=/u01/controlfile_TESTDB_0brtdenc_11_20170224 tag=TAG20170224T193716 comment=NONE
channel RMAN_BACK_CH01: backup set complete, elapsed time: 00:00:01
Finished backup at 24-FEB-17
Starting Control File and SPFILE Autobackup at 24-FEB-17
piece handle=/u01/autobackup_control_filec-2720074507-20170224-02 comment=NONE
Finished Control File and SPFILE Autobackup at 24-FEB-17
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/02rtb8fk_1_1 RECID=1 STAMP=936747510
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/rman_bkp_04rtb8gl_1_1 RECID=2 STAMP=936747542
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/controlfile_TESTDB_05rtdegg_5_20170224 RECID=3 STAMP=936819217
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/autobackup_control_filec-2720074507-20170224-00 RECID=4 STAMP=936819218
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/databasefiles_TESTDB_08rtdekk_8_20170224 RECID=5 STAMP=936819348
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/autobackup_control_filec-2720074507-20170224-01 RECID=6 STAMP=936819433
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/archivelogs_TESTDB_0artdenb_10_20170224 RECID=7 STAMP=936819435
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/controlfile_TESTDB_0brtdenc_11_20170224 RECID=8 STAMP=936819437
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/autobackup_control_filec-2720074507-20170224-02 RECID=9 STAMP=936819438
Crosschecked 9 objects
RMAN retention policy will be applied to the command
RMAN retention policy is set to recovery window of 2 days
no obsolete backups found
specification does not match any backup in the repository
released channel: RMAN_BACK_CH01
RMAN>
Recovery Manager complete.
As you can see the script assumes that you have all archivelogs, if that is not the case (most likely), and you have been periodically deleting them ie. crontab, or manually, you can always sync the existing RMAN catalog (or controlfile) by crosschecking what is there on the disk before you run it with the following command from RMAN:
For before Oracle 9i
RMAN> change archivelog all crosscheck;
or in Oracle 9i,10g
RMAN> crosscheck archivelog all;
This will crosscheck existing archivelogs and will show you the ones which are EXPIRED and need to be deleted from the RMAN catalog, that is, they are not on the disks anymore, cause you have deleted them not using RMAN. RMAN doesn’t know this, it expects them! To delete these misleading entries from your control file you will have to run the command:
RMAN> delete expired archivelog all;
After you delete the expired archivelog records from the control file you can run the script and take your backups.
To see how a recovery is done with a backup taken with this script on a New Host with the same directory structure search this blog for RMAN Recovery.
The following unix shell script will do a full RMAN hot backup to your database and will copy the backup files compressed to the directory you will specify. This is a hot-backup, and the database must to be in ARCHIVELOG mode for this to work.
A backup retention policy with a recovery window of 2 days is defined in this script. With this retention policy RMAN will keep archive logs and backup files necessary to recover to any point of time within those 2 days in the recovery window. Older backups and archivelogs not needed to satisfy this retention policy will be automatically deleted by this script. No RECOVERY CATALOG is being used with this script, instead database controlfiles are used to record the RMAN repository information.
Make sure you set the rman CONFIGURE CONTROLFILE AUTOBACKUP parameter to ON in in RMAN in order to take extra backups of the controlfile and spfile (RMAN will not backup init.ora files) as extra protection.
This script will delete obsolete backups and not needed archive logs from the disks only after a successful backup. This means you don’t need to set up cronjobs or manually delete not needed backups. This is the beauty of using RMAN. It does this automatically with the commands: “…DELETE NOPROMPT OBSOLETE;…” and “…DELETE NOPROMPT EXPIRED BACKUP…” See last lines in the script.
The RMAN hot backup script rman_backup.sh
# Declare your ORACLE environment variables
export ORACLE_SID= (put your SID here)
export ORACLE_BASE= (put your ORACLE BASE here)
export ORACLE_HOME= (put your ORACLE_HOME here)
export PATH=$PATH:${ORACLE_HOME}/bin
# Start the rman commands
rman target=/ << EOF
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/u01/autobackup_control_file%F';
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 2 DAYS;
run {
ALLOCATE CHANNEL RMAN_BACK_CH01 TYPE DISK;
CROSSCHECK BACKUP;
BACKUP AS COMPRESSED BACKUPSET DATABASE FORMAT '/u01/databasefiles_%d_%u_%s_%T';
sql ‘ALTER SYSTEM ARCHIVE LOG CURRENT’;
BACKUP AS COMPRESSED BACKUPSET ARCHIVELOG ALL FORMAT '/u01/archivelogs_%d_%u_%s_%T' DELETE INPUT;
BACKUP AS COMPRESSED BACKUPSET CURRENT CONTROLFILE FORMAT '/u01/controlfile_%d_%u_%s_%T';
CROSSCHECK BACKUP;
DELETE NOPROMPT OBSOLETE;
DELETE NOPROMPT EXPIRED BACKUP;
RELEASE CHANNEL RMAN_BACK_CH01;
}
EXIT;
EOF
#
Note: If you run this script on a database which was being backed up using OS methods and user managed backups, initially it will fail as it will look to satisfy the ‘… ARCHIVELOG ALL…” clause. That is it will try to backup archivelogs since day 1. Well, unless you have lots of money to allocate for storage, we know that is not practical to keep all archivelogs and with OS backups usually we delete them manually according to the chosen backup retention pollicy. The script once run initially will fail like this:
oracle@localhost.localdomain:/u01 [TESTDB] >tail -f nohup.out
Starting backup at 24-FEB-17
channel RMAN_BACK_CH01: starting compressed full datafile backup set
channel RMAN_BACK_CH01: specifying datafile(s) in backup set
input datafile file number=00001 name=/u01/app/oracle/oradata/TESTDB/system01.dbf
input datafile file number=00002 name=/u01/app/oracle/oradata/TESTDB/sysaux01.dbf
input datafile file number=00005 name=/u01/app/oracle/oradata/TESTDB/example01.dbf
input datafile file number=00003 name=/u01/app/oracle/oradata/TESTDB/undotbs01.dbf
input datafile file number=00004 name=/u01/app/oracle/oradata/TESTDB/users01.dbf
channel RMAN_BACK_CH01: starting piece 1 at 24-FEB-17
channel RMAN_BACK_CH01: finished piece 1 at 24-FEB-17
piece handle=/u01/databasefiles_TESTDB_08rtdekk_8_20170224 tag=TAG20170224T193547 comment=NONE
channel RMAN_BACK_CH01: backup set complete, elapsed time: 00:01:25
Finished backup at 24-FEB-17
Starting Control File and SPFILE Autobackup at 24-FEB-17
piece handle=/u01/autobackup_control_filec-2720074507-20170224-01 comment=NONE
Finished Control File and SPFILE Autobackup at 24-FEB-17
sql statement: ALTER SYSTEM ARCHIVE LOG CURRENT
Starting backup at 24-FEB-17
current log archived
channel RMAN_BACK_CH01: starting compressed archived log backup set
channel RMAN_BACK_CH01: specifying archived log(s) in backup set
input archived log thread=1 sequence=4 RECID=1 STAMP=936819434
input archived log thread=1 sequence=5 RECID=2 STAMP=936819434
channel RMAN_BACK_CH01: starting piece 1 at 24-FEB-17
channel RMAN_BACK_CH01: finished piece 1 at 24-FEB-17
piece handle=/u01/archivelogs_TESTDB_0artdenb_10_20170224 tag=TAG20170224T193714 comment=NONE
channel RMAN_BACK_CH01: backup set complete, elapsed time: 00:00:01
channel RMAN_BACK_CH01: deleting archived log(s)
archived log file name=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_4_936746381.dbf RECID=1 STAMP=936819434
archived log file name=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_5_936746381.dbf RECID=2 STAMP=936819434
Finished backup at 24-FEB-17
Starting backup at 24-FEB-17
channel RMAN_BACK_CH01: starting compressed full datafile backup set
channel RMAN_BACK_CH01: specifying datafile(s) in backup set
including current control file in backup set
channel RMAN_BACK_CH01: starting piece 1 at 24-FEB-17
channel RMAN_BACK_CH01: finished piece 1 at 24-FEB-17
piece handle=/u01/controlfile_TESTDB_0brtdenc_11_20170224 tag=TAG20170224T193716 comment=NONE
channel RMAN_BACK_CH01: backup set complete, elapsed time: 00:00:01
Finished backup at 24-FEB-17
Starting Control File and SPFILE Autobackup at 24-FEB-17
piece handle=/u01/autobackup_control_filec-2720074507-20170224-02 comment=NONE
Finished Control File and SPFILE Autobackup at 24-FEB-17
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/02rtb8fk_1_1 RECID=1 STAMP=936747510
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/rman_bkp_04rtb8gl_1_1 RECID=2 STAMP=936747542
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/controlfile_TESTDB_05rtdegg_5_20170224 RECID=3 STAMP=936819217
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/autobackup_control_filec-2720074507-20170224-00 RECID=4 STAMP=936819218
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/databasefiles_TESTDB_08rtdekk_8_20170224 RECID=5 STAMP=936819348
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/autobackup_control_filec-2720074507-20170224-01 RECID=6 STAMP=936819433
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/archivelogs_TESTDB_0artdenb_10_20170224 RECID=7 STAMP=936819435
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/controlfile_TESTDB_0brtdenc_11_20170224 RECID=8 STAMP=936819437
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/autobackup_control_filec-2720074507-20170224-02 RECID=9 STAMP=936819438
Crosschecked 9 objects
RMAN retention policy will be applied to the command
RMAN retention policy is set to recovery window of 2 days
no obsolete backups found
specification does not match any backup in the repository
released channel: RMAN_BACK_CH01
RMAN>
Recovery Manager complete.
As you can see the script assumes that you have all archivelogs, if that is not the case (most likely), and you have been periodically deleting them ie. crontab, or manually, you can always sync the existing RMAN catalog (or controlfile) by crosschecking what is there on the disk before you run it with the following command from RMAN:
For before Oracle 9i
RMAN> change archivelog all crosscheck;
or in Oracle 9i,10g
RMAN> crosscheck archivelog all;
This will crosscheck existing archivelogs and will show you the ones which are EXPIRED and need to be deleted from the RMAN catalog, that is, they are not on the disks anymore, cause you have deleted them not using RMAN. RMAN doesn’t know this, it expects them! To delete these misleading entries from your control file you will have to run the command:
RMAN> delete expired archivelog all;
After you delete the expired archivelog records from the control file you can run the script and take your backups.
To see how a recovery is done with a backup taken with this script on a New Host with the same directory structure search this blog for RMAN Recovery.
Comments
Post a Comment