#!/bin/ksh
#
# 2001-07-10 by Tom Kranz (tom@siliconbunny.com)

# This script is distributed under the GNU Public License (GPL) with the
# following extra conditions:
# - attritbution must be maintained
# - CD-ROM or similar media for commercial distribution without the prior 
#   approval of the author

# A joint effort with the DBAs at this site
# Specific SQL scripts and some other information has been removed
# 
# DO NOT USE THIS SCRIPT IN PRODUCTION!
#
# It is provided as an example of what is possible, and will be very unlikely
# to work in your environment. It may even break things quite badly.

# Basic idea - unmount our local BCV volumes, mirror them up to the main disks
# split them again, and remount them locally again

# Set the environment
export ORACLE_HOME=/opt/oracle8/product/8.1.7
export ORACLE_SID=WAREHOUSE
export ORACLE_OWNER=oracle
export ORACLE_BASE=/opt/oracle8/
export ORACLE_TERM=sun5
export ORACLE_NLS33=$ORACLE_HOME/ocommon/nls/admin/data
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/jdbc/lib
export PATH=/bin:/usr/sbin:/sbin:$PATH

# Ungracefully kill off Oracle
# This is our local Oracle database, which is about to be blown away
/usr/bin/su oracle -c $ORACLE_HOME/bin/dbabort

# A simple naming structure works wonders
VOLUMES="vol01 vol02 vol03 vol04 vol05 vol06 vol07 vol08 vol09 vol10 vol11 vol13"

# Unmount the volumes 
for vol in $VOLUMES
do
	if /sbin/umount -f /dev/vx/dsk/oradg/$vol ; then
		echo "Volume $vol successfully unmounted"
	else
		echo "Volume $vol umount failed - vxvol will abort the script"
		exit 1
	fi
done

# Stop the volumes, and export the disk groups
if /usr/sbin/vxvol -g oradg stopall; then
	echo "vxvol successfully stopped the oradg disk group"
else
	echo "vxvol could not stop the oradg disk group - script aborted"
	exit 1
fi
if /usr/sbin/vxdg -C deport oradg; then
	echo "vxdg successfully deported the oradg disk group"
else
	echo "vxdg was unable to deport the oradg disk group - script aborted"
	exit 1
fi

# Here we go to the EMC and remirror the BCVs
# NOTE: the 'establish' parameter means establish the mirror onto the BCVs
# We don't want to go the other way, otherwise we'll overwrite all our data 
# with the cruft from the BCVs
# That might be considered bad by other people
/usr/symcli/bin/symmir -g bcv -noprompt establish

# A very noddy loop to essentially wait until the mirroring is finished
syncronized=2
until [ $syncronized -eq 1 ] ;
do
	sleep 60
	/usr/symcli/bin/symmir -g bcv query > /tmp/syncfile
	/usr/bin/grep SyncInProg /tmp/syncfile > /dev/null
	syncronized=$?
done

## Stick the tablespaces in hot backup mode
# At this stage, you have either two options:
# - prod runs in hot backup mode all the time :-(
# - you execute a clever script here that puts all your tablespaces into
#   hot backup mode
# I would suggest the latter course of action
# <some clever sql script to put prod into hot backup mode>

# Split out the BCVs again
# We leave the archive/redo logs area until a while after
# This means we have extra logs after the data has been split off, which might
# be useful if we ever bothered to do a proper recovery on startup :-)
echo 'Start Split of vols dev 1,3-12 (u01,u03-u010,backup)'
/usr/symcli/bin/symmir -g bcv -noprompt split DEV001 DEV003 DEV004 DEV005 DEV006 DEV007 DEV008 DEV009 DEV010 DEV011 DEV012
echo 'Finished Split of vols dev 1,3-12 (u01,u03-u010,backup)'

## Archive any stuff hanging around in the online redo logs
# Another clever SQL script will be called here to do this
# <more clever sql>

# Archive all logfiles on prod
echo 'Start Split of vols dev 2,13-16 (u02,arch)'
/usr/symcli/bin/symmir -g bcv -noprompt split DEV002 DEV013 DEV014 DEV015 DEV016
echo 'Finished Split of vols dev 2,13-16 (u02,arch)'

# Wait for split to finish
# Again, this is noddy, and there are better ways of doing this
syncronized=2
until [ $syncronized -eq 0 ] ;
do
	sleep 60
	/usr/symcli/bin/symmir -g bcv query > /tmp/syncfile
	/usr/bin/grep Split /tmp/syncfile > /dev/null
	syncronized=$?
done

# Now we import the disk group again, and restart the volume manager
/usr/sbin/vxdg -C import oradg
/usr/sbin/vxvol -g oradg startall

# The volumes are in a bit of state, poor things, so we'll give them a damn
# good fscking.
# That'll straighten them out
for vol in $VOLUMES
do
	/usr/sbin/fsck -F vxfs -o full -y /dev/vx/rdsk/oradg/$vol > /dev/null
done

# Having finished with that sordid piece of file system debauchery, we now
# mount them all
while read VOL MOUNT
do
	/sbin/mount -F vxfs /dev/vx/dsk/oradg/$VOL /$MOUNT
done << EOD
vol01 u01
vol02 u02
vol03 u03
vol04 u04
vol05 u05
vol06 u06
vol07 u07
vol08 u08
vol09 u09
vol10 u10
vol11 backup
vol13 arch 
EOD

# No recovery is actually done - Oracle's just kicked into life
/usr/bin/su oracle -c $ORACLE_HOME/bin/dbrecover

# We'll round off by letting people know what has happened
echo "split.ksh has been successful"
echo
echo "Script finished at:" `date`
echo
echo "You owe the success of this to your sysadmin team. Buy them beer!"

# Build the datawarehouse by calling some scripts here

# And that's pretty much it
exit 0


