Friday, October 24, 2008

Oracle DBA scripts



DBA
ORACLE DATABASE ADMINISTRATOR
Oracle backup and recovery scripts, part I: Cold backup under Unix
Rajendra Gutta
06.18.2003
Rating: -4.43- (out of 5)


This is the first of a series of excerpts from "Oracle DBA automation scripts" by Rajendra Gutta. Click for part two on hot backup under Unix and part three on backup and recovery under Windows NT. You can purchase the book here.

The backup and recovery scripts discussed here have been tested under Sun Solaris 2.x, HP-UX 11.x and AIX 4.x. The use of a particular command is discussed if there is a difference between these operating systems. They might also work in higher versions of the same operating system. These scripts are written based on the common ground among these three Unix flavors. However, I advise that you test the scripts under your environment for both backup and recovery before using it as a regular backup script. This testing not only gives you confidence in the script, it also gives you an understanding of how to use the script in case a recovery is needed and gives you peace of mind when a crisis hits.

Backup Scripts for HP-UX, Sun Solaris, and AIX

The backup scripts provided here work for HP-UX, Sun Solaris, and AIX with one slight modification. That is, the scripts use v$parameter and v$controlfile to get the user dump destination and control file information. Because in Unix the dollar sign ($) is a special character, you have to precede it with a forward slash (\) that tells Unix to treat it as a regular character. However, this is different in each flavor of Unix. AIX and HP-UX need one forward slash, and the Sun OS needs two forward slashes to make the dollar sign a regular character.

Sun OS 5.x needs two \\

AIX 4.x needs one \

HP-UX 11.x needs one \

These scripts are presented in modular approach. Each script consists of a number of small functions and a main section. Each function is designed to meet a specific objective so that they are easy to understand and modify. These small functions are reusable and can be used in the design of your own scripts. If you want to change a script to fit to your unique needs, you can do so easily in the function where you want the change without affecting the whole script.

After the backup is complete, it is necessary to check the backup status by reviewing log and error files generated by the scripts.
Cold Backup

Cold backup program (see Listing 3.1) performs the cold backup of the database under the Unix environment. The script takes two input parameters—SID and OWNER. SID is the instance to be backed up, and OWNER is the Unix account under which Oracle is running. Figure 3.3 describes the functionality of the cold backup program. Each box represents a corresponding function in the program:


Listing 3.1 coldbackup_ux
#####################################################################
# PROGRAM NAME:coldbackup_ux

# PURPOSE:Performs cold backup of the database. Database

#should be online when you start
# the script. It will shutdown and take a cold backup and brings
# the database up again


# USAGE:$coldbackup_ux SID OWNER

# INPUT PARAMETERS: SID(Instance name), OWNER(Owner of instance)

