#!/bin/sh
$SMSBIN/smsinit $LOADL_STEP_ID

#############################################################
# Author : David Michaud
# Purpose: Script to backup /nwprod/ to /scratch1/nwprod
#          every Sunday
# History: Mar 20, 2000 - First Implemented into production
#          Sep 28, 2000 - Split nwprod.tar file into 1 Gb
#                         segments so that the resulting
#                         files will fit on the backup lnx177
#                         machine which doesn't allow files
#                         larger than 2 Gb.
#    MAS   Aug 22, 2001 - changed /gpfs/a/nco/nwprod to /nwprod
#                         so it will work on both asp and bsp.
#    SAG   Aug 28, 2001 - Added rcp of nwprod.tar to the S70 /offsite
#                       - Added backup tar of necessary restart files
#    SAG   Feb 11, 2002 - Split production.restart.tar file into 1 Gb
#                         segments so that the resulting
#                         files will fit on the backup lnx177
#                         machine which doesn't allow files
#                         larger than 2 Gb.
#    SAG   Apr 01, 2003 - Changed for snow/frost.  Offsite data is now written
#                         hanfs1:/offsite.
#    SAG   Jun 17, 2003 - Added section to save /nwtest and /nwpara to HPSS
#    JLC   Apr 18, 2005 - Changed HPSS_HOSTNAME to add and an 's' after node
#                         name.
# 
#############################################################

###################################
# Run setpdy and initialize PDY variables
###################################
export cycle=t00z
sh /nwprod/util/ush/setpdy.sh
. PDY

set -x

DAY=`date -u +%a`

if test "$DAY" = "Sun"
then
   msg="Started Making Backup from /nwprod to /nwbkup/nwprod"
   /nwprod/util/ush/postmsg.sh "$jlogfile" "$msg"

   ###########################
   # Create New Backup of /nwprod
   ###########################
   cd /nwprod
   tar -cvf /tmpnwprd/nwprod.ccs.tar .

   ###########################
   # Remove Current Backup from /nwbkup
   ###########################
   cd /nwbkup/nwprod
   rm -rf /nwbkup/nwprod/

   ###########################
   # Create New Backup into /nwbkup
   ###########################
   cd /nwbkup/nwprod
   tar -xvf /tmpnwprd/nwprod.ccs.tar

   err=$?
                                                                                                                                                             
   if [ $err -ne 0 ]
   then
     echo "exnwbkup.sh.sms:  Backup to /nwbkup/nwprod not successful."
     exit $err
   fi

   ###########################
   # Copy nwprod.ccs.tar to HANFS for the Offsite backup
   ###########################
   ### Do this part only on the primary machine ###
 if [ $machine = primary ]
 then
   sftp hanfs1 << EOF
     put /tmpnwprd/nwprod.ccs.tar /offsite/nwprod.ccs.tar
     bye
EOF

   ###########################
   # Split up nwprod.tar file to store offsite.
   # rcp backup to lnx177 box for offsite backup.
   ###########################
   cd /tmpnwprd
#   split -b 1000m nwprod.ccs.tar nwprod.ccs.tar.
   rm -rf /tmpnwprd/nwprod.ccs.tar /tmpnwprd/nwprod.ccs.tar.??

   msg="Finished Making Backup from /nwprod to /nwbkup/nwprod"
   /nwprod/util/ush/postmsg.sh "$jlogfile" "$msg"

   msg="Started Making Backup of necessary restart files"
   /nwprod/util/ush/postmsg.sh "$jlogfile" "$msg"

   ###########################
   # Create New Backup of required restart files
   ###########################
   cd /nwges/prod/ndas.hold
   tar cvf /tmpnwprd/production.restart.tar .
   cd /com/nam/prod/ndas.$PDY
   tar uvf /tmpnwprd/production.restart.tar `ls *.t06z.*grb`
   cd /nwges/prod/gfs.$PDY
   tar uvf /tmpnwprd/production.restart.tar gfs.t06z.sfcanl gfs.t06z.sanl gfs.t06z.abias
   cd /com/gfs/prod/gfs.$PDY
   tar uvf /tmpnwprd/production.restart.tar `ls gfs.t06z.*grb`
   cd /nwges/prod/gdas.$PDY
   tar uvf /tmpnwprd/production.restart.tar `ls *.t06z.sfcanl *.t06z.sanl *.t06z.abias`
   cd /com/gfs/prod/gdas.$PDY
   tar uvf /tmpnwprd/production.restart.tar `ls gdas1.t06z.*grb`

   ###########################
   # Copy production.restart.tar to HANFS for the Offsite backup
   ###########################
   sftp hanfs1 << EOF
     put /tmpnwprd/production.restart.tar /offsite/production.restart.tar
     bye
EOF
   cd /tmpnwprd
#   split -b 1000m production.restart.tar production.restart.tar.
   rm -rf /tmpnwprd/production.restart.tar /tmpnwprd/production.restart.tar.??

   msg="Finished Making Backup of necessary restart files"
   /nwprod/util/ush/postmsg.sh "$jlogfile" "$msg"

   export job=jcrossref
   export envir=prod
   export SENDSMS=NO
#   /nwprod/jobs/JCROSSREF.sms.prod

 fi
fi

$SMSBIN/smscomplete
exit
