
#!/bin/sh
# File	: noc_vserver-dr_create.sh
# By	: Maarten.deBoer@Atos.net, 240130
# Subject	: Netapp Ontap Cli script for creating vserver/SVM-DR
#(0.2),240205	: Added FILTER
#(0.3),240205	: Check 1st
PGM=`basename $0|cut -d\. -f1`
VER="0.3"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"

DATAINDIR="${HOME}/data/in"
FILTER="[?]*"
FILTER="mss"

# VARs
CLUSTER="nlnaf199"
DR_CLUSTER="nlnaf199"
VSERVER="nlnafsmss99"
VSERVER_DR="nlnafsmss99-dr"
IP_SPACE="mss99-ipspace"

if [ "${1}" != "" ]; then
  FILTER="${1}"
fi
if [ "${2}" != "" ]; then
  DATAINDIR="${2}"
fi
echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
echo "  FILTER(1)=${FILTER}"
echo "  DATAINDIR(2)=${DATAINDIR}"
sleep 1

if [ ! -d ${DATAINDIR} ]; then
  echo "    NO DATAINDIR (${DATAINDIR}) found. Exiting ..."|tee -a ${LOG}
  exit 3
fi  # INDIR

cd ${DATAINDIR}
ls -1 *.rc |grep "${FILTER}"|while read RCFILE
do
  echo "  RCFILE=${RCFILE}"
  if [ -f ${RCFILE} ]; then
    echo "    Using ${RCFILE} "|tee -a ${LOG}
    . ./${RCFILE}
  fi  # RCFILE

  echo "    CLUSTER=${CLUSTER}"
  echo "    DR_CLUSTER=${DR_CLUSTER}"
  echo "    VSERVER=${VSERVER}"
  echo "    VSERVER_DR=${VSERVER_DR}"
  echo "    IP_SPACE=${IP_SPACE}"
  sleep 1

# Check Dest-IPspace
  if [ "${DR_CLUSTER}" != "" ]; then
    echo ""
    echo "--- Checking Dest. IPspace @${DR_CLUSTER}"
    ${SSH} ${DR_CLUSTER} "network ipspace show -ipspace ${IP_SPACE} " 1>/dev/null 2>&1
    EC=${?}
    if [ ${EC} -ne 0 ]; then
      echo "    Creating IP_SPACE ${IP_SPACE} @${DR_CLUSTER}" |tee -a ${LOG}
      ${SSH} ${DR_CLUSTER} "network ipspace create -ipspace ${IP_SPACE}"
    fi  # EC <> 0
    ${SSH} ${DR_CLUSTER} "network ipspace show -ipspace ${IP_SPACE} "

#    BRO_DOM=`${SSH} ${CLUSTER} "ipspace show -ipspace ${IP_SPACE} -field broadcast"|grep "${IP_SPACE}"|awk '{print $2}'`
#    MTU=`${SSH} ${CLUSTER} "broadcast show -broadcast ${BRO_DOM} -field mtu"|grep "${BRO_DOM}"|awk '{print $3}'`
#    echo "BRO_DOM=${BRO_DOM} MTU=${MTU}"

    echo "--- Checking Dest. vserver ${VSERVER_DR}"
    ${SSH} ${DR_CLUSTER} "vserver show -vserver ${VSERVER_DR}" 1>/dev/null 2>&1
    EC=${?}
    if [ ${EC} -ne 0 ]; then
      echo "    Creating DR-vserver ${DR_CLUSTER}:${VSERVER_DR}"|tee -a ${LOG}
      ${SSH} ${DR_CLUSTER} "vserver create -vserver ${VSERVER_DR} -subtype dp-destination -ipspace ${IP_SPACE}  "
    fi  # EC <> 0
    ${SSH} ${DR_CLUSTER} "vserver show -vserver ${VSERVER_DR} -field vserver,operational-state,ipspace"