#####################################################################

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_verify(): Verify that database is online
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
funct_verify(){
STATUS=´ps -fu ${ORA_OWNER} |grep -v grep| grep ora_pmon_${ORA_SID}´
funct_chk_ux_cmd_stat "Database is down for given SID($ORA_SID),
Owner($ORA_OWNER). Can't generate files to be backed up"
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_verify_shutdown(): Verify that database is down
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
funct_verify_shutdown(){
STATUS=´ps -fu ${ORA_OWNER} |grep -v grep| grep ora_pmon_${ORA_SID}´
if [ $? = 0 ]; then
echo "´date´" >> $LOGFILE
echo "COLDBACKUP_FAIL: ${ORA_SID}, Database is up, can't make
coldbackup if the database is online."|tee -a ${BACKUPLOGFILE} >> $LOGFILE
exit 1
fi
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_shutdown_i(): Shutdown database in Immediate mode
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
funct_shutdown_i(){
${ORACLE_HOME}/bin/sqlplus -s << EOF
/ as sysdba
shutdown immediate;
exit
EOF
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_shutdown_n(): Shutdown database in Normal mode
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_shutdown_n(){
${ORACLE_HOME}/bin/sqlplus -s << EOF
/ as sysdba
shutdown normal;
exit
EOF
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_startup_r(): Startup database in restricted mode
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_startup_r(){
${ORACLE_HOME}/bin/sqlplus -s << EOF
/ as sysdba
startup restrict;
exit
EOF
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_startup_n(): Startup database in normal mode
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
funct_startup_n(){
${ORACLE_HOME}/bin/sqlplus -s << EOF
/ as sysdba
startup;
exit
EOF
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_build_dynfiles(): Identify the files to backup
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_build_dynfiles(){
# Build datafile list
echo "Building datafile list ." >> ${BACKUPLOGFILE}
datafile_list=´${ORACLE_HOME}/bin/sqlplus -s </ as sysdba
set heading off feedback off
select file_name from dba_data_files order by tablespace_name;
exit
EOF´

echo "############### SQL for Temp Files " >> ${RESTOREFILE}
${ORACLE_HOME}/bin/sqlplus -s <> ${RESTOREFILE}
/ as sysdba
set heading off feedback off
select 'alter tablespace '||tablespace_name||' add tempfile '||''||
file_name||''||' reuse'||';'
from dba_temp_files;
exit
EOF

echo "Backingup controlfile and trace to trace file" >>${BACKUPLOGFILE}
${ORACLE_HOME}/bin/sqlplus -s </ as sysdba
set heading off feedback off
alter database backup controlfile to '${CONTROLFILE_DIR}/backup_control.ctl';
alter database backup controlfile to trace;
exit
EOF


# Backup trace of control file
CONTROL=´ls -t ${udump_dest}/*.trc |head -1´
if [ ! -z "$CONTROL" ]; then
grep 'CONTROL' ${CONTROL} 1> /dev/null
if test $? -eq 0; then
cp ${CONTROL} ${CONTROLFILE_DIR}/backup_control.sql
fi
fi
}
# Prepare restore file for control file
echo "###### Control File " >> ${RESTOREFILE}
echo "# Use your own discretion to copy control file, not advised unless
required..." >> ${RESTOREFILE}
echo " End of backup of control file" >> ${BACKUPLOGFILE}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_cold_backup(): Perform cold backup
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_cold_backup(){

#Copy datafiles to backup location
echo "############### Data Files " >> ${RESTOREFILE}
for datafile in ´echo $datafile_list´
do
echo "Copying datafile ${datafile} ..." >> ${BACKUPLOGFILE}
#Prepare a restore file to restore coldbackup in case a
restore is necessary
echo cp -p ${DATAFILE_DIR}/´echo $datafile|awk -F"/" '{print $NF}'´
$datafile >> ${RESTOREFILE}
cp -p ${datafile} ${DATAFILE_DIR}
funct_chk_ux_cmd_stat "Failed to copy datafile file to
backup location"
done

#Copy current init.ora file to backup directory
echo " Copying current init.ora file" >> ${BACKUPLOGFILE}
cp -p ${init_file} ${INITFILE_DIR}/init${ORA_SID}.ora
funct_chk_ux_cmd_stat "Failed to copy init.ora file to backup location"

echo "################ Init.ora File " >> ${RESTOREFILE}
echo cp -p ${INITFILE_DIR}/init${ORA_SID}.ora ${init_file}
>> ${RESTOREFILE}
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_parm(): Check for input parameters
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_parm() {
if [ ${NARG} -ne 2 ]; then
echo "COLDBACKUP_FAIL: ${ORA_SID}, Not enough arguments passed"
exit 1
fi
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_bkup_dir(): Create backup directories if not already existing
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_bkup_dir() {

RESTOREFILE_DIR="${BACKUPDIR}/restorefile_dir"
BACKUPLOG_DIR="${BACKUPDIR}/backuplog_dir"
DATAFILE_DIR="${BACKUPDIR}/datafile_dir"
CONTROLFILE_DIR="${BACKUPDIR}/controlfile_dir"
REDOLOG_DIR="${BACKUPDIR}/redolog_dir"
ARCLOG_DIR="${BACKUPDIR}/arclog_dir"
INITFILE_DIR="${BACKUPDIR}/initfile_dir"

BACKUPLOGFILE="${BACKUPLOG_DIR}/backup_log_${ORA_SID}"
RESTOREFILE="${RESTOREFILE_DIR}/restorefile_${ORA_SID}"
LOGFILE="${LOGDIR}/${ORA_SID}.log"

if [ ! -d ${RESTOREFILE_DIR} ]; then mkdir -p ${RESTOREFILE_DIR}; fi
if [ ! -d ${BACKUPLOG_DIR} ]; then mkdir -p ${BACKUPLOG_DIR}; fi
if [ ! -d ${DATAFILE_DIR} ]; then mkdir -p ${DATAFILE_DIR}; fi
if [ ! -d ${CONTROLFILE_DIR} ]; then mkdir -p ${CONTROLFILE_DIR}; fi
if [ ! -d ${REDOLOG_DIR} ]; then mkdir -p ${REDOLOG_DIR}; fi
if [ ! -d ${ARCLOG_DIR} ]; then mkdir -p ${ARCLOG_DIR}; fi
if [ ! -d ${INITFILE_DIR} ]; then mkdir -p ${INITFILE_DIR}; fi

if [ ! -d ${DYN_DIR} ]; then mkdir -p ${DYN_DIR}; fi
if [ ! -d ${LOGDIR} ]; then mkdir -p ${LOGDIR}; fi

# Remove old backup
rm -f ${RESTOREFILE_DIR}/*
rm -f ${BACKUPLOG_DIR}/*
rm -f ${DATAFILE_DIR}/*
rm -f ${CONTROLFILE_DIR}/*
rm -f ${REDOLOG_DIR}/*
rm -f ${ARCLOG_DIR}/*
rm -f ${INITFILE_DIR}/*

echo "${JOBNAME}: coldbackup of ${ORA_SID} begun on ´date +\"%c\"´" >
${BACKUPLOGFILE}
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_get_vars(): Get environment variables
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_get_vars(){

ORA_HOME=´sed /#/d ${ORATABDIR}|grep -i ${ORA_SID}|nawk -F ":"
'{print $2}'´
ORA_BASE=´echo ${ORA_HOME}|nawk -F "/" '{for (i=2; i<=NF-2; i++) print
"/"$i}'´
ORACLE_BASE=´echo $ORA_BASE|tr -d " "´
init_file=$ORA_HOME/dbs/init$ORA_SID.ora
#log_arch_dest1=´sed /#/d $init_file|grep -i log_archive_dest|
nawk -F "=" '{print $2}'´
#log_arch_dest=´echo $log_arch_dest1|tr -d "'"|tr -d '"'´

udump_dest=´${ORACLE_HOME}/bin/sqlplus -s </ as sysdba
set heading off feedback off
select value from v\\$parameter
where name='user_dump_dest';
exit
EOF´

if [ x$ORA_HOME = 'x' ]; then
echo "COLDBACKUP_FAIL: Can't get ORACLE_HOME from oratab file
for $ORA_SID"|tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
exit 1
fi

if [ ! -f $init_file ]; then
echo "COLDBACKUP_FAIL: init$ORA_SID.ora does not exist in
ORACLE_HOME/dbs"|tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
exit 1
fi

if [ x$udump_dest = 'x' ]; then
echo "COLDBACKUP_FAIL: user_dump_dest not defined in init$ORA_SID.ora"|
tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
exit 1
fi

ORACLE_HOME=${ORA_HOME}; export ORACLE_HOME
ORACLE_SID=${ORA_SID}; export ORACLE_SID
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_ux_cmd_stat(): Check the exit status of Unix command
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_ux_cmd_stat() {
if [ $? != 0 ]; then
echo "´date´" |tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
echo "COLDBACKUP_FAIL: ${1} "| tee -a ${BACKUPLOGFILE}
>> ${LOGFILE}
exit 1
fi
}


############################################################
# MAIN
############################################################

NARG=$#
ORA_SID=$1
ORA_OWNER=$2

# Set environment variables
BACKUPDIR="/u02/${ORA_SID}/cold"
ORATABDIR=/etc/oratab
TOOLS="/u01/oracomn/admin/my_dba"

DYN_DIR="${TOOLS}/DYN_FILES"
LOGDIR="${TOOLS}/localog"
JOBNAME="dbcoldbackup"

echo " Starting coldbackup of ${ORA_SID} "

funct_chk_parm
funct_chk_bkup_dir
funct_get_vars
funct_verify
funct_build_dynfiles
funct_shutdown_i
funct_startup_r
funct_shutdown_n
funct_verify_shutdown
funct_cold_backup
funct_startup_n

echo "${ORA_SID}, Coldbackup Completed successfully on ´date +\"%c\"´"
|tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
######## END MAIN ##########################
Cold Backup Script under Unix Checklist

In the main function, set correct values for the BACKUPDIR, ORATABDIR, and TOOLS variables highlighted in the cold backup script. The default location of ORATABDIR is different for each flavor of Unix. For information about the default location of the ORATAB file for different flavors of Unix, refer to Chapter 13, "Unix, Windows NT, and Oracle."

Check for the existence of SID in oratab file. If not already there, you must add the instance.

Check for existence of initSID.ora file in the ORACLE_HOME/dbs directory. If it is in a different location, you can create a soft link to the ORACLE_HOME/dbs directory.

Pass SID and OWNER as parameters to the program.

The database must be running when you start the program. It gets required information by querying the database and then shuts down the database and performs cold backup.

main() The main function defines the variables required and calls the functions to be executed. The variables BACKUPDIR defines the backup location, ORATABDIR defines the oratab file location. oratab files maintain the list of instances and their home directories on the machine. This file is created by default when oracle is installed. If it is not there, you must create one. OWNER is the owner of Oracle software directories. A sample oratab file can be found at the end of the chapter.

funct_get_vars() This function gets ORACLE_HOME from the oratab file and USER_DUMP_DEST from the initSID.ora file. The value of USER_DUMP_DEST is used to back up the trace of the control file.

funct_build_dynfiles() This function generates a list of files from the database for backup. It also creates SQL statements for temporary files. These temporary files do not need to be backed up, but can be recreated when a restore is performed. These temporary files are session-specific and do not have any content when the database is closed.

funct_shutdown_i() This function shuts down the database in Immediate mode, so that any user connected to the database will be disconnected immediately.

funct_startup_r() This function starts up the database in Restricted mode, so that no one can connect to the database except users with Restrict privileges.

funct_shutdown_n() This function performs a clean shutdown of the database.

funct_chk_ux_cmd_stat() This function is used to check the status of Unix commands, especially after copying files to a backup location.
Restore File

A cold backup program creates a restore file that contains the commands to restore the database. This functionality is added based on the fact that a lot of DBAs perform backups but, when it comes to recovery, they will not have any procedures to make the recovery faster. With the restore file, it is easier to restore files to the original location because it has all the commands ready to restore the backup. Otherwise, you need to know the structure of the database—what files are located where. A sample restore file is shown in Listing 3.2.
Listing 3.2 Sample Restore File
######### SQL for Temp Files
alter tablespace TEMP add tempfile '/u03/oracle/DEV/data/temp03.dbf' reuse;
alter tablespace TEMP add tempfile '/u03/oracle/DEV/data/temp04.dbf' reuse;
######### Data Files
cp -p /bkp/DEV/cold/datafile_dir/INDX01.dbf /u02/oracle/DEV/data/INDX01.dbf
cp -p /bkp/DEV/cold/datafile_dir/RBS01.dbf /u02/oracle/DEV/data/RBS01.dbf
cp -p /bkp/DEV/cold/datafile_dir/SYSTEM01.dbf /u02/oracle/DEV/data/SYSTEM01.dbf
cp -p /bkp/DEV/cold/datafile_dir/TEMP01.dbf /u02/oracle/DEV/data/TEMP01.dbf
cp -p /bkp/DEV/cold/datafile_dir/USERS01.dbf /u02/oracle/DEV/data/USERS01.dbf
######### Control Files
cp -p /bkp/DEV/cold/controlfile_dir/cntrl01.dbf
/u02/oracle/DEV/data/cntrl01.dbf
######### Init.ora File
cp -p /bkp/DEV/cold/initfile_dir/initDEV.ora /u02/apps/DEV/oracle/8.1.7/
dbs/initDEV.ora
Cold Backup Troubleshooting and Status Check

The important thing here is that the backup log file defined by BACKUPLOGFILE contains detailed information about each step of the backup process. This is a very good place to start investigating why the backup failed or for related errors. This file will also have the start and end time of the backup.

A single line about the success or failure of a backup is appended to SID.log file every time a backup is performed. This file is located under the directory defined by the LOGDIR variable. This file also has the backup completion time. A separate file is created for each instance. This single file maintains the history of performed backups and their status and timing information. The messages for a cold backup are 'COLDBACKUP_FAIL' if a cold backup failed and 'Coldbackup Completed successfully' if a backup completes successfully.

Apart from the BACKUPLOGFILE and SID.log files, it is always good to capture the out-of-the-ordinary errors displayed onscreen if you are running the backup unattended. You can capture these errors by running the command shown next. The same thing can be done for hot backups. This command captures onscreen errors to the coldbackup.log file.
coldbackup_ux SID OWNER 1> coldbackup.log 2>&1

The following is an excerpt from the SID.log file:
Tue Jul 18 16:48:46 EDT 2000
COLDBACKUP_FAIL: DEV, Failed to copy control file to backup location
BACKUPLOGFILE
Listing 3.3 Sample BACKUPLOGFILE
—dbcoldbackup: coldbackup of DEV begun on Sun May 20 21:15:27 2001
dbcoldbackup: building datafile list .
dbcoldbackup: Building controlfile list

Copying datafile /u02/oracle/DEV/data/INDX01.dbf ...
Copying datafile /u02/oracle/DEV/data/RBS01.dbf ...
Copying datafile /u02/oracle/DEV/data/SYSTEM01.dbf ...
Copying datafile /u02/oracle/DEV/data/TEMP01.dbf ...
Copying datafile /u02/oracle/DEV/data/USERS01.dbf ...
Copying control file /u02/oracle/DEV/data/cntrl01.dbf ...
Copying redolog file /u03/oracle/DEV/data/log01a.dbf ...
Copying redolog file /u03/oracle/DEV/data/log01b.dbf ...
Copying current init.ora file
DEV, Coldbackup Completed successfully on Sun May 20 21:19:38 2001

Reader Feedback

Todd B. writes: I was looking at this tip and one thing that came to mind is it neglects those who use a network backup system. I have created some scripts that test for the presence of the backup daemon. Once the backup daemon is not there, then I restart the database. This enables me to only have as much downtime as necessary to get a complete cold backup. I also have a second script that will check for the presence of the first. This is my "drop dead" script, meaning that at a certain time, regardless if the backups have finished or not, I need to have the database up. This script will kill the first script waiting on the backup daemon, start the database, then e-mail the administrators letting them know that we did not get a good backup.

ORACLE DATABASE ADMINISTRATOR
Oracle backup and recovery scripts, part II: Hot backup under Unix
Rajendra Gutta
06.25.2003
Rating: -4.58- (out of 5)


This is the second of a series of excerpts from "Oracle DBA automation scripts" by Rajendra Gutta. Click for part one on cold backup under Unix and part three on backup and recovery under Windows NT. You can purchase the book here.
Hot Backup

Listing 3.4 provides the script to perform the hot backup of a database under the Unix environment. The hot backup script takes two input parameters—SID and OWNER. SID is the instance to be backed up, and OWNER is the Unix account under which Oracle is running.

Figure 3.4 shows the functionality of the hot backup program. Each box represents a corresponding function in the program:


Listing 3.4 hotbackup_ux
#####################################################################
# PROGRAM NAME: hotbackup_ux

# PURPOSE: This utility will perform a warm backup of
# the database
# USAGE: $hotbackup_ux SID OWNER

# INPUT PARAMETERS: SID(Instance name), OWNER(Owner of instance)
#####################################################################


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_verify(): Verify that database is online
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_verify(){
STATUS=´ps -fu ${ORA_OWNER} |grep -v grep| grep ora_pmon_${ORA_SID}´
funct_chk_ux_cmd_stat "Database is down for given SID($ORA_SID),
Owner($ORA_OWNER). Can't perform hotbackup "
}

#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_dblogmode(): Check DB log mode
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
funct_chk_dblogmode(){
STATUS=´${ORACLE_HOME}/bin/sqlplus -s </ as sysdba
set heading off feedback off
select log_mode from v\\$database;
exit
EOF´

if [ $STATUS = "NOARCHIVELOG" ]; then
echo "´date´" |tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
echo "HOTBACKUP_FAIL: $ORA_SID is in NOARCHIVELOG mode. Can't perform
hotbackup " |tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
exit 1
fi
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_control_backup(): Backup control file
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_control_backup(){
echo "Begin backup of controlfile and trace to trace file" >>${BACKUPLOGFILE}
${ORACLE_HOME}/bin/sqlplus -s </ as sysdba
set heading off feedback off
alter database backup controlfile to '${CONTROLFILE_DIR}/backup_control.ctl';
alter database backup controlfile to trace;
exit
EOF

# Backup trace of control file
CONTROL=´ls -t ${udump_dest}/*.trc |head -1´
if [ ! -z "$CONTROL" ]; then
grep 'CONTROL' ${CONTROL} 1> /dev/null
if test $? -eq 0; then
cp ${CONTROL} ${CONTROLFILE_DIR}/backup_control.sql
fi
fi

# Prepare restore file for control file
echo "###### Control File " >> ${RESTOREFILE}
echo "# Use your own discretion to copy control file, not advised unless
required..." >> ${RESTOREFILE}
echo " End of backup of control file" >> ${BACKUPLOGFILE}
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_archivelog_backup(): Backup archivelog files
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_archivelog_backup(){
echo "Begin backup of archived redo logs" >> ${BACKUPLOGFILE}
#Switch logs to flush current redo log to archived redo before back up
${ORACLE_HOME}/bin/sqlplus -s </ as sysdba
set heading off feedback off
alter system switch logfile;
alter system archive log stop;
exit
EOF

# This gets the redo sequence number that is being archived
# and remove this from the list of files to be backed up
ARCSEQ=´${ORACLE_HOME}/bin/sqlplus -s </ as sysdba
set heading off feedback off
select min(sequence#) from v\\$log
where archived='NO';
exit
EOF´
#Get current list of archived redo log files
ARCLOG_FILES=´ls ${log_arch_dest}/*|grep -v $ARCSEQ´

${ORACLE_HOME}/bin/sqlplus -s </ as sysdba
set heading off feedback off
alter system archive log start;
exit
EOF

#Prepare restore file for arc log files
echo "##### Archive Log Files" >> ${RESTOREFILE}
for arc_file in ´echo $ARCLOG_FILES´
do
echo cp -p ${ARCLOG_DIR}/´echo $arc_file|awk -F"/" '{print $NF}'´
$arc_file >> ${RESTOREFILE}
done

#Copy arc log files to backup location
#remove the archived redo logs from the log_archive_dest if copy is successful
cp -p ${ARCLOG_FILES} ${ARCLOG_DIR}
if [ $? = 0 ]; then
rm ${ARCLOG_FILES}
else
echo "´date´" |tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
echo "HOTBACKUP_FAIL: Failed to copy Archive log files" |
tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
exit 1
fi
echo "End backup of archived redo logs" >> ${BACKUPLOGFILE}
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_init_backup(): Backup init.ora file
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_init_backup(){

#Copy current init.ora file to backup directory
echo " Copying current init${ORA_SID}.ora file" >> ${BACKUPLOGFILE}
cp -p ${init_file} ${INITFILE_DIR}/init${ORA_SID}.ora
funct_chk_ux_cmd_stat "Failed to copy init.ora file to backup location"

# Prepare restore file for init.ora
echo "############# Parameter Files" >> ${RESTOREFILE}
echo cp -p ${INITFILE_DIR}/init${ORA_SID}.ora ${init_file} >> ${RESTOREFILE}
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_temp_backup(): Prepre SQL for temp files
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_temp_backup(){
echo "############# Recreate the following Temporary Files" >> ${RESTOREFILE}
${ORACLE_HOME}/bin/sqlplus -s <> ${RESTOREFILE}
/ as sysdba
set heading off feedback off
select 'alter tablespace '||tablespace_name||' add tempfile '||''||
file_name||''||' reuse'||';'
from dba_temp_files;
exit
EOF
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#funct_hot_backup(): Backup datafiles
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_hot_backup(){

# Get the list of tablespaces
echo "Building tablespace list " >> ${BACKUPLOGFILE}
tablespace_list=´${ORACLE_HOME}/bin/sqlplus -s </ as sysdba
set heading off feedback off
select distinct tablespace_name from dba_data_files
order by tablespace_name;
exit
EOF´

echo "##### DATE:" ´date´ > ${RESTOREFILE}
echo "####Data Files(Please restore only corrupted files)" >> ${RESTOREFILE}
for tblspace in ´echo $tablespace_list´
do
# Get the datafiles for the current tablespace
datafile_list=´${ORACLE_HOME}/bin/sqlplus -s </ as sysdba
set heading off feedback off
select file_name from dba_data_files
where tablespace_name = '${tblspace}';
exit
EOF´

echo " Beginning back up of tablespace ${tblspace}..." >> ${BACKUPLOGFILE}
${ORACLE_HOME}/bin/sqlplus -s </ as sysdba
set heading off feedback off
alter tablespace ${tblspace} begin backup;
exit
EOF

# Copy datafiles of current tablespace
for datafile in ´echo $datafile_list´
do
echo "Copying datafile ${datafile}..." >> ${BACKUPLOGFILE}
# The next command prepares restore file
echo cp -p ${DATAFILE_DIR}/´echo $datafile|awk -F"/" '{print $NF}'´
$datafile >> ${RESTOREFILE}
cp -p ${datafile} ${DATAFILE_DIR}
if [ $? != 0 ]; then
echo "´date´" |tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
echo "HOTBACKUP_FAIL: Failed to copy file to backup location "|
tee -a ${BACKUPLOGFILE} >> ${LOGFILE}

# Ending the tablespace backup before exiting
´${ORACLE_HOME}/bin/sqlplus -s < / as sysdba
set heading off feedback off
alter tablespace ${tblspace} end backup;
exit
EOF´

exit 1
fi
done

${ORACLE_HOME}/bin/sqlplus -s </ as sysdba
set heading off feedback off
alter tablespace ${tblspace} end backup;
exit
EOF
echo " Ending back up of tablespace ${tblspace}.." >> ${BACKUPLOGFILE}
done
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_parm(): Check for input parameters
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_parm() {
if [ ${NARG} -ne 2 ]; then
echo "HOTBACKUP_FAIL: ${ORA_SID}, Not enough arguments passed"
exit 1
fi
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_bkup_dir(): Create backup directories if not already existing
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_bkup_dir() {

RESTOREFILE_DIR="${BACKUPDIR}/restorefile_dir"
BACKUPLOG_DIR="${BACKUPDIR}/backuplog_dir"
DATAFILE_DIR="${BACKUPDIR}/datafile_dir"
CONTROLFILE_DIR="${BACKUPDIR}/controlfile_dir"
REDOLOG_DIR="${BACKUPDIR}/redolog_dir"
ARCLOG_DIR="${BACKUPDIR}/arclog_dir"
INITFILE_DIR="${BACKUPDIR}/initfile_dir"

BACKUPLOGFILE="${BACKUPLOG_DIR}/backup_log_${ORA_SID}"
RESTOREFILE="${RESTOREFILE_DIR}/restorefile_${ORA_SID}"
LOGFILE="${LOGDIR}/${ORA_SID}.log"

if [ ! -d ${RESTOREFILE_DIR} ]; then mkdir -p ${RESTOREFILE_DIR}; fi
if [ ! -d ${BACKUPLOG_DIR} ]; then mkdir -p ${BACKUPLOG_DIR}; fi
if [ ! -d ${DATAFILE_DIR} ]; then mkdir -p ${DATAFILE_DIR}; fi
if [ ! -d ${CONTROLFILE_DIR} ]; then mkdir -p ${CONTROLFILE_DIR}; fi
if [ ! -d ${REDOLOG_DIR} ]; then mkdir -p ${REDOLOG_DIR}; fi
if [ ! -d ${ARCLOG_DIR} ]; then mkdir -p ${ARCLOG_DIR}; fi
if [ ! -d ${INITFILE_DIR} ]; then mkdir -p ${INITFILE_DIR}; fi

if [ ! -d ${DYN_DIR} ]; then mkdir -p ${DYN_DIR}; fi
if [ ! -d ${LOGDIR} ]; then mkdir -p ${LOGDIR}; fi

# Remove old backup
rm -f ${RESTOREFILE_DIR}/*
rm -f ${BACKUPLOG_DIR}/*
rm -f ${DATAFILE_DIR}/*
rm -f ${CONTROLFILE_DIR}/*
rm -f ${REDOLOG_DIR}/*
rm -f ${ARCLOG_DIR}/*
rm -f ${INITFILE_DIR}/*

echo "${JOBNAME}: hotbackup of ${ORA_SID} begun on ´date +\"%c\"´" >
${BACKUPLOGFILE}
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_get_vars(): Get environment variables
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_get_vars(){

ORA_HOME=´sed /#/d ${ORATABDIR}|grep -i ${ORA_SID}|nawk -F ":" '{print $2}'´
ORA_BASE=´echo ${ORA_HOME}|nawk -F "/" '{for (i=2; i<=NF-2; i++)
print "/"$i}'´
ORACLE_BASE=´echo $ORA_BASE|tr -d " "´
init_file=$ORA_HOME/dbs/init$ORA_SID.ora
#log_arch_dest1=´sed /#/d $init_file|grep -i log_archive_dest|
nawk -F "=" '{print $2}'´
#log_arch_dest=´echo $log_arch_dest1|tr -d "'"|tr -d '"'´

udump_dest=´${ORACLE_HOME}/bin/sqlplus -s </ as sysdba
set heading off feedback off
select value from v\\$parameter
where name='user_dump_dest';
exit
EOF´


if [ x$ORA_HOME = 'x' ]; then
echo "HOTBACKUP_FAIL: can't get ORACLE_HOME from oratab file for $ORA_SID"
| tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
exit 1
fi

if [ ! -f $init_file ]; then
echo "HOTBACKUP_FAIL: init$ORA_SID.ora does not exist in ORACLE_HOME/dbs"
| tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
exit 1
fi

if [ x$log_arch_dest = 'x' -o x$udump_dest = 'x' ]; then
echo "HOTBACKUP_FAIL: user_dump_dest or log_archive_dest not defined "
| tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
exit 1
fi

ORACLE_HOME=${ORA_HOME}; export ORACLE_HOME
ORACLE_SID=${ORA_SID}; export ORACLE_SID
}

#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_ux_cmd_stat(): Check the exit status of Unix command
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
funct_chk_ux_cmd_stat() {
if [ $? != 0 ]; then
echo "´date´" |tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
echo "HOTBACKUP_FAIL:${1} "|tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
exit 1
fi
}

############################################################
# MAIN
############################################################

NARG=$#
ORA_SID=$1
ORA_OWNER=$2

# Set environment variables
BACKUPDIR="/u02/${ORA_SID}/hot"
ORATABDIR=/etc/oratab
TOOLS="/u01/oracomn/admin/my_dba"
log_arch_dest="/export/home/orcl/arch"

DYN_DIR="${TOOLS}/DYN_FILES"
LOGDIR="${TOOLS}/localog"
JOBNAME="dbhotbackup"

echo " Starting hotbackup of .... ${ORA_SID}"

funct_chk_parm
funct_chk_bkup_dir
funct_get_vars
funct_verify
funct_chk_dblogmode
funct_hot_backup
funct_temp_backup
funct_control_backup
funct_archivelog_backup
funct_init_backup

echo "${ORA_SID}, hotbackup Completed successfully on ´date +\"%c\"´" |
tee -a ${BACKUPLOGFILE} >> ${LOGFILE}

######## END MAIN #########################
Hot Backup Script under Unix Checklist

In the main function, set the correct values for BACKUPDIR, ORATABDIR, TOOLS, and log_arch_dest variables highlighted in the script. The default location of ORATABDIR is different for each flavor of Unix.

Check for existence of the SID instance in the oratab file. If not already there, you must add the instance.

Check for the existence of the initSID.ora file in the ORACLE_HOME/dbs directory. If it is in a different location, you must create a soft link to the ORACLE_HOME/dbs directory.

Pass SID and OWNER as parameters to the program:

main() BACKUPDIR defines the backup location. ORATABDIR defines the oratab file location. oratab files maintain the list of instances and their home directories on the machine. This file is created by default when Oracle is installed. If it is not there, you must create one. OWNER is the owner of the Oracle software directories.

funct_get_vars() Make sure that the USER_DUMP_DEST parameter is set correctly in Init.ora file. I was reluctant to get LOG_ARCHIVE_DEST from the Init.ora file because there are some changes between Oracle 7 and 8 in the way the archive destination is defined. There are a variety of ways that you can define log_archive_dest based on how many destinations you are using. Consequently, I have given the option to define log_archive_dest in main function.

funct_temp_backup() Oracle 7 and Oracle 8 support permanent temporary tablespaces (created with create tablespce tablespace_name ... temporary). Apart from this, Oracle 8I has new features to create temporary tablespaces that do not need back up (created with create tablespace temporary...). Data in these temporary tablespaces is session-specific and gets deleted as soon as the session is disconnected. Because of the nature of these temporary tablespaces, you do not need to back them up; in the case of a restore, you can just add the data file for these temporary tablespaces. The files for these temporary tablespaces are listed under the dba_temp_files data dictionary view.

funct_control_backup() In addition to taking backup of control file, this function also backs up the trace of the control file. The trace of the control file will be useful to examine the structure of the database. This is the single most important piece of information that you need to perform a good recovery, especially if the database has hundreds of files.

funct_chk_bkup_dir() This function creates backup directories for data, control, redo log, archivelog, init files, restore files, and backup log files.
Restore file

The restore file for hot backup looks similar to cold backup. Please refer to the explanation under the heading restore file for cold backup.
Hot Backup Troubleshooting and Status Check

The important thing here is that the backup log file defined by (BACKUPLOGFILE) contains detailed information about each step of the backup process. This is a very good place to start investigating why a backup has failed or for related errors. This file will also have the start and end time of the backup.

A single line about the success or failure of a backup is appended to the SID.log file every time a backup is performed. This file is located under the directory defined by the LOGDIR variable. This file also has the backup completion time. A separate file is created for each instance. This single file maintains the history of the performed backups, their status, and timing information. The messages for a hot backup are 'HOTBACKUP_FAIL', if the hot backup failed, and 'Hotbackup Completed successfully', if the backup completes successfully.

The following is an excerpt from the log file:
Tue Jul 18 16:48:46 EDT 2000
HOTBACKUP_FAIL: DEV, Not enough arguments passed
Export

The export program (see Listing 3.5) performs a full export of the database under Unix environment. The export script takes two input parameters—SID and OWNER. SID is the instance to be backed up, and OWNER is the Unix account under which Oracle is running. Figure 3.5 shows the functionality of the export and split export programs. Each box represents a corresponding function in the program.

Figure 3.5 Functions in export and split export scripts for Unix.
Listing 3.5 xport_ux
######################################################################
# PROGRAM NAME: xport_ux
# PURPOSE: Performs export of the database
# USAGE: $xport_ux SID OWNER
# INPUT PARAMETERS: SID(Instance name), OWNER(Owner of instance)

)
######################################################################


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_verify(): Verify that database is online
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_verify(){
STATUS=´ps -fu ${ORA_OWNER} |grep -v grep| grep ora_pmon_${ORA_SID}´
funct_chk_unix_command_status "Database is down for given SID($ORA_SID),
Owner($ORA_OWNER). Can't perform export "
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_cleanup(): Cleanup interim files
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_cleanup() {
echo "Left for user convenience" > /dev/null
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_build_parfile(): This will create parameter file
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_build_parfile() {
# This line makes sure that it always creates a new parameter file
echo " " >${PARFILE}
echo "userid=system/manager">>${PARFILE}
# if you use connect string. see next line.
#userid=system/manager@${CON_STRING}
#echo "Owner=scott">>${PARFILE}
#echo "Tables=scott.T1">>${PARFILE}
echo "Full=Y">>${PARFILE}
#echo "Direct=Y">>${PARFILE}
echo "Grants=Y">>${PARFILE}
echo "Indexes=Y">>${PARFILE}
echo "Rows=Y">>${PARFILE}
echo "Constraints=Y">>${PARFILE}
echo "Compress=N">>${PARFILE}
echo "Consistent=Y">>${PARFILE}
echo "File=${FILE}">>${PARFILE}
echo "Log=${EXPORT_DIR}/${ORA_SID}.exp.log">>${PARFILE}
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_export(): Export the database
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_export() {
# Remove old export file
rm -f ${FILE}

${ORACLE_HOME}/bin/exp parfile=${PARFILE}
if [ $? != 0 ]; then
echo ´date´ >> $LOGDIR/${ORA_SID}.log
echo "EXPORT_FAIL: ${ORA_SID}, Export Failed" >> $LOGDIR/${ORA_SID}.log
funct_cleanup
exit 1
fi
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_parm(): Check for input parameters
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_parm() {
if [ ${NARG} -ne 2 ]; then
echo "EXPORT_FAIL: ${ORA_SID}, Not enough arguments passed"
exit 1
fi
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_bkup_dir(): Create backup directories if not already exist
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_bkup_dir() {
EXPORT_DIR=${BACKUPDIR}
if [ ! -d ${EXPORT_DIR} ]; then mkdir -p ${EXPORT_DIR}; fi
if [ ! -d ${DYN_DIR} ]; then mkdir -p ${DYN_DIR}; fi
if [ ! -d ${LOGDIR} ]; then mkdir -p ${LOGDIR}; fi

FILE="${EXPORT_DIR}/${ORA_SID}.dmp"
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_get_vars(): Get environment variables
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_get_vars(){
ORA_HOME=´sed /#/d ${ORATABDIR}|grep -i ${ORA_SID}|nawk -F ":" '{print $2}'´
ORA_BASE=´echo ${ORA_HOME}|nawk -F "/" '{for (i=2; i<=NF-2; i++)
print "/"$i}'´
ORACLE_BASE=´echo $ORA_BASE|tr -d " "´
ORACLE_HOME=${ORA_HOME}; export ORACLE_HOME
ORACLE_SID=${ORA_SID}; export ORACLE_SID
#CON_STRING=${ORA_SID}.company.com
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_unix_command_status(): Check exit status of Unix command
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_unix_command_status() {
if [ $? != 0 ]; then
echo "´date´" >> ${LOGDIR}/${ORA_SID}.log
echo "EXPORT_FAIL: ${1} " >> ${LOGDIR}/${ORA_SID}.log
exit 1
fi
}


######################################
# MAIN
######################################


NARG=$#
ORA_SID=$1
ORA_OWNER=$2

# Set up the environment
BACKUPDIR="/u02/${ORA_SID}/export"
ORATABDIR=/etc/oratab
TOOLS="/u01/oracomn/admin/my_dba"

DYN_DIR="${TOOLS}/DYN_FILES"
PARFILE="${DYN_DIR}/export.par"
LOGDIR="${TOOLS}/localog"

echo "... Now exporting .... ${ORA_SID}"

funct_chk_parm
funct_get_vars
funct_verify
funct_chk_bkup_dir
funct_build_parfile
funct_export
funct_cleanup

echo ´date´ >> $LOGDIR/${ORA_SID}.log
echo "${ORA_SID}, export completed successfully" >> $LOGDIR/${ORA_SID}.log

####################### END MAIN ###############################
Export Script under Unix Checklist

In the main function, set the correct values for BACKUPDIR, ORATABDIR, and TOOLS variables highlighted in the export script. The default location of ORATABDIR is different for each flavor of Unix.

Check for existence of SID in the oratab file. If not already there, you must add the instance.

The funct_build_parfile() function builds the parameter file. By default, it performs a full export. You can modify the parameters to perform a user- or table-level export.

Pass SID and OWNER as parameters to the program:

funct_build_parfile() Builds the export.par parameter file dynamically, based on the information provided in this function. This function is configured for a full export of the database. To perform a different type of export (user- or table-level), set the correct parameters.

funct_cleanup() Removes the interim files.
Export Troubleshooting and Status Check

The 'Log' parameter value set in the parameter file will have detailed information about the status of export. This is a very good place to start investigating why an export has failed or for related errors.

A single line about the success or failure of export is appended to SID.log file every time an export is performed. This file is located under the directory defined by the LOGDIR variable. This file also has the backup completion time. A separate file is created for each instance. This single file maintains the history of performed backups, their status, and timing information. The messages for an export are 'EXPORT_FAIL', if the export failed, and 'Export Completed successfully', if the export completes successfully.

The following is an excerpt from a log file:
Tue Apr 8 16:07:12 EST 2000
DEV , export completed successfully
Split Export

The split export program (see Listing 3.6) performs an export of the database. Additionally, if the export file is larger than 2GB, the script compresses the export file and splits into multiple files to overcome the export limitation of a 2GB file system size. This is the only way to split the export file prior to Oracle 8i. New features in 8I allow you to split the export file into multiple files, but it does not compress the files on-the-fly to save space. The script uses the Unix commands split and compress to perform splitting and compressing of the files. The functions of the script are explained in Figure 3.5.

The split export script takes two input parameters—SID and OWNER. SID is the instance to be backed up, and OWNER is the Unix account under which Oracle is running.

Export New Features in 8i

In 8i, Oracle introduced two new export parameters called FILESIZE and QUERY. FILESIZE specifies the maximum file size of each dump file. This overcomes the 2GB file system limitations of export command operating systems. By using the QUERY parameter, you can export the subset of a table data. During an import, when using split export files, you have to specify the same FILESIZE limit.
Listing 3.6 splitZxport_ux
######################################################################
# PROGRAM NAME: splitZxport_ux
#
# PURPOSE: Performs export of the database
# Compresses the export file on the fly while splitting.
# Useful if the size of export file goes beyond 2GB
# USAGE: $splitZxport_ux SID OWNER
# INPUT PARAMETERS: SID(Instance name), OWNER(Owner of instance)


######################################################################


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_verify(): Verify that database is online
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_verify(){
STATUS=´ps -fu ${ORA_OWNER} |grep -v grep| grep ora_pmon_${ORA_SID}´
funct_chk_unix_command_status "Database is down for given SID($ORA_SID),
Owner($ORA_OWNER). Can't perform export "
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_cleanup(): Cleanup interim files
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_cleanup() {
rm –f ${PIPE_DEVICE}
rm –f ${SPLIT_PIPE_DEVICE}
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_splitcompress_pipe(): Creates pipe for compressing and
splitting of file
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_splitcompress_pipe() {
# Creates pipe for compressing
if [ ! -r ${PIPE_DEVICE} ]; then
/etc/mknod ${PIPE_DEVICE} p
fi

#Creates pipe for splitting
if [ ! -r ${SPLIT_PIPE_DEVICE} ]; then
/etc/mknod ${SPLIT_PIPE_DEVICE} p
fi

# Splits the file for every 500MB
# As it splits it adds aa,bb,cc ... zz to the name
nohup split -b1000m - ${ZFILE} < ${SPLIT_PIPE_DEVICE} &
nohup compress < ${PIPE_DEVICE} >${SPLIT_PIPE_DEVICE} &
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_build_parfile(): Creates parameter file
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_build_parfile() {
# This line makes sure that it always creates a new parameter file
echo " " >${PARFILE}
echo "userid=system/manager">>${PARFILE}
echo "Full=Y">>${PARFILE}
#echo "tables=scott.t1">>${PARFILE}
echo "Grants=Y">>${PARFILE}
echo "Indexes=Y">>${PARFILE}
echo "Rows=Y">>${PARFILE}
echo "Constraints=Y">>${PARFILE}
echo "Compress=N">>${PARFILE}
echo "Consistent=Y">>${PARFILE}
echo "File=${PIPE_DEVICE}">>${PARFILE}
echo "Log=${EXPORT_DIR}/${ORA_SID}.exp.log">>${PARFILE}
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_export(): Export the database
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_export() {
# Remove old export file
rm -f ${ZFILE}

${ORACLE_HOME}/bin/exp parfile=${PARFILE}
if [ $? != 0 ]; then
echo ´date´ >> $LOGDIR/${ORA_SID}.log
echo "EXPORT_FAIL: ${ORA_SID}, Export Failed" >> $LOGDIR/${ORA_SID}.log
funct_cleanup
exit 1
fi
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_parm(): Check for input parameters
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_parm() {
if [ ${NARG} -ne 2 ]; then
echo "EXPORT_FAIL: ${ORA_SID}, Not enough arguments passed"
exit 1
fi
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_bkup_dir(): Create backup directories if not already existing
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_bkup_dir() {
EXPORT_DIR=${BACKUPDIR}
if [ ! -d ${EXPORT_DIR} ]; then mkdir -p ${EXPORT_DIR}; fi
if [ ! -d ${DYN_DIR} ]; then mkdir -p ${DYN_DIR}; fi
if [ ! -d ${LOGDIR} ]; then mkdir -p ${LOGDIR}; fi
ZFILE="${EXPORT_DIR}/${ORA_SID}.dmp.Z"
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_get_vars(): Get environment variables
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_get_vars(){
ORA_HOME=´sed /#/d ${ORATABDIR}|grep -i ${ORA_SID}|nawk -F ":" '{print $2}'´
ORA_BASE=´echo ${ORA_HOME}|nawk -F "/" '{for (i=2; i<=NF-2; i++)
print "/"$i}'´
ORACLE_BASE=´echo $ORA_BASE|tr -d " "´
ORACLE_HOME=${ORA_HOME}; export ORACLE_HOME
ORACLE_SID=${ORA_SID}; export ORACLE_SID
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_unix_command_status(): Check exit status of Unix command
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_unix_command_status() {
if [ $? != 0 ]; then
echo "´date´" >> ${LOGDIR}/${ORA_SID}.log
echo "EXPORT_FAIL: ${1} " >> ${LOGDIR}/${ORA_SID}.log
exit 1
fi
}

#######################################
## MAIN
#######################################

NARG=$#
ORA_SID=$1
ORA_OWNER=$2

# Set up environment
BACKUPDIR="/u02/${ORA_SID}/export"
ORATABDIR=/etc/oratab
TOOLS="/u01/oracomn/admin/my_dba"

DYN_DIR="${TOOLS}/DYN_FILES"
PARFILE="${DYN_DIR}/export.par"
LOGDIR="${TOOLS}/localog"

PIPE_DEVICE="/tmp/export_${ORA_SID}_pipe"
SPLIT_PIPE_DEVICE="/tmp/split_${ORA_SID}_pipe"

echo "... Now exporting .... ${ORA_SID}"

funct_chk_parm
funct_get_vars
funct_verify
funct_chk_bkup_dir
funct_splitcompress_pipe
funct_build_parfile
funct_export
funct_cleanup

echo ´date´ >> $LOGDIR/${ORA_SID}.log
echo "${ORA_SID}, export completed successfully" >> $LOGDIR/${ORA_SID}.log

####################### END MAIN ###############################
Split Export Script under Unix Checklist

The checklist of things to verify before the splitZxport is run is the same as for the export program.

funct_splitcompress_pipe() This function creates two pipes—one for compressing and another for splitting. The export dump file is passed to the compress pipe for compression, and the output of the compress command is passed to the split command for the split operation. The output of split command is passed to a file. The split command splits the dump file into pieces of 1000MB. When the split operation occurs, it appends aa, bb, cc...zz to the name of the original file to maintain different names for individual pieces. compress and split are Unix commands.

funct_build_parfile() In building the parameter file, we pass the pipe name as a filename to the export command. The pipe acts as a medium to transfer output from one command to another.
Split Import

The split import program (see Listing 3.7) performs an import using the compressed split export dump files created by the splitZxport program. The script takes two input parameters—SID and OWNER. SID is the instance to be backed up, and OWNER is the Unix account under which Oracle is running.
Listing 3.7 splitZmport_ux
######################################################################
# PROGRAM NAME: splitZmport_ux

# PURPOSE: Performs import of the database using export files created by
#splitZxport program. Uncompresses the dump file on the fly while desplitting.

# USAGE: $splitZmport_ux SID OWNER
# INPUT PARAMETERS: SID(Instance name), OWNER(Owner of instance)


######################################################################


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_verify(): Verify that database is online
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_verify(){
STATUS=´ps -fu ${ORA_OWNER} |grep -v grep| grep ora_pmon_${ORA_SID}´
funct_chk_unix_command_status "Database is down for given SID($ORA_SID),
Owner($ORA_OWNER). Can't perform impot"
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_cleanup(): Cleanup interim files
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_cleanup() {
rm –f ${PIPE_DEVICE}
rm –f ${SPLIT_PIPE_DEVICE}
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_desplitcompress_pipe(): Creates pipe for uncompressing and
desplitting of file
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_desplitcompress_pipe() {
# Creates pipe for uncompressing
if [ ! -r ${PIPE_DEVICE} ]; then
/etc/mknod ${PIPE_DEVICE} p
fi

#Creates pipe for desplitting
if [ ! -r ${SPLIT_PIPE_DEVICE} ]; then
/etc/mknod ${SPLIT_PIPE_DEVICE} p
fi

nohup cat ${ZFILES} > ${SPLIT_PIPE_DEVICE} &
sleep 5
nohup uncompress < ${SPLIT_PIPE_DEVICE} >${PIPE_DEVICE} &
sleep 30
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_build_parfile(): Creates parameter file
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_build_parfile() {
# This line makes sure that it always creates a new parameter file
echo " " >${PARFILE}
echo "userid=system/manager">>${PARFILE}
#echo "indexfile=${BACKUPDIR}/${ORA_SID}.ddl">>${PARFILE}
#echo "Owner=scott">>${PARFILE}
#echo "Fromuser=kishan">>${PARFILE}
#echo "Touser=aravind">>${PARFILE}
#echo "Tables=T1,T2,t3,t4">>${PARFILE}
echo "Full=Y">>${PARFILE}
echo "Ignore=Y">>${PARFILE}
echo "Commit=y">>${PARFILE}
echo "File=${PIPE_DEVICE}">>${PARFILE}
echo "Log=${BACKUPDIR}/${ORA_SID}.imp.log">>${PARFILE}
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_import(): Import the database
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_import() {
${ORACLE_HOME}/bin/imp parfile=${PARFILE}
if [ $? != 0 ]; then
echo ´date´ >> $LOGDIR/${ORA_SID}.log
echo "IMPORT_FAIL: ${ORA_SID}, Import Failed" >> $LOGDIR/${ORA_SID}.log
funct_cleanup
exit 1
fi
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_parm(): Check for input parameters
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_parm() {
if [ ${NARG} -ne 2 ]; then
echo "IMPORT_FAIL: ${ORA_SID}, Not enough arguments passed"
exit 1
fi
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_bkup_dir(): Check for backup directories
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_bkup_dir() {
if [ ! -d ${DYN_DIR} ]; then mkdir -p ${DYN_DIR}; fi
if [ ! -d ${LOGDIR} ]; then mkdir -p ${LOGDIR}; fi
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_get_vars(): Get environment variables
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_get_vars(){
ORA_HOME=´sed /#/d ${ORATABDIR}|grep -i ${ORA_SID}|nawk -F ":" '{print $2}'´
ORA_BASE=´echo ${ORA_HOME}|nawk -F "/" '{for (i=2; i<=NF-2; i++)
print "/"$i}'´
ORACLE_BASE=´echo $ORA_BASE|tr -d " "´
ORACLE_HOME=${ORA_HOME}; export ORACLE_HOME
ORACLE_SID=${ORA_SID}; export ORACLE_SID
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_unix_command_status(): Check exit status of Unix command
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_unix_command_status() {
if [ $? != 0 ]; then
echo "´date´" >> ${LOGDIR}/${ORA_SID}.log
echo "IMPORT_FAIL: ${1} " >> ${LOGDIR}/${ORA_SID}.log
exit 1
fi
}


#######################################
## MAIN
#######################################

NARG=$#
ORA_SID=$1
ORA_OWNER=$2

# Set up environment
BACKUPDIR="/u02/${ORA_SID}/export"
ORATABDIR=/etc/oratab
TOOLS="/u01/oracomn/admin/my_dba"
# List all split files in ZFILES variable
#ZFILES=´echo ${BACKUPDIR}/file.dmp.Z??|sort´
ZFILES="${BACKUPDIR}/file.dmp.Zaa ${BACKUPDIR}/file.dmp.Zab"

DYN_DIR="${TOOLS}/DYN_FILES"
PARFILE="${DYN_DIR}/import.par"
LOGDIR="${TOOLS}/localog"

PIPE_DEVICE="/tmp/import_${ORA_SID}_pipe"
SPLIT_PIPE_DEVICE="/tmp/split_${ORA_SID}_pipe"
NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1;export NLS_LANG

echo "... Now importing .... ${ORA_SID}"

funct_chk_parm
funct_get_vars
funct_verify
funct_chk_bkup_dir
funct_desplitcompress_pipe
funct_build_parfile
funct_import
funct_cleanup

echo ´date´ >> $LOGDIR/${ORA_SID}.log
echo "${ORA_SID}, import completed successfully" >> $LOGDIR/${ORA_SID}.log

####################### END MAIN ###############################
SplitImport Script under Unix Checklist

In the main() function, set the correct values for the BACKUPDIR, ORATABDIR, and TOOLS variables highlighted in the import script. The default location of ORATABDIR is different for each flavor of Unix.

Check for the existence of the SID in the oratab file. If not already there, you must add the instance.

List all split filenames in the ZFILES variable in the main() function.

The funct_build_parfile() function builds the parameter file. By default, it performs a full import. You can modify the settings to perform a user or table import.

Pass SID and OWNER as parameters to the program:

funct_desplitcompress_pipe() The only trick here is that we need to split and uncompress the files before we use them as input to import command. That is accomplished by creating two pipes. Here, we use the cat command to send output from split files to the split pipe device. The split pipe device is passed to the uncompress command. The output from the uncompress command is sent to the Oracle import command. cat and uncompress are Unix commands. Everything else is the same as a regular import.
Oracle Software Backup

This section discusses backing up the software directories of Oracle. We have already discussed how to back up the database. Backing up software is also a very important part of a backup strategy. The software might not need to be backed up as often as the database because it does not change quite as often. But as you upgrade, or before you apply any patches to existing software, it is important to make a backup copy of it to avoid getting into trouble.

Listing 3.8 contains the script to perform a backup of Oracle software. The script takes two input parameters—SID and OWNER. SID is the instance to be backed up, and OWNER is the Unix account under which Oracle is running.
Listing 3.8 OraSoftware_ux
#####################################################################
# PROGRAM NAME: OraSoftware_ux

# PURPOSE: Backup ORACLE_HOME & ORACLE_BASE
# USAGE: $OraSoftware_ux SID OWNER
# INPUT PARAMETERS: SID(Instance name), OWNER(Owner of instance)


#####################################################################


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_verify_shutdown(): Verify that database is down
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_verify_shutdown(){
STATUS=´ps -fu ${ORA_OWNER} |grep -v grep| grep ora_pmon_${ORA_SID}´
if [ $? = 0 ]; then
echo "´date´" >> ${LOGFILE}
echo "SOFTWAREBACKUP_FAIL: ${ORA_SID}, Database is up, can't do software
backup if the database is online." |tee -a ${BACKUPLOGFILE} >>
${LOGFILE}
exit 1
fi
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_shutdown_i(): Shutdown database in Immediate mode
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_shutdown_i(){
${ORACLE_HOME}/bin/sqlplus -s << EOF
/ as sysdba
shutdown immediate;
exit
EOF
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_startup_n(): Startup database in normal mode
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_startup_n(){
${ORACLE_HOME}/bin/sqlplus -s << EOF
/ as sysdba
startup;
exit
EOF
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_software_bkup(): Backup software
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_software_bkup(){

echo "tarring ${ORA_HOME}" >> ${BACKUPLOGFILE}
echo "tarring ${ORA_BASE}" >> ${BACKUPLOGFILE}

nohup tar cvlpf - ${ORA_HOME} | compress > ${ORAHOMEFILE} 2> ${BACKUPLOGFILE}
nohup tar cvlpf - ${ORA_BASE} | compress > ${ORABASEFILE} 2> ${BACKUPLOGFILE}

#Prepare restore file
echo "zcat ${ORAHOMEFILE}| tar xvlpf - ${ORA_HOME}" > ${RESTOREFILE}
echo "zcat ${ORABASEFILE}| tar xvlpf - ${ORA_BASE}" >> ${RESTOREFILE}
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_parm(): Check for input parameters
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_parm() {
if [ ${NARG} -ne 2 ]; then
echo "SOFTWAREBACKUP_FAIL: ${ORA_SID}, Not enough arguments passed"
exit 1
fi
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_bkup_dir(): Create backup directories if not already exist
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

funct_chk_bkup_dir() {

RESTOREFILE_DIR="${BACKUPDIR}/restorefile_dir"
BACKUPLOG_DIR="${BACKUPDIR}/backuplog_dir"
SOFTWARE_DIR="${BACKUPDIR}/software_dir"

BACKUPLOGFILE="${BACKUPLOG_DIR}/backup_log_${ORA_SID}"
RESTOREFILE="${RESTOREFILE_DIR}/restorefile_${ORA_SID}"
ORAHOMEFILE="${SOFTWARE_DIR}/orahome_${ORA_SID}.tar.Z"
ORABASEFILE="${SOFTWARE_DIR}/orabase_${ORA_SID}.tar.Z"
LOGFILE="${LOGDIR}/${ORA_SID}.log"

if [ ! -d ${RESTOREFILE_DIR} ]; then mkdir -p ${RESTOREFILE_DIR}; fi
if [ ! -d ${BACKUPLOG_DIR} ]; then mkdir -p ${BACKUPLOG_DIR}; fi
if [ ! -d ${SOFTWARE_DIR} ]; then mkdir -p ${SOFTWARE_DIR}; fi

if [ ! -d ${DYN_DIR} ]; then mkdir -p ${DYN_DIR}; fi
if [ ! -d ${LOGDIR} ]; then mkdir -p ${LOGDIR}; fi

# Remove old files
rm -f ${RESTOREFILE_DIR}/*
rm -f ${BACKUPLOG_DIR}/*
rm -f ${SOFTWARE_DIR}/*

echo "${JOBNAME}: software backup of ${ORA_SID} begun on ´date +\"%c\"´ "
>> ${BACKUPLOGFILE}
}


#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_get_vars(): Get environment variables
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
funct_get_vars(){
ORA_HOME=´sed /#/d ${ORATABDIR}|grep -i ${ORA_SID}|nawk -F ":" '{print $2}'´
ORA_BASE=´echo ${ORA_HOME}|nawk -F "/" '{for (i=2; i<=NF-2; i++)
print "/"$i}'´
ORACLE_BASE=´echo $ORA_BASE|tr -d " "´
init_file=$ORA_HOME/dbs/init$ORA_SID.ora
ORACLE_HOME=${ORA_HOME}; export ORACLE_HOME
ORACLE_SID=${ORA_SID}; export ORACLE_SID

if [ x$ORA_HOME = 'x' ]; then
echo "SOFTWAREBACKUP_FAIL: Can't get ORACLE_HOME from oratab for
$ORA_SID"|
tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
exit 1
fi

if [ ! -f $init_file ]; then
echo "SOFTWAREBACKUP_FAIL: int$ORA_SID.ora does not exist in
ORACLE_HOME/dbs. Used by funct_startup_n to start database"|tee -a
${BACKUPLOGFILE} >> ${LOGFILE}
exit 1
fi
}

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# funct_chk_unix_command_status(): Check the exit status of Unix command
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
funct_chk_unix_command_status() {
if [ $? != 0 ]; then
echo "´date´" |tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
echo "SOFTWAREBACKUP_FAIL: ${1}"| tee -a ${BACKUPLOGFILE} >> ${LOGFILE}
exit 1
fi
}

############################################################
## MAIN
############################################################
NARG=$#
ORA_SID=$1
ORA_OWNER=$2

# Set environment variables
BACKUPDIR="/u02/${ORA_SID}/software"
ORATABDIR=/etc/oratab
TOOLS="/u01/oracomn/admin/my_dba"

DYN_DIR="${TOOLS}/DYN_FILES"
LOGDIR="${TOOLS}/localog"
JOBNAME="orasoftware"

echo "Preparing to make software backup of ${ORA_SID}"

funct_chk_parm
funct_chk_bkup_dir
funct_get_vars
funct_shutdown_i
funct_verify_shutdown
funct_software_bkup
funct_startup_n
echo "${ORA_SID}, Software Backup Completed successfully on ´date +\"%c\"´ "
|tee -a ${BACKUPLOGFILE} >> ${LOGFILE}

######## END MAIN #########################
Oracle Software Backup Script under Unix Checklist

In the main function, set correct values for BACKUPDIR, ORATABDIR, and TOOLS variables highlighted in the software backup script. The default location of ORATABDIR is different for each flavor of Unix.

Check for the existence of the SID in the oratab file. If not already there, you must add the instance.

If your Oracle software directory structure does not follow OFA guidelines, set ORA_BASE and ORA_HOME manually in funct_get_vars().

Pass SID and OWNER as parameters to the program:

funct_software_bkup() This function tars the software directories of ORACLE_HOME and ORACLE_BASE and compresses the output using the compress command. Here, we are assuming that the Oracle software is installed using OFA (optimal Flexible Architecture) guidelines. If not, you have to manually set ORA_BASE and ORA_HOME in the funct_get_vars() function. 'nohup' commands submit the tar command at the server.

main() If the database is running, it shuts down the database and starts backing up the software directories. When the backup is complete, it restarts the database.

Troubleshooting and status check:

The important thing here is that the backup log file defined by BACKUPLOGFILE contains detailed information about each step of the backup process. This is a very good place to start investigating why a backup has failed or for related errors. This file will also have the start and end time of backup.

A single line about the success or failure of backup is appended to SID.log file every time backup is performed. This file is located under the directory defined by the LOGDIR variable. The messages for a software backup are 'SOFTWAREBACKUP_FAIL', if the software backup failed, and 'Software Backup Completed', successfully', if the backup completes successfully.
Restoring Oracle Software

The steps to restore the software are as follows:

Shutdown database.

Use restore file from the backup to restore the directories.

Start up the database.

The restore command in the restore file first does a zcat (uncompress and cat) of the output file and passes it to tar for extraction. For example,
zcat ora_home.tar.Z | tar xvlpf - /oracle/ora81

ora_home.tar.Z: File to extract
/oracle/ora81: Destination directory

ORACLE DATABASE ADMINISTRATOR
Oracle backup and recovery scripts, part III: Backup and recovery under Windows NT
Rajendra Gutta
06.24.2003
Rating: -4.05- (out of 5)


This is the third of a series of excerpts from "Oracle DBA automation scripts" by Rajendra Gutta. Click for part one on cold backup under Unix and part two on hot backup under Unix. You can purchase the book here.

This section presents and explains the scripts for taking a backup and recovering a database in the Windows NT environment. We use the DOS Shell batch programming techniques to automate the backup process. After the backup is complete, it is important to check the backup status by reviewing log and error files generated by the scripts.
Cold Backup

Listing 3.9 performs a cold backup of a database under the Windows NT environment. The cold backup script takes SID, the instance to be backed up, as the input parameter. The general steps to write a backup script in Unix and Windows NT are the same. The only difference is that we will be using commands that are understood by Windows NT. Figure 3.6 shows the functionality of a cold backup program under Windows NT. Each box represents a corresponding section in the program. For example, the Parameter Checking section checks for the necessary input parameters and also checks for the existence of the backup directories.


Listing 3.9 coldbackup_nt.bat
@echo off
REM ##############################################################
REM PROGRAM NAME: coldbackup_nt.bat

REM PURPOSE: This utility performs cold backup of
REM the database on Windows NT
REM USAGE: c:\>coldbackup_nt.bat SID

REM INPUT PARAMETERS: SID (Instance name)
''
REM ###############################################################

REM ::::::::::::::::::::Begin Declare Variables Section

set ORA_HOME=c:\oracle\ora81\bin
set CONNECT_USER="/ as sysdba"
set ORACLE_SID=%1
set BACKUP_DIR=c:\backup\%ORACLE_SID%\cold
set INIT_FILE=c:\oracle\admin\orcl\pfile\init.ora set
TOOLS=c:\oracomn\admin\my_dba
set LOGDIR=%TOOLS%\localog
set LOGFILE=%LOGDIR%\%ORACLE_SID%.log

set CFILE=%BACKUP_DIR%\log\coldbackup.sql
set ERR_FILE=%BACKUP_DIR%\log\cerrors.log
set LOG_FILE=%BACKUP_DIR%\log\cbackup.log
set BKP_DIR=%BACKUP_DIR%

REM :::::::::::::::::::: End Declare Variables Section

REM :::::::::::::::::::: Begin Parameter Checking Section

if "%1" == " goto usage

REM Create backup directories if already not exist
if not exist %BACKUP_DIR%\data mkdir %BACKUP_DIR%\data
if not exist %BACKUP_DIR%\control mkdir %BACKUP_DIR%\control
if not exist %BACKUP_DIR%\redo mkdir %BACKUP_DIR%\redo
if not exist %BACKUP_DIR%\log mkdir %BACKUP_DIR%\log
if not exist %LOGDIR% mkdir %LOGDIR%

REM Check to see that there were no create errors
if not exist %BACKUP_DIR%\data goto backupdir
if not exist %BACKUP_DIR%\control goto backupdir
if not exist %BACKUP_DIR%\redo goto backupdir
if not exist %BACKUP_DIR%\log goto backupdir

REM Deletes previous backup. Make sure you have it on tape.
del/q %BACKUP_DIR%\data\*
del/q %BACKUP_DIR%\control\*
del/q %BACKUP_DIR%\redo\*
del/q %BACKUP_DIR%\log\*

echo. > %ERR_FILE%
echo. > %LOG_FILE%
(echo Cold Backup started & date/T & time/T) >> %LOG_FILE%

echo Parameter Checking Completed >> %LOG_FILE%
REM :::::::::::::::::::: End Parameter Checking Section

REM :::::::::::::::::::: Begin Create Dynamic files Section
echo. >%CFILE%
echo set termout off heading off feedback off >>%CFILE%
echo set linesize 300 pagesize 0 >>%CFILE%
echo set serveroutput on size 1000000 >>%CFILE%
echo. >>%CFILE%
echo spool %BACKUP_DIR%\log\coldbackup_list.bat >>%CFILE%
echo. >>%CFILE%
echo exec dbms_output.put_line('@echo off' ); >>%CFILE%
echo. >>%CFILE%
echo exec dbms_output.put_line('REM ******Data files' ); >>%CFILE%
echo select 'copy '^|^| file_name^|^| ' %BKP_DIR%\data '
>>%CFILE%
echo from dba_data_files order by tablespace_name; >>%CFILE%
echo. >>%CFILE%
echo exec dbms_output.put_line('REM ******Control files' ); >>%CFILE%
echo select 'copy '^|^| name^|^| ' %BKP_DIR%\control ' >>%CFILE%
echo from v$controlfile order by name; >>%CFILE%
echo. >>%CFILE%
echo exec dbms_output.put_line('REM ******Init.ora file ' );
>>%CFILE%
echo select ' copy %INIT_FILE% %BKP_DIR%\control ' >>%CFILE%
echo from dual; >>%CFILE%
echo exec dbms_output.put_line('exit;'); >>%CFILE%
echo spool off >>%CFILE%
echo exit >>%CFILE%

echo Dynamic files Section Completed >> %LOG_FILE%
REM :::::::::::::::::::: End Create Dynamic files Section


REM :::::::::::::::::::: Begin ColdBackup Section

%ORA_HOME%\sqlplus -s %CONNECT_USER% @%CFILE%
%ORA_HOME%\sqlplus -s %CONNECT_USER% @shutdown_i_nt.sql
%ORA_HOME%\sqlplus -s %CONNECT_USER% @startup_r_nt.sql
%ORA_HOME%\sqlplus -s %CONNECT_USER% @shutdown_n_nt.sql

REM Copy the files to backup location
start/b %BACKUP_DIR%\log\coldbackup_list.bat 1>> %LOG_FILE% 2>> %ERR_FILE%
%ORA_HOME%\sqlplus -s %CONNECT_USER% @startup_n_nt.sql

(echo ColdBackup Completed Successfully & date/T & time/T) >> %LOG_FILE%
(echo ColdBackup Completed Successfully & date/T & time/T) >> %LOGFILE%
goto end

REM :::::::::::::::::::: End ColdBackup Section


REM :::::::::::::::::::::::::::: Begin Error handling section

:usage
echo Error, Usage: coldbackup_nt.bat SID
goto end

:backupdir
echo Error creating Backup directory structure >> %ERR_FILE%
(echo COLDBACKUP_FAIL:Error creating Backup directory structure
& date/T & time/T) >> %LOGFILE%
REM :::::::::::::::::::: End Error handling section

REM :::::::::::::::::::: Cleanup Section
:end
set ORA_HOME=
set ORACLE_SID=
set CONNECT_USER=
set BACKUP_DIR=
set INIT_FILE=
set CFILE=
set ERR_FILE=
set LOG_FILE=
set BKP_DIR=
Cold Backup Script for Windows NT Checklist

Check to see that ORA_HOME, BACKUP_DIR, and TOOLS are set to correct values according to your directory structure. These variables are highlighted in the script.

Verify that CONNECT_USER is set to correct the username and password.

Define the INIT_FILE variable to the location of the Init.ora file.

Be sure that the user running the program has Write access to backup directories.

When you run the program, pass SID as a parameter.
Cold Backup under Windows NT Troubleshooting and Status Check

Backup log files defined by LOG_FILE contain detailed information about each step of the backup process. This is a very good place to start investigating why a backup has failed or for related errors. This file will also have the start and end time of backup. ERR_FILE has error information.

A single line about the success or failure of backup is appended to the SID.log file every time a backup is performed. This file is located under the directory defined by the LOGDIR variable. The messages for a cold backup are 'COLDBACKUP_FAIL', if the cold backup failed, and 'Cold Backup Completed successfully', if the backup completes successfully.

You can schedule automatic backups using the 'at' command, as shown in the following:
at 23:00 "c:\backup\coldbackup_nt.bat ORCL"
Runs at 23:00 hrs on current date.

at 23:00 /every:M,T,W,Th,F "c:\backup\coldbackup_nt.bat ORCL "

This command runs a backup at 23:00 hours every Monday, Tuesday, Wednesday, Thursday, and Friday.

The "Create Dynamic Files" section in the coldbackup_nt.bat program creates the coldbackup.sql file (see Listing 3.10) under the log directory. coldbackup.sql is called from coldbackup_nt.bat and generates a list of data, control, and redo log files to be backed up from the database. A sample coldbackup.sql is shown in Listing 3.10 for your understanding. The contents of this file are derived based on the structure of the database.
Listing 3.10 coldbackup.sql
set termout off heading off feedback off
set linesize 300 pagesize 0
set serveroutput on size 1000000

spool c:\backup\orcl\cold\log\coldbackup_list.bat

exec dbms_output.put_line('@echo off' );

exec dbms_output.put_line('REM ******Data files' );
select 'copy '|| file_name|| ' c:\backup\orcl\cold\data '
from dba_data_files order by tablespace_name;

exec dbms_output.put_line('REM ******Control files' );
select 'copy '|| name|| ' c:\backup\orcl\cold\control '
from v$controlfile order by name;

exec dbms_output.put_line('REM ******Init.ora file ' );
select ' copy c:\oracle\admin\orcl\pfile\init.ora
c:\backup\orcl\cold\control '
from dual;
exec dbms_output.put_line('exit;');
spool offexit

When the coldbackup.sql file is called from the coldbackup_nt.bat program, it spools output to the coldbackup_list.bat DOS batch file (see Listing 3.11). This file has the commands necessary for performing the cold backup.

This is only a sample file. Note that in the contents of file data, control, redo log, and Init.ora files are copied to respective backup directories.
Listing 3.11 coldbackup_list.bat
@echo off

REM ******Data files
copy C:\ORADATA\DSGN01.DBF c:\backup\orcl\cold\data
copy C:\ORADATA\INDX01.DBF c:\backup\orcl\cold\data
copy C:\ORADATA\OEM01.DBF c:\backup\orcl\cold\data
copy C:\ORADATA\RBS01.DBF c:\backup\orcl\cold\data
copy C:\ORADATA\SYSTEM01.DBF c:\backup\orcl\cold\data
copy C:\ORADATA\TEMP01.DBF c:\backup\orcl\cold\data
copy C:\ORADATA\USERS01.DBF c:\backup\orcl\cold\data


REM ******Control files
copy C:\ORADATA\CONTROL01.CTL c:\backup\orcl\cold\control
copy C:\ORADATA\CONTROL02.CTL c:\backup\orcl\cold\control


REM ******Init.ora file
copy c:\oracle\admin\orcl\pfile\init.ora c:\backup\orcl\cold\control
exit;
Hot Backup

The hot backup program (see Listing 3.12) performs a hot backup of a database under the Windows NT environment. The hot backup script takes SID, the instance to be backed up, as the input parameter.
Listing 3.12 hotbackup_nt.bat
@echo off
REM #####################################################################
REM PROGRAM NAME: hotbackup_nt.bat

REM PURPOSE: This utility performs hot backup of
REM the database on Windows NT
REM USAGE: c:\>hotbackup_nt.bat SID

REM INPUT PARAMETERS: SID (Instance name)
''
REM #####################################################################

REM :::::::::::::::::::: Begin Declare Variables Section

set ORA_HOME=c:\oracle\ora81\bin
set CONNECT_USER="/ as sysdba"
set ORACLE_SID=%1
set BACKUP_DIR=c:\backup\%ORACLE_SID%\hot
set INIT_FILE=c:\oracle\admin\orcl\pfile\init.ora
set ARC_DEST=c:\oracle\oradata\orcl\archive

set TOOLS=c:\oracomn\admin\my_dba
set LOGDIR=%TOOLS%\localog
set LOGFILE=%LOGDIR%\%ORACLE_SID%.log

set HFILE=%BACKUP_DIR%\log\hotbackup.sql
set ERR_FILE=%BACKUP_DIR%\log\herrors.log
set LOG_FILE=%BACKUP_DIR%\log\hbackup.log
set BKP_DIR=%BACKUP_DIR%
REM :::::::::::::::::::: End Declare Variables Section

REM :::::::::::::::::::: Begin Parameter Checking Section

if "%1" == " goto usage

REM Create backup directories if already not exist
if not exist %BACKUP_DIR%\data mkdir %BACKUP_DIR%\data
if not exist %BACKUP_DIR%\control mkdir %BACKUP_DIR%\control
if not exist %BACKUP_DIR%\arch mkdir %BACKUP_DIR%\arch
if not exist %BACKUP_DIR%\log mkdir %BACKUP_DIR%\log
if not exist %LOGDIR% mkdir %LOGDIR%

REM Check to see that there were no create errors
if not exist %BACKUP_DIR%\data goto backupdir
if not exist %BACKUP_DIR%\control goto backupdir
if not exist %BACKUP_DIR%\arch goto backupdir
if not exist %BACKUP_DIR%\log goto backupdir

REM Deletes previous backup. Make sure you have it on tape.
del/q %BACKUP_DIR%\data\*
del/q %BACKUP_DIR%\control\*
del/q %BACKUP_DIR%\arch\*
del/q %BACKUP_DIR%\log\*

echo. > %ERR_FILE%
echo. > %LOG_FILE%
(echo Hot Backup started & date/T & time/T) >> %LOG_FILE%
echo Parameter Checking Completed >> %LOG_FILE%
REM :::::::::::::::::::: End Parameter Checking Section

REM :::::::::::::::::::: Begin Create Dynamic files Section
echo. >%HFILE%
echo set termout off heading off feedback off >>%HFILE%
echo set linesize 300 pagesize 0 >>%HFILE%
echo set serveroutput on size 1000000 >>%HFILE%
echo spool %BACKUP_DIR%\log\hotbackup_list.sql >>%HFILE%

echo Declare >>%HFILE%
echo cursor c1 is select distinct tablespace_name from dba_data_files
order by tablespace_name; >>%HFILE%
echo cursor c2( ptbs varchar2) is select file_name from dba_data_files
where tablespace_name = ptbs order by file_name; >>%HFILE%
echo Begin >>%HFILE%
echo dbms_output.put_line('set termout off heading off feedback off');
>>%HFILE%

echo. >>%HFILE%
echo dbms_output.put_line(chr(10) ); >>%HFILE%
echo dbms_output.put_line('host REM ******Data files' ); >>%HFILE%
echo for tbs in c1 loop >>%HFILE%
echo dbms_output.put_line(' alter tablespace '^|^| tbs.tablespace_name
^|^|' begin backup;'); >>%HFILE%
echo for dbf in c2(tbs.tablespace_name) loop >>%HFILE%
echo dbms_output.put_line(' host copy '^|^|dbf.file_name^|^|'
%BKP_DIR%\data 1^>^> %LOG_FILE% 2^>^> %ERR_FILE%'); >>%HFILE%
echo end loop; >>%HFILE%
echo dbms_output.put_line(' alter tablespace '^|^|tbs.tablespace_name
^|^|' end backup;'); >>%HFILE%
echo end loop; >>%HFILE%

echo. >>%HFILE%
echo dbms_output.put_line(chr(10) ); >>%HFILE%
echo dbms_output.put_line('host REM ******Control files ' );
>>%HFILE%
echo dbms_output.put_line(' alter database backup controlfile to
'^|^| ''^|^|'%BKP_DIR% \control\coltrol_file.ctl'^|^|''^|^|';');
>>%HFILE%
echo dbms_output.put_line(' alter database backup controlfile to trace;');
>>%HFILE%

echo. >>%HFILE%
echo dbms_output.put_line(chr(10) ); >>%HFILE%
echo dbms_output.put_line('host REM ******Init.ora file ' );
>>%HFILE%
echo dbms_output.put_line(' host copy %INIT_FILE% %BKP_DIR%\control
1^>^> %LOG_FILE% 2^>^> %ERR_FILE%'); >>%HFILE%

echo. >>%HFILE%
echo dbms_output.put_line(chr(10) ); >>%HFILE%
echo dbms_output.put_line('host REM ******Archivelog files' );
>>%HFILE%
echo dbms_output.put_line(' alter system switch logfile;'); >>%HFILE%
echo dbms_output.put_line(' alter system archive log stop;'); >>%HFILE%
echo dbms_output.put_line('host move %ARC_DEST%\* %BKP_DIR%\arch
1^>^> %LOG_FILE% 2^>^> %ERR_FILE%' ); >>%HFILE%
echo dbms_output.put_line(' alter system archive log start;');
>>%HFILE%


echo dbms_output.put_line('exit;'); >>%HFILE%
echo End; >>%HFILE%
echo / >>%HFILE%
echo spool off >>%HFILE%
echo exit; >>%HFILE%

echo Dynamic files Section Completed >> %LOG_FILE%
REM :::::::::::::::::::: End Create Dynamic files Section

REM :::::::::::::::::::: Begin HotBackup Section

%ORA_HOME%\sqlplus -s %CONNECT_USER% @%HFILE%
REM Copy the files to backup location
%ORA_HOME%\sqlplus -s %CONNECT_USER% @%BACKUP_DIR%\log\hotbackup_list.sql

(echo HotBackup Completed Successfully & date/T & time/T) >> %LOG_FILE%
(echo HotBackup Completed Successfully & date/T & time/T) >> %LOGFILE%
goto end

REM :::::::::::::::::::: End HotBackup Section


REM :::::::::::::::::::: Begin Error handling section

:usage
echo Error, Usage: hotbackup_nt.bat SID
goto end

:backupdir
echo Error creating Backup directory structure >> %ERR_FILE%
(echo HOTBACKUP_FAIL:Error creating Backup directory structure
& date/T & time/T) >> %LOGFILE%
REM :::::::::::::::::::: End Error handling section

REM :::::::::::::::::::: Cleanup Section
:end
set ORA_HOME=
set ORACLE_SID=
set CONNECT_USER=
set BACKUP_DIR=
set INIT_FILE=
set ARC_DEST=
set HFILE=
set ERR_FILE=
set LOG_FILE=
set BKP_DIR=

Hot backup program functionality can be shown with the similar diagram as for a cold backup. The sections and their purposes in the program are the same as for a cold backup.
Hot Backup Script under Windows NT Checklist

Check to see that ORA_HOME, BACKUP_DIR, and TOOLS are set to the correct values according to your directory structure. These variables are highlighted in the script.

Verify that CONNECT_USER is set to the correct username and password.

Define the INIT_FILE variable to the location of the Init.ora file.

Define the ARC_DEST variable to the location archive destination.

Be sure that the user running the program has Write access to the backup directories.

When you run the program, pass SID as a parameter.
Hot Backup under Windows NT Troubleshooting and Status Check

The backup log file defined by LOG_FILE contains detailed information about each step of the backup process. This is a very good place to start investigating why a backup has failed or for related errors. This file will also have the start and end time of backup. ERR_FILE has error information.

A single line about the success or failure of backup is appended to the SID.log file every time a backup is performed. This file is located under the directory defined by the LOGDIR variable. The messages for a hot backup are 'HOTBACKUP_FAIL', if a hot backup failed, and 'Hot Backup Completed successfully', if a backup completes successfully.

The "Create Dynamic Files" section, in the hotbackup_nt.bat creates the hotbackup.sql file (see Listing 3.13) under the log directory. This generates a list of tablespaces, data, control, and redo log files from the database. It is called from the hotbackup_nt.bat program.
Listing 3.13 hotbackup.sql
set termout off heading off feedback off
set linesize 300 pagesize 0
set serveroutput on size 1000000
spool c:\backup\orcl\hot\log\hotbackup_list.sql
Declare
cursor c1 is select distinct tablespace_name from dba_data_files
order by tablespace_name;
cursor c2( ptbs varchar2) is select file_name from dba_data_files
where tablespace_name = ptbs order by file_name;
Begin
dbms_output.put_line('set termout off heading off feedback off');

dbms_output.put_line(chr(10) );
dbms_output.put_line('host REM ******Data files' );
for tbs in c1 loop
dbms_output.put_line(' alter tablespace '|| tbs.tablespace_name ||'
begin backup;');
for dbf in c2(tbs.tablespace_name) loop
dbms_output.put_line(' host copy '||dbf.file_name||'
c:\backup\orcl\hot\data 1>> hbackup.log 2>> herrors.log');
end loop;
dbms_output.put_line(' alter tablespace '||tbs.tablespace_name ||
' end backup;');
end loop;

dbms_output.put_line(chr(10) );
dbms_output.put_line('host REM ******Control files ' );
dbms_output.put_line(' alter database backup controlfile to '||
''||'c:\backup\orcl\hot\control\coltrol_file.ctl
'||''||';');
dbms_output.put_line(' alter database backup controlfile to trace;');

dbms_output.put_line(chr(10) );
dbms_output.put_line('host REM ******Init.ora file ' );
dbms_output.put_line('host copy
c:\oracle\admin\orcl\pfile\init.orac:\backup\orcl\hot\control
1>> hbackup.log 2>> herrors.log');

dbms_output.put_line(chr(10) );
dbms_output.put_line('host REM ******Archivelog files' );
dbms_output.put_line(' alter system switch logfile;');
dbms_output.put_line(' alter system archive log stop;');
dbms_output.put_line('host move c:\oracle\oradata\orcl\archive\*
c:\backup\orcl\hot\arch 1>> hbackup.log 2>> herrors.log' );
dbms_output.put_line(' alter system archive log start;');
dbms_output.put_line('exit;');
End;
/
spool off
exit;

The hotbackup.sql file is called from hotbackup_nt.bat and it spools output to the hotbackup_list.sql SQL file (see Listing 3.14). This file has the commands necessary for performing a hot backup.

This is only a sample file. Note in the file that the data, control, archive log, and Init.ora files are copied to their respective backup directories. First, it puts the tablespace into Backup mode, copies the corresponding files to backup location, and then turns off the Backup mode for that tablespace. This process is repeated for each tablespace, and each copy command puts the status of the copy operation to hbackup.log and reports any errors to the herrors.log file.

Listing 3.14 is generated based on the structure of the database. In a real environment, the database structure changes as more data files or tablespaces get added. Because of this, it is important to generate the backup commands dynamically, as shown in hotbackup_list.sql. It performs the actual backup and is called from hotbackup_nt.bat.
Listing 3.14 hotbackup_list.sql
set termout off heading off feedback off

host REM ******Data files
alter tablespace DESIGNER begin backup; host copy C:\ORADATA\DSGN01.DBF
c:backup\orcl\hot\data
1>> hbackup.log 2>> herrors.logalter tablespace DESIGNER end backup;
alter tablespace DESIGNER_INDX begin backup;
host copy C:\ORADATA\DSGN_INDX01.DBF c:backup\orcl\hot\data
1>> hbackup.log 2>> herrors.log
alter tablespace DESIGNER_INDX end backup;
alter tablespace INDX begin backup;
host copy C:\ORADATA\INDX01.DBF c:backup\orcl\hot\data 1>> hbackup.log
2>> herrors.log
alter tablespace INDX end backup;
alter tablespace OEM_REPOSITORY begin backup;
host copy C:\ORADATA\OEMREP01.DBF c:backup\orcl\hot\data
1>> hbackup.log 2>> herrors.log
alter tablespace OEM_REPOSITORY end backup;

host REM ******Control files
alter database backup controlfile to 'c:\hot\control\coltrol_file.ctl';
alter database backup controlfile to trace;

host REM ******Init.ora file
host copy c:\oracle\admin\orcl\pfile\init.ora c:backup\orcl\hot\control
1>> hbackup.log2>> herrors.log

host REM ******Archivelog files
alter system switch logfile;
alter system archive log stop;
host move c:\oracle\oradata\orcl\archive\* c:\backup\orcl\hot\arch
1>> hbackup.log 2>> herrors.log
alter system archive log start;
exit;
Export

The export program (see Listing 3.15) performs a full export of the database under a Windows NT environment. The export script takes SID, the instance to be backed up, as the input parameter.
Listing 3.15 export_nt.bat
@echo off
REM #####################################################################
REM PROGRAM NAME: export_nt.bat

REM PURPOSE: This utility performs a full export of
REM database on Windows NT
REM USAGE: c:\>export_nt.bat SID

REM INPUT PARAMETERS: SID (Instance name)
''
REM #####################################################################

REM :::::::::::::::::::: Begin Declare Variables Section

set ORA_HOME=c:\oracle\ora81\bin
set ORACLE_SID=%1
set CONNECT_USER=system/manager
set BACKUP_DIR=c:\backup\%ORACLE_SID%\export

set TOOLS=c:\oracomn\admin\my_dba
set LOGDIR=%TOOLS%\localog
set LOGFILE=%LOGDIR%\%ORACLE_SID%.log

REM :::::::::::::::::::: End Declare Variables Section

REM :::::::::::::::::::: Begin Parameter Checking Section

if "%1" == " goto usage

REM Create backup directories if already not exist
if not exist %BACKUP_DIR% mkdir %BACKUP_DIR%
if not exist %LOGDIR% mkdir %LOGDIR%


REM Check to see that there were no create errors
if not exist %BACKUP_DIR% goto backupdir

REM Deletes previous backup. Make sure you have it on tape.
del/q %BACKUP_DIR%\*

REM :::::::::::::::::::: End Parameter Checking Section

REM :::::::::::::::::::: Begin Export Section

%ORA_HOME%\exp %CONNECT_USER% parfile=export_par.txt
(echo Export Completed Successfully & date/T & time/T) >> %LOGFILE%
goto end

REM :::::::::::::::::::: End Export Section


REM :::::::::::::::::::: Begin Error handling section

:usage
echo Error, Usage: coldbackup_nt.bat SID
goto end

:backupdir
echo Error creating Backup directory structure
(echo EXPORT_FAIL:Error creating Backup directory structure
& date/T & time/T) >> %LOGFILE%

REM :::::::::::::::::::: End Error handling section


REM ::::::::::::::::::::Cleanup Section
:end
set ORA_HOME=
set ORACLE_SID=
set CONNECT_USER=
set BACKUP_DIR=

This program performs an export of the database by using the parameter file specified by export_par.txt. In Listing 3.16 is a sample parameter file that performs a full export of the database. You can modify the parameter file to suit to your requirements.
Listing 3.16 export_par.txt
file= %BACKUP_DIR%\export.dmp
log= %BACKUP_DIR%\export.log
full=y
compress=n
consistent=y
Export Script under Windows NT Checklist

Check to see that ORA_HOME and BACKUP_DIR, TOOLS are set to correct values according to your directory structure. These variables are highlighted in the program.

Verify that CONNECT_USER is set to the correct username and password.

Be sure that the user running the program has Write access to the backup directories.

Edit the parameter file to your specific requirements. Specify the full path of the location of your parameter file in the program.

When you run the program, pass SID as a parameter.
Export under Windows NT Troubleshooting and Status Check

The log file specified in the parameter file contains detailed information about each step of the export process. This is a very good place to start investigating why an export has failed or for related errors.

A single line about the success or failure of export is appended to the SID.log file every time an export is performed. This file is located under the directory defined by the LOGDIR variable. The messages for an export are 'EXPORT_FAIL', if the export failed, and 'Export Completed successfully', if the export completes successfully.

Reader Feedback:

Frank P. writes: Your article on Windows backups for Oracle is incorrect. In a hot backup, Oracle recommends the use of the supplied OCOPY utility, NOT Windows copy. See Metalink note #139327.1. Also, I believe that the scripts are using O/S authentication. This has major security risks and should be noted. Otherwise, good tip.

No comments: