
#!/bin/ksh
set +x
#--------------------------------------------------------------------------#
# Name: backup_vol0_etc.sh                                                 #
# Description:  Script to copy /vol/vol0/etc from (v)filers                #
# References:                                                              #
# Documentation:                                                           #
# Parameters: none                                                         #
# Usage:  backup_vol0_etc.sh                                               #
# Global Description:                                                      #
# Author(s): Atos                                                          #
# Change log:                                                              #
# aut   date            vers    comments                                   #
# AH    04-01-2008      1.0     Initial version Andre Hilgersom            #
# RL    22-10-2008      1.1     Update for (v)filers Richard Loos          #
# RL    08-09-2010      1.2     Modification for (v)filers with different  #
#                               root-volume                                #
# RL    18-10-2011      1.3     New backup filers nlnaf55 and nlnaf62      #
# MdB	25-01-2016	1.4	Add -exclude to exclude files like rstabRENAME*, stats_archive_data*
# DST_FILER="nlnaf56"
# MdB	15-nov-2018	2.0	Changed source & dest-filer
# MdB   11-apr-2019	2.1	Mod's to 2.0
#                                                                          #
#--------------------------------------------------------------------------#
# Initialize variabels                                                     #
#--------------------------------------------------------------------------#
PGM="`basename $0|cut -d\. -f1`"
VER="2.1"
LOGDIR=${HOME}/log
FILE=${LOGDIR}/backup_vol0_etc.log
BACKUP_VOLUME="na_vol0_backups"
# Separated with ,s NO spaces
EXCLUDES="\"rstabRENAME*\",\"stats_archive_data*\" "
SRC_DEST_FILE="${HOME}/etc/filers_source-location-dest"

#--------------------------------------------------------------------------#
# Destination (DST_FILER) and new destination filer (NDST_FILER)           #
#--------------------------------------------------------------------------#
if [ ! -f ${SRC_DEST_FILE} ]; then
  echo "  NO Source-location-dest file (${SRC_DEST_FILE}) file found. Exiting ..."|tee -a ${FILE}
  exit 2
fi
# Default values
DST_FILER="nlnaf84"
NDST_FILER="nlnaf85"

#--------------------------------------------------------------------------#
# Create FILE                                                              #
#--------------------------------------------------------------------------#
cp ${FILE} ${FILE}.tmp
tail -n300 ${FILE}.tmp > ${FILE}
rm ${FILE}.tmp

echo "`date`: ${PGM} v${VER} start."|tee -a ${FILE}

#--------------------------------------------------------------------------#
# Start ndmpcopy of /vol0/etc to destination filer                         # 
#--------------------------------------------------------------------------#
for FILER in $(cat ${HOME}/etc/filers)
do
# Search with FILER for the dest-filer in file ${SRC_DEST_FILE}
  NDST_FILER=`grep ${FILER} ${SRC_DEST_FILE}|awk -F\; '{print $3}'`
  SRC_LOCATION=`grep ${FILER} ${SRC_DEST_FILE}|awk -F\; '{print $2}'`
# echo "FILER=${FILER} NDST_FILER=${NDST_FILER} ..."
  if [ "${NDST_FILER}" = "" ]; then
    NDST_FILER=${DST_FILER}
  fi
  if [ "${FILER}" = "${DST_FILER}" ]
  then
    export NDST_FILER=${NDST_FILER}
  else
    export NDST_FILER=${DST_FILER}
  fi
  DEST_LOCATION=`grep ^${DST_FILER} ${SRC_DEST_FILE}|awk -F\; '{print $2}'|head -1`
