Logo

UNIX Shell Script

Cold Database Backup

Tested on Oracle 8.0 Tested on Oracle 7

Updated : 08-Jan-2002
Version : 1.1

Description

Retreives a list of all database files from the database whilst still running and generates a Shell script to do a unix copy to a directory and a shell script to run the dbverify utility. The datafiles can be copied to tape drive from the disk copy. Implementing the backup this way via a disk to disk copy reduces the database down time. This script uses the assumes you've copied the shutdown database script as shutdown_db.ksh.

Parameters

$1 (MANDATORY) - ORACLE_SID of the database to backup
$2 (MANDATORY) - The directory to backup the database
$3 (MANDATORY) - The database connect string to export from.

Shell Source

#!/bin/ksh
DATABASE=$1
ORA_BACKUP=$2; export ORA_BACKUP
USERPASS=$3
# Check that we have a database
if [ -z "${DATABASE}" ]
then
   echo
   echo "No Database Specified !"
   echo
   echo "Usage : cold_backup.ksh <ORACLE_SID> <BACKUP DIRECTORY> <CONNECT STRING>"
   echo
   echo "where ORACLE_SID (Mandatory) - SID of database to start"
   ehco "      BACKUP DIRECTORY - The location of the directory to backup the files to."
   echo "      CONNECT - Username/Password of the database user to export from."
   echo
   echo " e.g.  $ cold_backup.ksh PROD system/manager" 
   echo
   exit 1
fi
# Check we have an Oracle username and password
if [ -z "${USERPASS}" ]
then
   echo
   echo "No Database connect string specified !"
   echo
   echo "Usage : cold_backup.ksh <ORACLE_SID> <BACKUP DIRECTORY> <CONNECT STRING>"
   echo
   echo "where ORACLE_SID (Mandatory) - SID of database to start"
   ehco "      BACKUP DIRECTORY - The location of the directory to backup the files to."
   echo "      CONNECT - Username/Password of the database user to export from."
   echo
   echo " e.g.  $ cold_backup.ksh PROD system/manager" 
   echo
   exit 1
fi
# Check the backup directory exists
if [ ! -d "${ORA_BACKUP}" ]
then
  echo
  echo "$ORA_BACKUP does not exist !"
  echo
  exit 1
fi
# Get the ORACLE_HOME from the oratab file
ORACLE_HOME=`cat /etc/oratab | grep $DATABASE | awk -F: '{print $2}'`
if [ ! -d "${ORACLE_HOME}" ]
then
  echo
  echo "$ORACLE_HOME does not exist !"
  echo
  exit 1
fi
ORACLE_SID=$DATABASE
export ORACLE_SID
PATH=.:$ORACLE_HOME/bin:$PATH; export PATH
# See if we can find svrmgrl, if not use sqldba instead
if [ -x "${ORACLE_HOME}/bin/svrmgrl" ]
then
    SVR="svrmgrl"
else
    SVR="sqldba lmode=y"
fi
# GET A LIST OF ALL THE DATABASE DATAFILES
# 
 sqlplus -s $USERPASS << EOF
set feed off pages 0 head off echo off line 250
spool /tmp/copy1.ksh
select ' echo "Backing Up All Datafiles ..."' from dual;
select ' cp -p '||file_name||
       ' ${ORA_BACKUP}/'||
       substr(file_name,instr(file_name,'/')+1 ,length(file_name))
from sys.dba_data_files;
select ' cp -p '||name
        ||'  ${ORA_BACKUP}/'||
        substr(name,instr(name,'/')+1,length(name))
from v\$controlfile;
select ' cp -p '||member
        ||'  ${ORA_BACKUP}/'||
        substr(member,instr(member,'/')+1,length(member))
from v\$logfile;
spool off
exit
EOF
#
# GENERATE SOME DBVERIFY COMMANDS
# 
 sqlplus -s system/manager << EOF
set feed off pages 0 head off echo off line 250
spool /tmp/dbv1.ksh
select ' dbv file=${ORA_BACKUP}/'||
       substr(file_name,instr(file_name,${ORA_BACKUP})+1 ,length(file_name)) 
       from sys.dba_data_files;
spool off
exit
EOF
#
# SHUT THE DATABASE DOWN
# 
 ./shutdown_db.ksh "$DATABASE" "NORMAL"
#
# DO THE COPY AND DBVERIFY IF IT EXISTS
# 
 chmod +x /tmp/copy1.ksh
 chmod +x /tmp/dbv1.ksh
 /tmp/copy1.ksh
#
 if [ -x "${ORACLE_HOME}/bin/dbv" ]  
 then
    /tmp/dbv1.ksh
 fi
#
# STARTUP THE DATABASE 
# 
$SVR << EOF
connect internal
startup
exit
EOF
exit 0

Return to Index of SQL Scripts


Home | Company Profile | Services | Contact Us | SQL scripts and tips | Quiz
Legal

Logo