#!/bin/ksh
#######################################################################
# #
# This script backs up the operating system of selected AIX servers #
# and stores the backup images as new resources in NIM master.
# #
#######################################################################
# Gets the name of all NIM clients:
CLIENTLIST=`lsnim -t standalone | awk '{print $1}'`
#CLIENTLIST=tsm1
ERRORLOG=/var/log/nim/nim_clients_mksysb.log
#######################################
function backup_me_in_nim
# This function gets the nim client name and
# makes a mksysb backup resource in NIM master
#######################################
{
CLIENT=$1
echo "Mksysb started for $CLIENT. Please wait ..." >> $ERRORLOG
RESOURCE_NAME=mksysb_"$CLIENT"_`date +"%a%d%h%y"`
BACKUP_FILE=/export/images/$RESOURCE_NAME
if [[ -e $BACKUP_FILE ]]; then
echo "$RESOURCE_NAME already exists!" >> $ERRORLOG
else
nim -o define -t mksysb -a server=master -a
location=$BACKUP_FILE -a source=$CLIENT -a mk_image=yes -a mksysb_flags=XeA
$RESOURCE_NAME 2>> $ERRORLOG
# Checks whether mksysb is successful or not:
if [[ $? != 0 ]]; then
echo "Mksysb backup of $CLIENT failed." >> $ERRORLOG
else
echo "Backup finished successfully for $CLIENT." >>
$ERRORLOG
fi
fi
clean_old_backups $CLIENT
} # end of function
#######################################
function clean_old_backups
#######################################
{
CLIENT=$1
NUMBER_OF_BACKUPS_TO_KEEP=3 # Only this number of backups will be kept
MKSYSB_LIST=`ls -ltr /export/images/mksysb_"$CLIENT"* | awk '{ print $9}'`
COUNTER=`ls -ltr /export/images/mksysb_"$CLIENT"* | awk '{ print $9}' | wc
-l`
for file in $MKSYSB_LIST
do
RESOURCE_NAME=`echo $file | cut -f4 -d\/`
if [[ $COUNTER -gt $NUMBER_OF_BACKUPS_TO_KEEP ]]; then
if [[ `lsnim | grep $RESOURCE_NAME | wc -l` -eq 0 ]]; then
echo "$RESOURCE_NAME does not exist in NIM and cannot be
removed!" >> %ERRORLOG
else
nim -o remove $RESOURCE_NAME # Removes the resource from NIM
database
if [[ $? != 0 ]]; then
echo "$RESOURCE_NAME resource could not be removed from NIM
database!" >> $ERRORLOG
else
echo "$RESOURCE_NAME resource removed from NIM database
successfully." >> $ERRORLOG
fi
fi
rm -f $file
if [[ $? != 0 ]]; then
echo "$file could not be deleted from filesystem!" >> $ERRORLOG
else
echo "Old backup $file deleted." >> $ERRORLOG
fi
let "COUNTER=$COUNTER - 1"
fi
done
} # end of function
# start of execution:
echo ----------------------------------------------------------- >>
$ERRORLOG
echo "Mksysb backup by NIM beginning at `date`" >> $ERRORLOG
# This loop do the mksysb backup of AIX systems in NIM:
for nimclient in $CLIENTLIST
do
nim -Fo check $nimclient 2>> $ERRORLOG
if [[ $? != 0 ]]; then
echo "$nimclient cannot be reset. mksysb operation aborted." >>
$ERRORLOG
else
echo "$nimclient is healthy in NIM." >> $ERRORLOG
backup_me_in_nim $nimclient
fi
done
#backs up the nim server database:
/usr/lpp/bos.sysmgt/nim/methods/m_backup_db /etc/objrepos/nimdb.backup 2>>
$ERRORLOG
if [[ $? != 0 ]]; then
echo "NIM database backup failed." >> $ERRORLOG
else
echo "NIM database backup succeded." >> $ERRORLOG
fi
# Getting mksysb for NIM master
echo "Mksysb started for NIM master. Please wait ..." >> $ERRORLOG
nimclient=master
backup_me_in_nim $nimclient # backs up master OS
echo "End of mksysb backup `date` " >> $ERRORLOG
echo ----------------------------------------------------------- >>
$ERRORLOG