SAN Switch Backup

Example switch list:-

fcsw1-01
fcsw2-01
#!/bin/bash
#Written Andrew Stinger 08/09/2010
#Purpose is to backup SAN switch config
#This should be run from cron once per week, maybe less frequently.


#turn on debugging, -x starts, +x stops
set +x

DAY=`date +%F`

SWITCHUSER="admin"
SWITCHPASS="password"
SWITCHLOGIN="login:"
SWITCHPROMPT="admin>"
#BACKUPHOST="monitor01"
BACKUPHOST="172.27.206.210"
BACKUPUSER="backupsan"
BACKUPPASS="l4t1tud3"
BACKUPPATH="/home/backupsan/backupfiles"
SENDEMAIL=no

DATENUM=`date +%d`
#Test to force 1st of the month
#DATENUM=01
if [ $DATENUM == 01 ]
then
        MONTHTEST=" - 1st of the month test email."
        SENDEMAIL=yes
fi

EMAILSUBJECT="SAN Switch Backup${MONTHTEST}"



#e-mail distribution list
dist1="andrew.stringer@xxx.com"

TMPMAILFILE=/tmp/SANswitchBackup-$$
DEBUGLOG=/tmp/SANswitch-debug-$$

SANLIST=$1
if [ "$1" = "" ]
 then
   echo "You need to provide a server list file, eg. ./SAN-backup.sh sanswitchlist.txt."
   exit 1
fi


echo "To: ${dist1}" > ${TMPMAILFILE}
echo "From: backupsan@mon.2ld.net" >> ${TMPMAILFILE}
echo "Subject: SAN Switch Backup. " >> ${TMPMAILFILE}

echo "From /home/backupsan/SANswitchbackup.sh" >> ${TMPMAILFILE}
echo "Start of debug log file" > ${DEBUGLOG}

#loop through all switches
for SWITCH in `cat ${SANLIST}`;
do
        echo "-------------------------------------------"  >> ${DEBUGLOG}
        echo "Switch ${SWITCH}"  >> ${DEBUGLOG}
        echo "Switch ${SWITCH}"  >> ${TMPMAILFILE}
        export BACKUPFILE="${BACKUPPATH}/${SWITCH}-${DAY}"
        echo "Backup file is ${BACKUPFILE} - exported."  >> ${DEBUGLOG}

        #run expect to get backup file
        /usr/bin/expect SAN_config_upload.exp ${SWITCH} ${SWITCHUSER} ${SWITCHPASS} ${SWITCHLOGIN} \
        ${SWITCHPROMPT} ${BACKUPHOST} ${BACKUPUSER} ${BACKUPPASS} ${BACKUPFILE}
        #>> ${DEBUGLOG}
        RC=$?
        echo "\$RC is >${RC}<"

        if [ ${RC} -eq 0 ]
        then
                SUCCESS=true
        else
                SENDEMAIL=yes
        fi


        #echo "Pipestatus is >$PIPESTATUS<"

        echo "Expect return code is ${RC}" >> ${TMPMAILFILE}
        echo "Expect return code is ${RC}" >> ${DEBUGLOG}
        echo "-------------------------------------------"  >> ${TMPMAILFILE}
        echo "-------------------------------------------"  >> ${DEBUGLOG}
        echo " " >> ${TMPMAILFILE}
        echo " " >> ${DEBUGLOG}
done

echo "All switches done"  >> ${TMPMAILFILE}
echo " "  >> ${TMPMAILFILE}



#Housekeeping, delete files older than 4 weeks
echo "Housekeeping." >> ${TMPMAILFILE}
echo "-------------" >> ${TMPMAILFILE}

#How old do the files need to be?
AGE=28

NUMFILES='zero'
NUMFILES=`find ${BACKUPPATH}/ -mtime +${AGE} | wc -l`

echo " "
echo "Deleting these files (${NUMFILES}) as they are older than 4 weeks:-" >> ${TMPMAILFILE}


#Do the deed!
find ${BACKUPPATH} -mtime +${AGE} -exec ls -l {} \; >> ${TMPMAILFILE}
find ${BACKUPPATH} -mtime +${AGE} -exec rm -f {} \;


echo " " >> ${TMPMAILFILE}
echo "SAN switch backup report." >> ${TMPMAILFILE}
echo "-------------------------" >> ${TMPMAILFILE}
echo " " >> ${TMPMAILFILE}
echo " " >> ${TMPMAILFILE}
echo "Current backup files in ${BACKUPPATH}:-" >> ${TMPMAILFILE}
echo " " >> ${TMPMAILFILE}

ls -1rt ${BACKUPPATH}  >>${TMPMAILFILE}


#Mail Out
if [  ${SENDEMAIL} == yes ]
then
        echo "Sending notification mail at `date +%H:%M:%S` UTC." >> ${TMPMAILFILE}
        echo " "  >> ${TMPMAILFILE}
        echo "Return Codes:-"  >> ${TMPMAILFILE}
        echo " "  >> ${TMPMAILFILE}
        echo "RC=0, all ok."  >> ${TMPMAILFILE}
        echo "RC=1, expect failed to log in to the FC switch."  >> ${TMPMAILFILE}
        echo "RC=2, switch failed to log in to the backup server over ssh." >> ${TMPMAILFILE}
        echo "RC=3, switch reported it didn't complete backup." >> ${TMPMAILFILE}
        echo "RC=130, expect was killed with ctrl-c." >> ${TMPMAILFILE}

        echo " " >> ${TMPMAILFILE}
        /bin/mail ${dist1} -s "${EMAILSUBJECT}" < ${TMPMAILFILE}
else
        #don't send mail, so mail command is commented out. Left in for testing
        echo "Sending notification mail at `date +%H:%M:%S` UTC." >> ${TMPMAILFILE}
        #/bin/mail ${dist1} -s "${EMAILSUBJECT} - Test!" < ${TMPMAILFILE}
fi

#/usr/sbin/sendmail -t < ${TMPMAILFILE}

rm ${TMPMAILFILE}

#Comment if you want to see debug messages.
#rm ${DEBUGLOG}

exit 0

rb/sanswitchbackup.txt · Last modified: 03/10/2014 09:01 by andrew