
#!/bin/sh
# File	:  set_nfs_ro.sh
# By	: Maarten.deBoer@Atos.net, 191014
# Subject	: Script to set a NFS-export RO
PGM=`basename $0|cut -d\. -f1`
VER="0.1"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}.log"
ETC="${HOME}/etc/${PGM}.exports"
SSH="/bin/ssh -n"
DATADIR="${HOME}/data/${PGM}"
DATI=`date +%Y%d%m-%H%M%S`

if [ ! -f ${ETC} ]; then
  echo "  Input-file (${ETC}) NOT found. Exiting..."
  exit 2
fi
if [ ! -d ${DATADIR} ]; then
  echo "  DATA-dir (${DATADIR}) not available. Please create. Exiting..."
  exit 3
fi

echo "  Saving exports data first (into data/${PGM}/<filer>_${DATI}) ..."
cat ${ETC}|grep -v ^#|awk '{print $1}'|sort -u|while read FILER
do
  echo "  ${FILER}"
  ${SSH} ${FILER} "exportfs" > ${DATADIR}/${FILER}_${DATI}.exports-old
done  # cat ETC

touch ${TMP}
cat ${ETC}|grep -v ^#|while read LINE
do
  FILER=`echo ${LINE}|awk '{print $1}'`
  VFILER=`echo ${LINE}|awk '{print $2}'`
  EXPORT=`echo ${LINE}|awk '{print $3}'`
  echo "|${FILER}|${VFILER}|${EXPORT}|"
# Save data WITHOUT line (to be changed)
  cat ${DATADIR}/${FILER}_${DATI}.exports-old|grep -v "${EXPORT}" >> ${DATADIR}/${FILER}_${DATI}.exports-new
# Save now with line to be changed
  cat ${DATADIR}/${FILER}_${DATI}.exports-old|grep "${EXPORT}"|sed 's/rw=/ro=/g' >> ${DATADIR}/${FILER}_${DATI}.exports-new
done  # LINE

echo "  Setting exports to RO ..."
cat ${ETC}|grep -v ^#|awk '{print $1}'|sort -u|while read FILER
do
  echo "  ${FILER}"
  cat ${DATADIR}/${FILER}_${DATI}.exports-new|${SSH} ${FILER} "wrfile /etc/exports"
  ${SSH} ${FILER} "exportfs -a"
done  # cat ETC

echo "  Checking LIVE  ..."
cat ${ETC}|grep -v ^#|while read LINE
do
  FILER=`echo ${LINE}|awk '{print $1}'`
  VFILER=`echo ${LINE}|awk '{print $2}'`
  EXPORT=`echo ${LINE}|awk '{print $3}'`
  echo "|${FILER}|${VFILER}|${EXPORT}|"

  ${SSH} ${FILER} "exportfs"|grep ${EXPORT}

done  # cat ETC



exit 0