echo "FILER=${FILER} (SRC_LOCATION=${SRC_LOCATION}) DST_FILER=${DST_FILER} (DEST_LOCATION=${DEST_LOCATION}) NDST_FILER=${NDST_FILER} ..."
sleep 2
  #------------------------------------------------------------------------#
  # Request for password of user ndmpd_user of source (PWD_SRC)            #
  # and destination filer (PWD_DST)                                        # 
  #------------------------------------------------------------------------#
  PWD_SRC=`ssh ${FILER} ndmpd password ndmpd_user | awk '{print $2}'`
  PWD_DST=`ssh ${NDST_FILER} ndmpd password ndmpd_user | awk '{print $2}'`
  if [[ "${PWD_SRC}" = "such" || "${PWD_DST}" = "such" ]]
  then
    #----------------------------------------------------------------------#
    # If no user ndmpd_user, do:                                           #
    # filer> useradmin user add ndmpd_user -g "Backup Operators" -m xxx    #
    #----------------------------------------------------------------------#
    echo "No user ndmpd_user on filers ${FILER} and/or ${NDST_FILER}, please create one"
  else
    #----------------------------------------------------------------------#
    # If destination qtree doesn't exist, create one                       #
    #----------------------------------------------------------------------#
    DIR_DST=`ssh ${NDST_FILER} qtree status | grep "${BACKUP_VOLUME}" | grep "${FILER}_etc" ` 
    if [ -z "${DIR_DST}" ]
    then
      echo "`date`: Create /vol/${BACKUP_VOLUME}/${FILER}_etc on ${NDST_FILER}"|tee -a ${FILE}
      ssh ${NDST_FILER} qtree create /vol/${BACKUP_VOLUME}/${FILER}_etc 
      sleep 30
    fi
    #----------------------------------------------------------------------#
    # Get ROOTVOL volume name (this can be different then to vol0)         #
    #----------------------------------------------------------------------#
    ROOTVOL="`ssh -n ${FILER} vol status | grep ' root'| awk '{print $1}'`"

    #----------------------------------------------------------------------#
    # Ndmpcopy of ROOTVOL (/vol0/etc) of filers to destination filer       # 
    #----------------------------------------------------------------------#
    echo "`date`: ssh ${FILER} ndmpcopy -sa ndmpd_user:${PWD_SRC} -da ndmpd_user:${PWD_DST} -exclude "${EXCLUDES}" ${FILER}-sm:/vol/${ROOTVOL}/etc ${NDST_FILER}-sm:/vol/${BACKUP_VOLUME}/${FILER}_etc"|tee -a ${FILE}
    ssh ${FILER} ndmpcopy -sa ndmpd_user:${PWD_SRC} -da ndmpd_user:${PWD_DST} -exclude "${EXCLUDES}" ${FILER}-sm:/vol/${ROOTVOL}/etc ${NDST_FILER}-sm:/vol/${BACKUP_VOLUME}/${FILER}_etc >>${FILE} 2>&1
#    sudo rm -rf /filers/${NDST_FILER}/${BACKUP_VOLUME}/${FILER}_etc/stats/archive/*
  fi
  for VFILER in `ssh ${FILER} vfiler status | grep -v vfiler0 | grep running | awk '{print $1}'`
  do
    #----------------------------------------------------------------------#
    # Start ndmpcopy when password of user ndmpd_user is filled            #
    # of source (PWD_SRC) and destination filer (PWD_DST)                  # 
    #----------------------------------------------------------------------#
    #----------------------------------------------------------------------#
    # Get ETC (vol0) of vfilers. Be found in `vfiler status -r <vfiler>`   #
    #----------------------------------------------------------------------#
    VROOTVOL="`ssh ${FILER} vfiler status -r ${VFILER} | grep etc | cut -d: -f2 | cut -d[ -f1|sed -e 's/ //g'`"

    #----------------------------------------------------------------------#
    # If nu "such" user, then not start                                    #
    #----------------------------------------------------------------------#
    if [[ "${PWD_SRC}" != "such" || "${PWD_DST}" != "such" ]]
    then 
      #--------------------------------------------------------------------#
      # If destination qtree doesn't exist, create one                     #
      #--------------------------------------------------------------------#
      DIR_DST=`ssh ${NDST_FILER} qtree status | grep "${BACKUP_VOLUME}" | grep "${VFILER}_etc" `
      if [ -z "${DIR_DST}" ]
      then
        echo "`date`: Create /vol/${BACKUP_VOLUME}/${VFILER}_etc on ${NDST_FILER}"|tee -a ${FILE}
        ssh ${NDST_FILER} qtree create /vol/${BACKUP_VOLUME}/${VFILER}_etc 
        sleep 30
      fi
      #--------------------------------------------------------------------#
      # Ndmpcopy of /vol/vol0_vfiler<customercode>/etc to destination filer# 
      #--------------------------------------------------------------------#
      echo "`date`: ssh ${FILER} ndmpcopy -sa ndmpd_user:${PWD_SRC} -da ndmpd_user:${PWD_DST} -exclude "${EXCLUDES}" ${FILER}-sm:${VROOTVOL}/etc ${NDST_FILER}-sm:/vol/${BACKUP_VOLUME}/${VFILER}_etc"|tee -a ${FILE}
      ssh ${FILER} ndmpcopy -sa ndmpd_user:${PWD_SRC} -da ndmpd_user:${PWD_DST} -exclude "${EXCLUDES}" ${FILER}-sm:${VROOTVOL}/etc ${NDST_FILER}-sm:/vol/${BACKUP_VOLUME}/${VFILER}_etc >>${FILE} 2>&1
#      sudo rm -rf /filers/${NDST_FILER}/${BACKUP_VOLUME}/${FILER}_etc/stats/archive/*
    fi
  done
done

echo "`date`: ${PGM} finished"|tee -a ${FILE}
exit 0

