Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Script to synchronize Pandium's Oracle archives from Orion's
- # flash_recovery_area. It is meant to be called continously from
- # a cron job, under the "oracle" user. The script makes sure that
- # only one sync job runs at a time - in case of previous job still
- # running. It also logs everything to /$LOG_DIR/$BASENAME/$DATE.log.
- # Constants/configs
- LOG_DIR="/logs"
- LOCK_DIR="/tmp"
- # Internal timekeeping/bookkeeping
- DATE=$(date +"%Y-%m-%d")
- TIME=$(date +"%H:%M:%S")
- TIMER_BEFORE=$(date +"%s")
- FILENAME=`basename $0`
- BASENAME=${FILENAME%\.*}
- LOCKFILE="${LOCK_DIR}/${BASENAME}.lock"
- LOGFILE_PATH="${LOG_DIR}/${BASENAME}"
- LOGFILE="$LOGFILE_PATH/${DATE}.log"
- # Make sure LOGFILE_PATH exists, so we can write a log file in this directory
- if [[ ! -e $LOGFILE_PATH ]]; then
- mkdir $LOGFILE_PATH
- fi
- echo "--------------------------------------------------------" >> $LOGFILE
- # Check if there's a lockfile created by previous script invocation
- if [ -e $LOCKFILE ]; then
- # If so, old job is still running, so we do nothing and let it finish
- echo "${TIME} Synchronization of /flash_recovery_area already running, aborting!" >> $LOGFILE
- else
- # No old instance running, so we're free to apply new archives
- echo "${TIME} Synchronization of /flash_recovery_area starting..." >> $LOGFILE
- # Create lock file, so we ensure only one instance runs
- echo 1 > $LOCKFILE
- # Check if we have mounted Orions flash recovery area
- if mount | grep -q '//orion.domain.com/flash_recovery_area/PROD/archivelog'; then
- echo "Files already accessable at mount-point!" >> $LOGFILE
- else
- # Mount Orions flash recovery area
- echo "No mount detected, mounting now!" >> $LOGFILE
- mount /backup/oracle/flash_recovery_area/FROM_PROD/archivelog &>> $LOGFILE
- sleep 1
- # Enter a wait loop until we have access to the new mount
- while ls /backup/oracle/flash_recovery_area/FROM_PROD/archivelog/* | grep -q 'cannot access' ;
- do
- sleep 1
- echo "Waiting for data to show up at mount-point..." >> $LOGFILE
- done
- echo "Files now accessable at mount-point!" >> $LOGFILE
- fi
- export ORACLE_SID=prod
- export ORACLE_HOME=/oracle/app/oracle/product/10.2.0/db_1
- $ORACLE_HOME/bin/rman cmdfile=/scripts/rman_recover_database.rcv >> $LOGFILE
- # Ensure our latest symlink is up to date
- if [[ -e ${LOGFILE_PATH}/latest.log ]]; then
- rm ${LOGFILE_PATH}/latest.log -f
- fi
- ln -s ${LOGFILE} ${LOGFILE_PATH}/latest.log
- # We're done synchronizing, so it's Ok to remove lock file
- rm -f $LOCKFILE
- TIMER_AFTER=$(date +"%s")
- DIFF=$(($TIMER_AFTER-$TIMER_BEFORE))
- DIFF_STRING="$(($DIFF / 60))m $(($DIFF % 60))s"
- TIME=$(date +"%H:%M:%S")
- echo "${TIME} Done synchronizing. Took ${DIFF_STRING}." >> $LOGFILE
- # Make sure that any log file older than a week is added to long-term log archive
- find ${LOGFILE_PATH}/*.log -type f -mtime +6 | xargs tar rvf ${LOGFILE_PATH}/archive.tar > /dev/null 2>&1
- # Delete the discrete log file we just added to long-term log archive
- find ${LOGFILE_PATH}/*.log -type f -mtime +6 -delete >/dev/null 2>&1
- fi
Advertisement
Add Comment
Please, Sign In to add comment