
#!/bin/sh
# File	: noc_volume_snapmirror_configure.sh
# By	: Maarten.deBoer@atos.net, 260922
# Subject	: Script to create,modify (configure) SnapMirrors
PGM=`basename $0|cut -d\. -f1`
VER="0.1"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"
RM_VOL_DEST=""

FILTER="tnl01"

CLUSTER_SRC="aff8080"
CLUSTER_DEST="fas8040"
AGGR_DEST="n01a0901"
VSERVER_SRC="tlenltnl01"
VSERVER_DEST="tlenltnl01b"

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

${SSH} ${CLUSTER_SRC} "set -unit GB;volume show -vserver ${VSERVER_SRC} -field volume,size"|grep -v vol0root|grep "${FILTER}"|while read LINE
do
#  echo ${LINE}

  VOLUME=`echo ${LINE}|awk '{print $2}'`
  SIZE_GB=`echo ${LINE}|awk '{print $3}'|sed 's/GB//g'`

  DEST_VOLUME=`${SSH} ${CLUSTER_DEST} "volume show -vserver ${VSERVER_DEST} -volume ${VOLUME} -field volume"|grep ${VSERVER_DEST}|awk '{print $2}'`

  echo "  VOLUME=${VOLUME} DEST_VOLUME=${DEST_VOLUME} SIZE_GB=${SIZE_GB}"

# new dest volume   
  if [ "${VOLUME}" != "${DEST_VOLUME}" ]; then
    echo "    Creating ${VOLUME} ${SIZE_GB}GB at ${CLUSTER_DEST} "|tee -a ${LOG}
    ${SSH} ${CLUSTER_DEST} "volume create -vserver ${VSERVER_DEST} -volume ${VOLUME} -size ${SIZE_GB}GB -aggregate ${AGGR_DEST} -type DP"
  else
    if [ ${RM_VOL_DEST} ]; then
      echo "    Removing ${VOLUME} at ${CLUSTER_DEST} "|tee -a ${LOG}
      ${SSH} ${CLUSTER_DEST} "volume unmount -vserver ${VSERVER_DEST} -volume ${VOLUME}"
      sleep 2
      ${SSH} ${CLUSTER_DEST} "volume offline -vserver ${VSERVER_DEST} -volume ${VOLUME}"
      sleep 2
      ${SSH} ${CLUSTER_DEST} "volume delete -vserver ${VSERVER_DEST} -volume ${VOLUME}"
    fi  # RM_VOL_DEST
  fi  # create VOLUME

  SNAPMIRROR_SHOW=`${SSH} ${CLUSTER_DEST} "snapmirror show -source-path ${VSERVER_SRC}:${VOLUME} -destination-path ${VSERVER_DEST}:${VOLUME} -field destination-path,state"|grep ${VOLUME} `
  echo "SNAPMIRROR_SHOW=${SNAPMIRROR_SHOW}|"
#source-path               destination-path           state
#------------------------- -------------------------- ----------
#tlenltnl01:tnl01_vol02115 tlenltnl01b:tnl01_vol02115 Broken-off
  if [ "${SNAPMIRROR_SHOW}" == "" ]; then
    echo "    SnapMirror ${VSERVER_SRC}:${VOLUME} to ${VSERVER_DEST}:${VOLUME} will be created"|tee -a ${LOG}
    ${SSH} ${CLUSTER_DEST} "snapmirror create -source-path ${VSERVER_SRC}:${VOLUME} -destination-path ${VSERVER_DEST}:${VOLUME} -type XDP -schedule hourly"
    ${SSH} ${CLUSTER_DEST} "snapmirror initialize -source-path ${VSERVER_SRC}:${VOLUME} -destination-path ${VSERVER_DEST}:${VOLUME}"

  fi  # SNAPMIRROR_SHOW

# Modify
  ${SSH} ${CLUSTER_DEST} "snapmirror modify -source-path ${VSERVER_SRC}:${VOLUME} -destination-path ${VSERVER_DEST}:${VOLUME} -schedule hourly"

  sleep 1
  ${SSH} ${CLUSTER_DEST} "snapmirror show -source-path ${VSERVER_SRC}:${VOLUME} -destination-path ${VSERVER_DEST}:${VOLUME} -field destination-path,state,status,schedule"


done  # LINE


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

