Cover V12, I05

Article
Figure 1
Listing 1

may2003.tar

Listing 1 Snapshot.sh shell script

#!/bin/sh

#
# Script that will stop processes using their respective init scripts,
# snapshots filesystems, and restart processes.  The snapshot directories
# are then backed up using backup program (Networker in my case).  Make
# sure there are no ".nsr" files in the original mount points as they will
# be contained in the snapshots.  The snapshots are read-only an cannot be
# modified.
#
# Make sure you modify the list of variables at the top of the script to your
# particular installation (otherwise it will most likely not work).
#
# To Use with Networker
# 1.  Change the backup command in the client resource to savepnpc on the Networker 
#     server.
# 2.  Create /nsr/res/groupname.res file where groupname is the groupname the client
#     is started from.  Make sure the groupname.res file redirects the output of 
#     this script to some file or pipe.
#
# Script located via anonymous ftp at ftp://CoE.syr.edu/pub/Solaris/scripts/snapshot.sh
#
# Change History
# Version 1.0        Initial creation to support backup of Oracle 8.0.5 database
#                which is now unsupported by Oracle.  Normal database backup
#                tools (read SQL-backtrack) also do not support recoveries of
#                unsupported Oracle databases.  Vendor support only ufsdump
#                to local tape with oracle shutdown which does give our customer
#                much uptime for their system.  With that said, I developed this
#                script to assist our efforts.
#                Christopher T. Beers - 7/12/2003    ctbeers@CoE.syr.edu
#
# Version 1.1        Updated to be more readable and portable.  Instituted for loops,
#                more variables, and some error checking.
#                Have successfully used the script to stop Oracle 8.05 and 8.1.7
#                as well as Endeavor Voyager and Computer Associates ServiceDesk
#                Christopher T. Beers - 1/22/2003    ctbeers@CoE.syr.edu
#
# Version 1.1.1        Added -F ufs to mount commands in case mount could not figure
#                this out. I was having problems with the first snapshot ever 
#                created on a system not getting mounted, but all other would. 
#                Maybe this will help.
#                Christopher T. Beers - 2/4/2003        ctbeers@CoE.syr.edu
#

#-------------EDIT VARIABLES BELOW TO YOUR INSTALLATION-----------------------------
# Init scripts in the order they need to be executed on startup
INIT_SCRIPTS_STARTUP="oracle"

# Init scripts in the order they need to be executed on shutdown
INIT_SCRIPTS_SHUTDOWN="oracle"

# Application keywords passed to ps to check to see if processes are shutdown
# I use the usernames to processes are started with.
# Write the line as you would pass it to egrep (ie. using a OR symbol between the 
# keywords)
PROCS_KEYWORDS="oracle|my_app"

# Mount points that need contain oracle data (files/tables, control files, 
# redo file, archive log files, etc) and need to be snapshot
MOUNT_POINTS="/ora01 /ora02 /ora03 /ora04 /ora10 /ora11"

# Directory to store backing-store file to track block changes in the snapshot filesystems
SCRATCH_SPACE="/var/tmp"

# Extension to add to $MOUNT_POINT when mounting the read-only image with fssnap
MOUNT_POINT_EXT="snapshot"

# Backing-store file extension
BSTORE_EXT="bstore"

# Location of fssnap
FSSNAP="/usr/sbin/fssnap"

# Set if you wish to control the size of the backing-store file
# Read man page on fssnap_ufs but it suffices to say that maxsize=n[k,m,g] where n 
# is a number
# Be aware that if this file fills up, your snapshot will be invalid as it can no 
# longer track changes
# I choose not set it for this reason and the fact that my $SCRATCH_SPACE is 5 times
# the size of the DB.
BSTORE_MAXSIZE=""

# Set our PATH variable explicitly and export it to the script
PATH=/usr/sbin:/usr/bin:/usr/ucb
export PATH

#-----------------------NOTHING TO EDIT BELOW HERE------------------------

case $1 in
'start')
    echo "Starting Snapshot process at `date`"
    echo

    for i in $INIT_SCRIPTS_SHUTDOWN
    do
        echo "Stopping $i"
        if [ -x /etc/init.d/$i ]
        then
            /etc/init.d/$i stop
            echo "Shutdown of $i Successful"
        else
            echo "/etc/init.d/$i is not there or is not executable"
            echo "Shutdown of $i FAILED."
            exit 2
        fi
    echo
    done

    if [ ! -d $SCRATCH_SPACE ]
    then
        echo "Creating $SCRATCH_SPACE because it does not exist"
        mkdir -p $SCRATCH_SPACE
        echo
    fi

    for i in $MOUNT_POINTS
    do
        if [ -f $SCRATCH_SPACE$i-$BSTORE_EXT ]
        then
            echo "$SCRATCH_SPACE$i-$BSTORE_EXT exists....deleting"
            rm $SCRATCH_SPACE$i-$BSTORE_EXT 
            echo
        elif [ ! -d $i$MOUNT_POINT_EXT ]
        then
            echo "Creating $i$MOUNT_POINT_EXT to use with $FSSNAP"
            mkdir -p $i$MOUNT_POINT_EXT
            echo
        fi
    done

    echo "Processes should be OFF.  Check ps output below"
    ps -ef | egrep $PROCS_KEYWORDS

    sleep 10
    
    for i in $MOUNT_POINTS
    do
        echo "Using $FSSNAP to snapshot $i and mount to $i$MOUNT_POINT_EXT"
        if [ $BSTORE_MAXSIZE ]
        then
            echo "Backingstore file limited to $BSTORE_MAXSIZE to track filesystem changes"
            mount -F ufs -o ro `$FSSNAP -F ufs \
              -o maxsize=$BSTORE_MAXSIZE,backing-store=$SCRATCH_SPACE$i-$BSTORE_EXT \
              $i` $i$MOUNT_POINT_EXT
            $FSSNAP -i $i
            echo            
        else
            mount -F ufs -o ro `$FSSNAP -F ufs -o \
              backing-store=$SCRATCH_SPACE$i-$BSTORE_EXT $i` $i$MOUNT_POINT_EXT
            $FSSNAP -i $i
            echo
        fi
    done

    echo "Printing Disk Usage information"
    df -k
    echo

    for i in $INIT_SCRIPTS_STARTUP
    do
        echo "Starting $i"
        /etc/init.d/$i start
        echo
    done

    echo "Processes should be running.  Check ps output below"
    ps -ef | egrep $PROCS_KEYWORDS

    echo
    echo "Snapshot directories can now be backed up"
    echo "Finished creating and mounting snapshots at `date`"
    ;;

'stop')
    echo "Removing snapshot information and directories at `date`"
    echo
    for i in $MOUNT_POINTS
    do
        $FSSNAP -i $i
        umount $i$MOUNT_POINT_EXT
        echo
        $FSSNAP -d $i
        echo
        if [ -f $SCRATCH_SPACE$i-$BSTORE_EXT ]
        then
            echo "Removing backing-store file $SCRATCH_SPACE$i-$BSTORE_EXT"
            ls -la $SCRATCH_SPACE$i-$BSTORE_EXT 
            rm $SCRATCH_SPACE$i-$BSTORE_EXT 
            echo
        fi
    done

    echo
    echo "Cleanup complete.  File System output should not contain snapshots"
    echo
    df -k
    echo
    echo "Finished at `date`" 
    ;;

*)
        echo "usage: $0 {start|stop}"
    ;;
esac