# Create a peer connection between source vserver and new DR vserver:
    echo "--- Checking peer from ${VSERVER} to ${VSERVER_DR}"
    ${SSH} ${DR_CLUSTER} "vserver peer show -vserver ${VSERVER_DR} " 1>/dev/null 2>&1
    EC=${?}
    if [ ${EC} -ne 0 ]; then
      echo "    Creating Peers between ${VSERVER} and ${VSERVER_DR}"|tee -a ${LOG}
      ${SSH} ${DR_CLUSTER} "vserver peer create -vserver ${VSERVER_DR} -peer-vserver ${VSERVER} -applications snapmirror -peer-cluster ${CLUSTER} "
      EC=${?}
      if [ ${EC} -eq 0 ]; then
        sleep 10
      fi
      ${SSH} ${CLUSTER} "vserver peer accept -vserver ${VSERVER} -peer-vserver ${VSERVER_DR} "
      ${SSH} ${CLUSTER} "vserver peer show  -vserver ${VSERVER}"
    fi  # EC <> 0
    ${SSH} ${DR_CLUSTER} "vserver peer show  -vserver ${VSERVER_DR}"

    echo "--- Checking SnapMirror from ${VSERVER} to ${VSERVER_DR}"
    ${SSH} ${DR_CLUSTER} "snapmirror show -destination-path ${VSERVER_DR}: -field vserver" 1>/dev/null 2>&1
    EC=${?}
    if [ ${EC} -ne 0 ]; then
# Create snapmirror relation between source vserver and DR vserver configuration setting “-identity-preserve true”:
      echo "    Creating SnapMirror between source vserver and DR vserver"|tee -a ${LOG}
      echo "${DR_CLUSTER}::>snapmirror create -destination-path ${VSERVER_DR}: –source-path ${VSERVER}: -vserver ${VSERVER_DR} -throttle unlimited -schedule hourly -policy MirrorAllSnapshots -identity-preserve true "

      ${SSH} ${DR_CLUSTER} "snapmirror create -destination-path ${VSERVER_DR}: –source-path ${VSERVER}: -vserver ${VSERVER_DR} -throttle unlimited -schedule hourly -policy MirrorAllSnapshots -identity-preserve true "

# snapmirror create -destination-path nlnafsmss13-dr: -source-path nlnafsmss13: -vserver nlnafsmss13-dr -throttle unlimited -schedule hourly -policy MirrorAllSnapshots -identity-preserve true
# nlnaf102::> snapmirror create -destination-path nlnafsmss13-dr: -source-path nlnafsmss13: -type XDP -schedule hourly -policy MirrorAllSnapshots -identity-preserve true
# cluster_dst::> snapmirror create -source-path svm1: -destination-path svm_backup: -type XDP -schedule my_daily -policy MirrorAllSnapshots -identity-preserve true
    fi  # EC <> 0

    echo "--- Checking SnapMirror status ${VSERVER} to ${VSERVER_DR}"
    ${SSH} ${DR_CLUSTER} "snapmirror show -destination-path ${VSERVER_DR}: -state Uninitialized -status Idle" 1>/dev/null 2>&1
    EC=${?}
    if [ ${EC} -eq 0 ]; then
      echo "    Initializing SnapMirror between source vserver and DR-vserver ${VSERVER_DR}"|tee -a ${LOG}
#      echo "${DR_CLUSTER}::> snapmirror initialize -destination-path ${VSERVER_DR}:"
      ${SSH} ${DR_CLUSTER} "snapmirror initialize -destination-path ${VSERVER_DR}:"
    fi  # EC <> 0
    ${SSH} ${DR_CLUSTER} "snapmirror show -destination-path ${VSERVER_DR}: -field vserver,state,status"

    echo "--- Volumes in vserver-dr ${VSERVER_DR}:"
    VOL_PREFIX=`echo ${VSERVER}|sed 's/nlnafs//g'`
    ${SSH} ${DR_CLUSTER} "snapmirror show -destination-volume ${VOL_PREFIX}* "

#    ${SSH} ${DR_CLUSTER} "network interface show -vserver ${VSERVER_DR} -field vserver,curr-port,curr-node"
  fi  # DR_CLUSTER

done  # FNAME


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

