
#!/bin/ksh
set +x
#--------------------------------------------------------------------------#
#                                                                          #
# Name: rm_sm2tape_snaps.sh                                                #
#                                                                          #
# Description:  Script to remove old SM2Tape snapshots                     #
#                                                                          #
# Version: 2.1                                                             #
#                                                                          #
# References:                                                              #
#                                                                          #
# Documentation:                                                           #
#                                                                          #
# Parameters: none                                                         #
#                                                                          #
# Usage:  rm_sm2tape_snaps.sh                                              #
#                                                                          #
# Global Description:                                                      #
#                                                                          #
# Author(s): Atos Origin                                                   #
#                                                                          #
# Change log:                                                              #
# aut   date            vers    comments                                   #
# RL    06-01-2010      1.0     Initial version Richard Loos               #
# RL    16-06-2010      1.1     Update version Richard Loos                #
# AH    13-10-2010      2.0     Update: removed backup status              #
# AH    18-10-2010      2.0.1   Update: changed snap lag check             #
# RL    20-09-2011      2.1     Remove SM2Tape snapshots from vol0         #
#                                                                          #
#--------------------------------------------------------------------------#
#--------------------------------------------------------------------------#
# Initialize variabels                                                     #
#--------------------------------------------------------------------------#
LOGDIR=${HOME}/log
RUNDATE=$(date +%a)
RUNHOUR=$(date +%H)
LOGFILE=${LOGDIR}/rm_sm2tape_snaps_${RUNDATE}_${RUNHOUR}.log

#--------------------------------------------------------------------------#
# Create logfile and execute steps                                         #
#--------------------------------------------------------------------------#
>${LOGFILE}

for FILER in $(cat ${HOME}/etc/filers)
do
  #BACKUP_STATUS="`ssh ${FILER} backup status|grep \"No backup\"`"
  BACKUP_STATUS=ok
  #------------------------------------------------------------------------#
  # Start process when no backup is running                                #
  #------------------------------------------------------------------------#
  #if  [ "${BACKUP_STATUS}" == "No backup activity." ]
  if  [ "${BACKUP_STATUS}" == "ok" ]
  then
    echo "Collecting from filer ${FILER} ..." >>${LOGFILE}
    #----------------------------------------------------------------------#
    # Get SM2Tape relation with long LAGs                                  #
    #----------------------------------------------------------------------#
    #  update 2.01: SM2TAPE_LIST1="`ssh ${FILER} snapmirror status | grep : | grep Source | grep snapmirror_tape | grep -v ' 0[0-9]:' | grep -v ' 1[0-9]:' | grep -v ' 2[0-9]:' | grep -v ' 3[0-9]:' | awk '{print $1,$2}'`"
    SM2TAPE_LIST1="`ssh ${FILER} snapmirror status | grep : | grep Source | grep snapmirror_tape | grep -v ' 0[0-9]:' | grep -v ' 1[0-9]:' | awk '{print $1,$2}'`"
    SM2TAPE_LIST2=`echo "${SM2TAPE_LIST1}"| sed "s/ /?/g" | awk '{print $1}'`
    for SM2TAPE in ${SM2TAPE_LIST2}
    do
      #--------------------------------------------------------------------#
      # Release SM2Tape relation with LAG of more then 20 hours            #
      #--------------------------------------------------------------------#
      SM_REL1=`echo ${SM2TAPE}| cut -f2- -d":"`
      SM_REL2=`echo ${SM_REL1}| awk -F "?" '{print $1}'`
      SM_REL3=`echo ${SM_REL1}| awk  -F "?" '{print $2}'`
      SM_REL4="`ssh ${FILER} snapmirror destinations | grep \"${SM_REL2}\" | grep \"${SM_REL3}\"`"
      echo "ssh ${FILER} snapmirror release ${SM_REL4}" >>${LOGFILE}
      ssh ${FILER} snapmirror release ${SM_REL4}
    done
  else
    echo "Backup is running on filer ${FILER}, skip step" >>${LOGFILE}
  fi
done

