
#!/bin/ksh
set +x
#--------------------------------------------------------------------------#
#                                                                          #
# Name: restore_file_etc.sh                                                #
#                                                                          #
# Description:  Script to restore file to /vol/vol0_vfiler*****/etc        #
#                                                                          #
# Version: 1.2                                                             #
#                                                                          #
# References:                                                              #
#                                                                          #
# Documentation:                                                           #
#                                                                          #
# Parameters: <file>                                                       #
#                                                                          #
# Usage:  restore_file_etc.sh <file>                                       #
#                                                                          #
# Global Description:                                                      #
#                                                                          #
# Author(s): Atos                                                          #
#                                                                          #
# Change log:                                                              #
# aut   date            vers    comments                                   #
# RL    12-03-2009      1.0     Initial version Richard Loos               #
# RL    04-07-2011      1.1     Update                                     #
# RL    08-04-2013      1.2     Modification for data transfer via SM or   #
#                               MGT LAN                                    #
#                                                                          #
#--------------------------------------------------------------------------#
#--------------------------------------------------------------------------#
# Initialize variabels                                                     #
#--------------------------------------------------------------------------#
VER="1.2" 
LOGDIR=${HOME}/log
FILE=${LOGDIR}/restore_file_etc.log
VOLUME="na_vol0_backups"
DIR_SRC=`pwd`

#--------------------------------------------------------------------------#
# Read parameter                                                           #
#--------------------------------------------------------------------------#
OFILE=${1}
USAGE="usage: restore_file_etc.sh <file>"
if [ -z "${OFILE}" ]
then
  echo "${USAGE}"
  exit 1
fi

if [ ! -f "${DIR_SRC}/${OFILE}" ]
then
  echo "File ${DIR_SRC}/${OFILE} doesn't exist" 
  exit 1
fi

#--------------------------------------------------------------------------#
# Define source (SRC_FILER) and destination filer (DST_FILER)              #
#--------------------------------------------------------------------------#
SRC_FILER=`echo "${DIR_SRC}" | awk -F "/" '{print $3}'`
VFILER=`echo "${DIR_SRC}" | awk -F "/" '{print $5}'|cut -d_ -f1`

for FILER in $(cat ${HOME}/etc/filers_ns)
do
  DFILER=`ssh ${FILER} vfiler status | grep -v "DR" | grep "${VFILER}"`
  if [ -n "${DFILER}" ]
  then
    DST_FILER=${FILER}
  fi
done

#--------------------------------------------------------------------------#
# Check if SM LAN is defined in /etc/rc file.                              #
# If not use MGT LAN for transfer.                                         #
#--------------------------------------------------------------------------#
DST_FILER_SM=`ssh ${DST_FILER} rdfile /etc/rc | grep -v "#" | grep "\-sm"`
if [ "${DST_FILER_SM}" = "" ]
then
  SRC_FILER_SM="${SRC_FILER}"
  DST_FILER_SM="${DST_FILER}"
else
  SRC_FILER_SM="${SRC_FILER}-sm"
  DST_FILER_SM="${DST_FILER}-sm"
fi

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

#--------------------------------------------------------------------------#
# Request for password of user ndmpd_user of source (PWD_SRC)              #
# and destination filer (PWD_DST)                                          # 
#--------------------------------------------------------------------------#
PWD_SRC=`ssh ${SRC_FILER} ndmpd password ndmpd_user | awk '{print $2}'`
PWD_DST=`ssh ${DST_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 ${SRC_FILER} and/or ${DST_FILER}, please create one"
fi
#--------------------------------------------------------------------------#
# Start ndmpcopy when password of user ndmpd_user is filled                #
# of source (PWD_SRC) and destination filer (PWD_DST)                      # 
#--------------------------------------------------------------------------#
CUSTOMERCODE=`echo ${VFILER} | cut -c7-`
if [[ "${PWD_SRC}" != "such" || "${PWD_DST}" != "such" ]]
then 
  #------------------------------------------------------------------------#
  # Ndmpcopy of /vol0_vfiler<customercode>/etc/<file> to /etc/old/<file>   # 
  #------------------------------------------------------------------------#
  echo "ssh ${DST_FILER} ndmpcopy -sa ndmpd_user:${PWD_DST} -da ndmpd_user:${PWD_DST} ${DST_FILER_SM}:/vol/vol0_vfiler${CUSTOMERCODE}/etc/${OFILE} ${DST_FILER_SM}:/vol/vol0_vfiler${CUSTOMERCODE}/etc/old"
  ssh ${DST_FILER} ndmpcopy -sa ndmpd_user:${PWD_DST} -da ndmpd_user:${PWD_DST} ${DST_FILER_SM}:/vol/vol0_vfiler${CUSTOMERCODE}/etc/${OFILE} ${DST_FILER_SM}:/vol/vol0_vfiler${CUSTOMERCODE}/etc/old >>${FILE} 2>&1

  #------------------------------------------------------------------------#
  # Ndmpcopy of /na_vol0_backups/${VFILER}_etc/<file> to destination filer # 
  #------------------------------------------------------------------------#
  echo "ssh ${SRC_FILER} ndmpcopy -sa ndmpd_user:${PWD_SRC} -da ndmpd_user:${PWD_DST} ${SRC_FILER_SM}:/vol/na_vol0_backups/${VFILER}_etc/${OFILE} ${DST_FILER_SM}:/vol/vol0_vfiler${CUSTOMERCODE}/etc"
  ssh ${SRC_FILER} ndmpcopy -sa ndmpd_user:${PWD_SRC} -da ndmpd_user:${PWD_DST} ${SRC_FILER_SM}:/vol/na_vol0_backups/${VFILER}_etc/${OFILE} ${DST_FILER_SM}:/vol/vol0_vfiler${CUSTOMERCODE}/etc >>${FILE} 2>&1
fi

