
#!/bin/sh
# File	: cdot_snapmirror-volume_release-delete.sh
# By	: Maarten.deBoer@Atos.net, 211115
# Subject	: Script to release SnapMirror (if any) from offline volumes. And delete (if wanted, choice) the offline volume
#(0.2),240616	: From cdot_snapmirror-release_volume-delete.sh
PGM=`basename $0|cut -d\ -f1`
VER="0.2"
LOG="${HOME}/log/${PGM}.log"
TMP="/tmp/${PGM}.$$"
CLUSTERS="${HOME}/etc/clusters"
# Filters
CFILTER="nlnaf10[01]"
SFILTER="[*]?"
VFILTER="[*]?"
SSH="/usr/bin/ssh -n"
REMOVE_SM=""
DELETE_VOL=""

if [ "${1}" != "" ]; then
  CFILTER="${1}"
fi  # $1
if [ "${2}" != "" ]; then
  SFILTER="${2}"
fi  # $2
if [ "${3}" != "" ]; then
  VFILTER="${3}"
fi  # $2

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
echo "  CFILTER(1)=${CFILTER}"
echo "  SFILTER(2)=${SFILTER}"
echo "  VFILTER(3)=${VFILTER}"
echo "  REMOVE_SM=${REMOVE_SM}"
echo "  DELETE_VOL=${DELETE_VOL}"
sleep 1

# Check offline volumes 1st
cat ${CLUSTERS}|grep -v ^#|grep "${CFILTER}"|while read CLUSTER
do
  echo "  Checking offline volumes at ${CLUSTER} ..."
  ${SSH} ${CLUSTER} "set -showseparator \";\" ; volume show -state offline -field vserver,volume,state"|grep offline|grep -E "${SFILTER}"|grep -E "${VFILTER}"|while read LINE
  do
    VSERVER=`echo ${LINE}|awk -F\; '{print $1}'`
    VOLUME=`echo ${LINE}|awk -F\; '{print $2}'`
    echo "  VSERVER=${VSERVER} VOLUME=${VOLUME}"

    if [ "${VSERVER}" != "" ] && [ "${VOLUME}" != "" ]; then
# Check if SnapMirror relation.
# If so, then release (not empty)
      SNAPMIRROR_SRC=`${SSH} ${CLUSTER} "set -showseparator \";\" ; snapmirror show -vserver ${VSERVER} -source-volume ${VOLUME} -field vserver,source-volume"|grep "${VOLUME}"`
      echo "  SNAPMIRROR_SRC=${SNAPMIRROR_SRC}"
#      SNAPMIRROR_DEST=`${SSH} ${CLUSTER} "set -showseparator \";\" ; snapmirror list-destinations -source-vserver ${VSERVER} -source-volume ${VOLUME} -field source-vserver,source-volume"|grep "${VOLUME}"`
      SNAPMIRROR_DEST=`${SSH} ${CLUSTER} "set -showseparator \";\" ; snapmirror list-destinations -vserver ${VSERVER} -field source-vserver,source-volume"|grep "${VOLUME}"`
      echo "  SNAPMIRROR_DEST=${SNAPMIRROR_DEST}"
      if [ "${SNAPMIRROR_DEST}" != "" ]; then
        SM_SOURCE=`echo ${SNAPMIRROR_DEST}|awk -F\; '{print $1}'`
        SM_TYPE=`echo ${SNAPMIRROR_DEST}|awk -F\; '{print $2}'`
        SM_DEST=`echo ${SNAPMIRROR_DEST}|awk -F\; '{print $3}'`
        echo "  DEST: SM_SOURCE=${SM_SOURCE} SM_TYPE=${SM_TYPE} SM_DEST=${SM_DEST}"

        if [ ${REMOVE_SM}  ]; then
          echo "  Releasing SnapMirror ${SM_SOURCE} ${SM_TYPE} ${SM_DEST}"|tee -a ${LOG}
          ${SSH} ${CLUSTER} "volume online -vserver ${VSERVER} -volume ${VOLUME}"
          ${SSH} ${CLUSTER} "snapmirror release -destination-path ${SM_DEST}"
          ${SSH} ${CLUSTER} "volume offline -vserver ${VSERVER} -volume ${VOLUME}"
        fi #  REMOVE_SM
      fi  # SNAPMIRROR

      if [ ${DELETE_VOL} ]; then
        echo "  Deleting ${VSERVER}:${VOLUME}"|tee -a ${LOG}
        sleep 5
#        ${SSH} ${CLUSTER} "volume delete -vserver ${VSERVER} -volume ${VOLUME}"
      fi  # DELETE_VOL
    fi  # <> ""
  done  # LINE
done  # CLUSTER

echo "`date` ${PGM} v${VER} finished."|tee -a ${LOG}
exit 0

