#!/bin/sh
# disk mirroring for Linux. Run in /etc/cron.monthly.
# Jingchen Zhou, Feb. 9, 2006
#
# Mod:
#
# brobeck  01-Oct-2012
#   Made a change to use different account on testfac servers
#   to copy log file
# Brobeck  16-Jul-2009
#  -Adding logging feature and Maillist
#
#-----------------------------------------------------------------
#
#if [ `which whoami` != "root" ]
#  then 
#     echo "ERROR: $0 must be run as root"
#     exit 1
#fi
umask 0002
SLAC="@slac.stanford.edu"
MAILLIST="controls-system-admins$SLAC"
HOST=`uname -n`
cmdnm="$HOST-`basename $0`"

# Using $LCLS_DATA/sys/log/linuxdd
#
if [ -d /afs ] 
  then
    LOGDIR="/nfs/slac/g/lcls/sys/log/linuxdd"
  else
    LOGDIR="/u1/lcls/sys/log/linuxdd"
fi

dd if=/dev/sda of=/dev/sdb > /tmp/${cmdnm}.log 2>&1
if [ $? -ne 0 ]; then
        echo "$0 failed" >> /tmp/${cmdnm}.log
        cat /tmp/${cmdnm}.log | mailx -s "$cmdnm FAILURE" $MAILLIST
        exit 1
fi
echo "$0 successful" >> /tmp/${cmdnm}.log
tftest=`uname -n | awk -F- '{print $1}'`
if [ "$tftest" == "testfac" ]
  then
    su acctf -c "cp /tmp/${cmdnm}.log $LOGDIR/${cmdnm}.log"
  else
    su laci -c "cp /tmp/${cmdnm}.log $LOGDIR/${cmdnm}.log"
fi
cat $LOGDIR/${cmdnm}.log | mailx -s "$cmdnm SUCCESS" $MAILLIST
exit 0
