Skip to content
Home » Oracle Database » Scripts to Backup and Restore Oracle Home

Scripts to Backup and Restore Oracle Home

There're several items that we may consider to backup periodically:

  • Oracle inventory
  • It's a centralized repository of Oracle software installed on the server. I have specifically talked about backup and restore on Oracle inventory.

  • Oracle home
  • This is the main location of Oracle software of database server, it could be altered by patch or encrypted by someone unknown.

  • Grid home (Grid infra)
  • Grid infrastructure is all about clusterware inside, if it's in wrong configuration, all of your RAC databases on the servers do not work.

As for how to restore Oracle software, I will explain in the second half.

Backup Oracle Product

First of all, we need a space to store our backups by root.

[root@primary01 ~]# mkdir -p /backup/homes/dumps
[root@primary01 ~]# mkdir /backup/homes/logs

Then we edit an execution file for backing up the three items.

[root@primary01 ~]# vi /backup/homes/backup_oracle_software.sh
#!/bin/bash

# 0. General Settings
DUMP_DIR=/backup/homes/dumps
LOG_DIR=/backup/homes/logs
find $LOG_DIR -type f -name '*.log' -mtime +21 -exec rm -f {} \;


# 1. Oracle Inventory Backup

    # 1.1 Settings
    UPPER_DIR=/u01/app
    BACKUP_DIR=oraInventory
    ACRONYM=OI

    # 1.2 Backups Rotation
    rm -f $DUMP_DIR/"$ACRONYM"_BACKUP_02.tar.gz
    mv $DUMP_DIR/"$ACRONYM"_BACKUP_01.tar.gz $DUMP_DIR/"$ACRONYM"_BACKUP_02.tar.gz
    mv $DUMP_DIR/"$ACRONYM"_BACKUP_00.tar.gz $DUMP_DIR/"$ACRONYM"_BACKUP_01.tar.gz
    DUMP_FILE=$DUMP_DIR/"$ACRONYM"_BACKUP_00.tar.gz
    LOG_FILE=$LOG_DIR/"$ACRONYM"_BACKUP_`date +%Y%m%d`.log

    # 1.3 Tar Archive
    cd $UPPER_DIR
    tar -zcvf $DUMP_FILE $BACKUP_DIR > $LOG_FILE


# 2. Oracle Home Backup

    # 2.1 Settings
    UPPER_DIR=/u01/app/oracle/product/12.1.0
    BACKUP_DIR=db_1
    ACRONYM=OH

    # 2.2 Backups Rotation
    rm -f $DUMP_DIR/"$ACRONYM"_BACKUP_02.tar.gz
    mv $DUMP_DIR/"$ACRONYM"_BACKUP_01.tar.gz $DUMP_DIR/"$ACRONYM"_BACKUP_02.tar.gz
    mv $DUMP_DIR/"$ACRONYM"_BACKUP_00.tar.gz $DUMP_DIR/"$ACRONYM"_BACKUP_01.tar.gz
    DUMP_FILE=$DUMP_DIR/"$ACRONYM"_BACKUP_00.tar.gz
    LOG_FILE=$LOG_DIR/"$ACRONYM"_BACKUP_`date +%Y%m%d`.log

    # 2.3 Tar Archive
    cd $UPPER_DIR
    tar -zcvf $DUMP_FILE $BACKUP_DIR > $LOG_FILE


# 3. Grid Infra Backup

    # 3.1 Settings
    UPPER_DIR=/u01/app/12.1.0
    BACKUP_DIR=grid
    ACRONYM=GI

    # 3.2 Backups Rotation
    rm -f $DUMP_DIR/"$ACRONYM"_BACKUP_02.tar.gz
    mv $DUMP_DIR/"$ACRONYM"_BACKUP_01.tar.gz $DUMP_DIR/"$ACRONYM"_BACKUP_02.tar.gz
    mv $DUMP_DIR/"$ACRONYM"_BACKUP_00.tar.gz $DUMP_DIR/"$ACRONYM"_BACKUP_01.tar.gz
    DUMP_FILE=$DUMP_DIR/"$ACRONYM"_BACKUP_00.tar.gz
    LOG_FILE=$LOG_DIR/"$ACRONYM"_BACKUP_`date +%Y%m%d`.log

    # 3.3 Tar Archive
    cd $UPPER_DIR
    tar -zcvf $DUMP_FILE $BACKUP_DIR > $LOG_FILE

