oracledba.help
Scripts

vcSnapshot.sh

<- Scripts

This script uses an include file (inc_system.sh) for common\system variables as shown here.

#!/bin/bash
# Purpose:      Export packages, functions and related metadata.
# Version:      2019.09.11
# Dependencies: inc_system.sh
#
# usrDirObj & usrDirExp
#   mkdir /u03/exports/vc
#   CREATE OR REPLACE DIRECTORY VC AS '/u03/exports/vc';
#   GRANT read,write ON DIRECTORY VC TO system;
#
# usrScpDest
#   mkdir /mnt/smb01/exports/vc
##################################################################

######################################
# Preamble: 2018.10.01               #
######################################
set -a; DIR_SCRIPTS="/u01/app/scripts"; source $DIR_SCRIPTS/inc_system.sh
sFullName=$(basename "$0"); me=${sFullName%.*}
sSLog="$DIR_LOGS/$me.sess.log"; sHLog="$DIR_LOGS/$me.hist.log"; > $sSLog
if [[ $MAINT_WINDOW -eq 1 ]]; then printf "Maintenance Window Detected - Exiting\n"; exit; fi

# User Vars
usrDirExp="/u03/exports/vc"; # Dir for export .dmp Files. Ex: "/u03/exports/vc"
usrDirExpRet=30;          # Days to keep on DirObj dir. 0=DontDelete
usrDirObj="VC";           # Oracle DirObj used for export. SELECT * FROM dba_directories;
usrDpUn="system";         # Data Pump username.
usrDpPW="********";
usrOraSID="myinstance";   # ORACLE_SID of database for export. Case sensitive!

usrEmailList="scott@mycompany.com"; # Space delimited list of email address to get warnings.

usrScpMtPt="/mnt/smb01";
usrScpDest="root@localhost:$usrScpMtPt/exports/vc";
usrScpPW="********";
usrScpDelLocal=0;         # If value is 1 .dmp file deleted from usrDirExp after SCP (overrides usrDirExpRet).

######################################
# Init Script Actions\Functions\Vars #
######################################

# Vars
nExpStatus=1;
sDateStr=$(date "+%Y%m%d")
ORACLE_SID="$usrOraSID";
sParFile="$DIR_TMP/$me.$ORACLE_SID.par"
sExpFile="$me.$ORACLE_SID.$sDateStr";

######################################
# Start                              #
######################################
clear; log "$sSLog" "$sFullName Started"; linesep
SysVars_show
printf "ORACLE_SID:  $ORACLE_SID\n"
printf "sParFile:    $sParFile\n"
printf "sExpFile:    $sExpFile\n"
linesep
printf "usrDirObj:   $usrDirObj\n"
printf "usrDirExp:   $usrDirExp\n"
printf "usrScpDest:  $usrScpDest\n"
linesep "="
sleep 5

# Sweep
log "$sSLog" "Sweeeping"
rm $usrDirExp/$sExpFile.* > /dev/null 2>&1
rm $sParFile > /dev/null 2>&1

# Retention Mgr
log "$sSLog" "Retention Mgr"
if [[ $usrDirExpRet -ne 0 ]]; then
   find $usrDirExp/*.dmp -type f -mtime +$usrDirExpRet -delete > /dev/null 2>&1
   find $usrDirExp/*.log -type f -mtime +$usrDirExpRet -delete > /dev/null 2>&1
fi

# Create .par File
log "$sSLog" "Create .par File"

cat > $sParFile <<EOF
# Created By: $sFullName
CLUSTER=N
COMPRESSION=METADATA_ONLY
COMPRESSION_ALGORITHM=HIGH
DIRECTORY=$usrDirObj
DUMPFILE=$sExpFile.dmp
EXCLUDE=STATISTICS
FILESIZE=32g
FULL=YES
JOB_NAME=$me
LOGFILE=$sExpFile.log
CONTENT=METADATA_ONLY
PARALLEL=1
REUSE_DUMPFILES=YES
EOF


# Export
log "$sSLog" "Executing expdp ($ORACLE_SID)"
$ORACLE_HOME/bin/expdp $usrDpUn/$usrDpPW PARFILE=$sParFile
if [[ $? -eq 1 ]]; then
   log "$sSLog" "Export QC Check FAILED ($?)"
   mail -s "$HOSTNAME.$sFullName $ORACLE_SID Error" "$usrEmailList" <<< "Export QC Check: FAILED"
else
   log "$sSLog" "Export QC Check: PASSED ($?)"
   nExpStatus=0
fi


# If Export OK: SCP
if [[ $nExpStatus -eq 0 ]]; then
   # SCP
   if ! [[ -z "$usrScpDest" ]]; then

        # SCP: Operation
        if [[ $(findmnt -rno SOURCE $usrScpMtPt) ]]; then
           log "$sSLog" "SCP: Operation"
           sshpass -p "$usrScpPW" scp $usrDirExp/$sExpFile.dmp $usrScpDest

           # SC: QC
           if [[ $? -eq 0 ]]; then
              log "$sSLog" "SCP Operation: OK"
              # Delete .dmp File?
              if [[ $usrScpDelLocal -eq 1 ]]; then
                 log "$sSLog" "Deleting Local .dmp File"
                 rm $usrDirExp/$sExpFile.*
              fi
           else
              log "$sSLog" "SCP Operation: FAILED"
              mail -s "$HOSTNAME.$sFullName $ORACLE_SID Error" "$usrEmailList" <<< "SCP Operation: FAILED"
           fi
        fi

   fi
fi

######################################
# End                                #
######################################
log "$sSLog" "$sFullName Ended [Elapse Time: $(elapse)]";
echo $(linesep "=") >> $sHLog; cat $sSLog >> $sHLog;
tail -32768 $sHLog > $sHLog.tmp; mv $sHLog.tmp $sHLog