#!/bin/sh
#**************************************************************************
# Program:  Unix_OMRBackup.sh (Bourne shell)
#           Copyright (c) 2007, SAS Institute Inc.
#           Unpublished - All Rights Reserved 
# Author:   Enterprise Management Integration
# Created:  August, 2007
# Version:  Version 1.0
# History:  August, 2007:       Initial Implementation for Customer Use
#
# This shell script runs a SAS program that invokes the standard SAS
# %OMABAKUP macro to create a backup copy of the current SAS Metadata
# Server 'Foundation' repository. Additional host 'x' commands in the
# SAS program will also backup the adminUsers.txt, trustedUsers.txt, and
# omaconfig.xml files.
#
# NOTE:
# In its default configuration, the OMABAKUP macro will do the following:
#
# 1. Pause the repository manager, changing the access state to read-only 
#    so that repositories cannot be added or deleted.
#
# 2. Pause each metadata repository. The pause operation does the following:
#    a. retains all active client connections
#    b. temporarily changes the repository's access state to "offline"
#    c. temporarily stops receiving client requests to read or write
#       metadata.
#    d. flushes updated metadata from the server's memory and writes the
#       updated metadata to the metadata repository
#    e. closes the SAS datasets that serve as repository containers.
#
# 3. Uses PROC COPY to copy the metadata server's repository manager and
#    all of its registered metadata repositories to a path that you specify.
#
# 4. Creates control files that contain information about the backup.
#
# 5. Resumes the handling of client requests when the backup operation is
#    complete.
#
# For more details on %OMABAKUP, see:
#
#   SAS 9.1.3 Intelligence Platform - Systems Administration Guide
#   http://support.sas.com/documentation/configuration/bisag.pdf
#
# Each run of this script will generate a new subdirectory to store the
# contents of the backup data, based on the current date and time in 
# the form:
#
#      <backupdir>/OMRBackup_DDMMMYYYY_HH.MM.SS
#
# example:  /var/our_backups/OMRBackup_10AUG2007_20.31.16
#
# The name of the most recently-created backup directory is stored in:
#
#      <backupdir>/LastBackupDir.txt
#
# <backupdir> is passed into the script as its first and only argument ($1).
#
# Returns:  0    =    Successful Completion
#           1    =    Program Produced Warnings
#           2    =    Program Produced Errors
#           >2   =    Program Aborted with Severe Errors
#
#
# The script is designed to be run periodically by a Unix cron job. To avoid 
# permissions problems, the cron job should be established from the SAS 
# install user ID, typically "sas". That user ID will need write access
# to the designated backup directory.
#
# Note: some implementations of cron will email any output from scheduled
# jobs to the originating user ID. Messages directly sourced from this
# script are sent through a common 'logmsg' function. You can modify this
# function to route messages to a log file, to the syslog mechanism, or
# directly to /dev/null. See the definition of 'logmsg' below.
#
#**************************************************************************

#*****
# Site-dependent variables. Modify as needed to match your installation.
#*****

#*****
# BACKUPDIR
#
# Where the script will create a subdirectory to store the copy of the 
# SAS Metadata repository and configuration files. This is passed in 
# as the sole parameter to the script. 
# 
# The BACKUPDIR directory must be writable by the user ID that is running 
# this script, typically the SAS install ID ("sas").
#***** 
BACKUPDIR=$1

#*****
# SASCMD
#
# Where your executable copy of SAS 9.1 was installed.
#*****
SASCMD=<sas_install_path>/SAS/SAS_9.1/sas

#*****
# SOURCE
#
# Path to where the supplied Unix_OMRBackup.sas SAS program is stored. We 
# recommend that Unix_OMRBackup.sas be copied to the SAS BI server Utilities 
# install directory. That copy must be readable by the user ID that runs 
# this script.
#***** 
SOURCE=<sas_config_path>/Lev1/SASMain/Utilities/Unix_OMRBackup.sas

#*****
# BACKUPLOG
#
# Where the LOG output from the SAS backup program execution should be stored.
#*****
BACKUPLOG=$BACKUPDIR/OMRBackup.log

#*****
# BACKUPLST
#
# Where the print output from the SAS backup program execution should be stored.
#*****
BACKUPLST=$BACKUPDIR/OMRBackup.lst

#*****
# Typical Unix commands
#*****
ECHOCMD=/usr/bin/echo
DATECMD=/usr/bin/date
GREPCMD=/usr/bin/grep
WCCMD=/usr/bin/wc

#*****
# logmsg priority text text ...
#
# logmsg is used to issue informational and error messages related to
# the operations of this script. The default routine simply echos its
# argument(s) out to sysout. You can point this to a different
# file, change the routine to use the Unix "logger" command to write
# messages to the standard syslog, or simply direct its output to /dev/null.
#
# priority (first parameter):
#    NOTE - informational message
#    WARNING - something unexpected occurred, but script continues
#    ERROR - something nonrecoverable occurred, script terminated
#
# Messages are time-stamped.
#*****
logmsg()
{
    PRI=$1; shift
    $ECHOCMD `$DATECMD  +%Y%m%d:%H.%M.%S`':' "${PRI}:" "$*" 
}

#*****
#
# That is all that you should need to manually configure.
#
#*****









#######################################################################
# Did they specify a backup directory?
#######################################################################
if [ -z "$1" ]; then
{
  logmsg "ERROR" "$0, You must specify a backup directory as a parameter."
  exit 3
}
fi

########################################################################
# Execute the SAS Job to do the Metadata Backup and Check for Errors
########################################################################
if [ -w "$BACKUPDIR" ] ; then
   $SASCMD -work /usr/tmp -sysin $SOURCE -log $BACKUPLOG \
        -print $BACKUPLST -sysparm $1 ; ReturnCode=$?
   if [ `$GREPCMD "^NOTE: BACKUP of \".*\" Completed Successfully" \
        $BACKUPLOG | $WCCMD -l` -eq 0 -o "$ReturnCode" -ne 0 ] ; then
       exit $ReturnCode
   else
       exit 0
   fi
else
   logmsg "ERROR" "$0, You do not have write permissions to $BACKUPDIR"
   exit 3 
fi