Don't forget to make it executable.

[root@primary01 ~]# chmod u+x /backup/homes/backup_oracle_software.sh

Our goal of backup strategy is to rotate 3 generations of backups on a regular basis, that's why we delete the oldest backups and rename the other backups in the script. Generation 00 means the current and newest backup, 01 means the last backup and 02 means the oldest backup.

Moreover, we use tar -zcvf to pack and compress the target folder in the script for keeping the original permission, owner and group of all files.

If you'd like to apply the backup script in your environment, only section 0, 1.1, 2.1, and 3.1 should be tailored for yourself.

Restore Oracle Software

Please note that, you have to shutdown all Oracle database services on the database server before restoring Oracle software. This could include database and all cluster services on this node.

For single-instance databases on file system.

SQL> shutdown immediate;

For cluster databases on Grid.

[root@primary01 ~]# export PATH=$PATH:/u01/app/12.1.0/grid/bin
[root@primary01 ~]# crsctl stop cluster -n primary01

The command close all cluster services in node primary01.

Let's see the restoring script as below.

[root@primary01 ~]# vi /backup/homes/restore_oracle_software.sh
#!/bin/bash

# 0. General Settings
DUMP_DIR=/backup/homes/dumps
LOG_DIR=/backup/homes/logs
#GEN_NO=00


# 1. Oracle Inventory Restore

    # 1.1 Settings
    UPPER_DIR=/u01/app
    ACRONYM=OI

    # 1.2 Restores Rotation
    DUMP_FILE=$DUMP_DIR/"$ACRONYM"_BACKUP_"$GEN_NO".tar.gz
    LOG_FILE=$LOG_DIR/"$ACRONYM"_RESTORE_`date +%Y%m%d`.log

    # 1.3 Extract Archive
    cd $UPPER_DIR
    tar -zxvf $DUMP_FILE > $LOG_FILE


# 2. Oracle Home Restore

    # 2.1 Settings
    UPPER_DIR=/u01/app/oracle/product/12.1.0
    ACRONYM=OH

    # 2.2 Restores Rotation
    DUMP_FILE=$DUMP_DIR/"$ACRONYM"_BACKUP_"$GEN_NO".tar.gz
    LOG_FILE=$LOG_DIR/"$ACRONYM"_RESTORE_`date +%Y%m%d`.log

    # 2.3 Extract Archive
    cd $UPPER_DIR
    tar -zxvf $DUMP_FILE > $LOG_FILE


# 3. Grid Infra Restore

    # 3.1 Settings
    UPPER_DIR=/u01/app/12.1.0
    ACRONYM=GI

    # 3.2 Restores Rotation
    DUMP_FILE=$DUMP_DIR/"$ACRONYM"_BACKUP_"$GEN_NO".tar.gz
    LOG_FILE=$LOG_DIR/"$ACRONYM"_RESTORE_`date +%Y%m%d`.log

    # 3.3 Extract Archive
    cd $UPPER_DIR
    tar -zxvf $DUMP_FILE > $LOG_FILE

Don't forget to make it executable.

[root@primary01 ~]# chmod u+x /backup/homes/restore_oracle_software.sh

As you can see, I comment out the line of GEN_NO=00 to prevent any restoring by accident. Once you are prepared, you can decide which generation number should be used and remove the comment sign #.

Same as the backup script, you should modify section 0, 1.1, 2.1 and 3.1 for your environment.

Of course, you should startup your database after all software are restored.

Leave a Reply

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