#!/bin/sh
set -u  # force better programming and ability to use check for not set
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

# $Id$

# This script attempts to backup importatnt things on a node

bang=`echo -e "\041"`
node=`uname -n| sed -e 's/\([^\.]\)\..*/\1/'`
gang=`gang`
case $gang in
  stken) op="-ne"
          ;;
   d0en) op="-eq"
          ;;
  cdfen) op="-eq"
          ;;
      *) echo `date` NOT backing up nodes - Policy choice.
          exit 0
          ;;
esac

d=`date +"%j" | sed -e 's/^0*//'`
d2=`expr $d \/ 2 \* 2`

select="$d2 $op $d"
if test $select; then
  BACKUP=/master
# rip8:/fnal  /master nfs noauto 0 0
# rip8:/rip8b /master nfs noauto 0 0
else
  BACKUP=/backup
# d0ensrv3:/diskc  /backup nfs noauto 0 0
# stkensrv3:/diskc /backup nfs noauto 0 0
fi

# HACK UNTIL DISKS STRAIGHTENED OUT!
if /bin/true; then
  BACKUP=/backup
fi
# END HACK

CACHE=cachen2a:/diska/enstore_backup/$gang/tarit

mount $BACKUP
mount_failed=$?


if [ `echo $node | grep -c mvr` -ne 0 ]; then
  M=$BACKUP/enstore/$gang/movers
else
  M=$BACKUP/enstore/$gang/servers/`echo $node | eval sed -e 's/$gang//'`
fi
export M

here=`pwd`
cd $M
if [ "$BACKUP" = "/backup" ]; then
  mkdir -p old >/dev/null 2>/dev/null
  mv files.err files.tgz partition_table.dd old
else
  rm files.err files.tgz partition_table.dd
fi
cd $here

echo "`date`: dd-ing partition table to $M/partition_table"
dd if=/dev/hda of=$M/partition_table.dd bs=512 count=1

# generate a script excluding files we don't care about.
# NOTE:  no users files are backed up.
file_users=/tmp/file_users
export file_users
rm -f $file_users
users=""
ls -1 /home | while read user; do if [ "$user" != "enstore" ]; then
   users="$users --exclude='/home/$user/*'" ; fi; rm -f $file_users; echo "$users" > $file_users; done
users=`cat $file_users`
rm -f $file_users
cmd=""
cmd="nice -n 20 tar -l
--exclude='/lost+found/*' --exclude=/proc/ --exclude='/tmp/*' --exclude='/var/log/*'
--exclude='/*\.jon*' --exclude='/*\.fnal\.gov\.startup' --exclude='/*\.fnal\.gov\.monitor_server.output'
--exclude=BITERROR --exclude=SEL --exclude='/var/tmp/*' --exclude='/var/spool/mqueue/*' --exclude=.netscape
$cmd $users  -czf $M/files.tgz  / 2>$M/files.err"
echo "`date`: starting tar"
echo $cmd
eval $cmd
echo "`date`: tar complete"
ls -alR $M
STASH=$CACHE/$d.$node.files.tgz
echo "`date` stashing a copy on $STASH'"
echo disabled enrcp  $M/files.tgz $STASH

# on server1, also copy all "special" node-specific files from all nodes
if [ $node = ${gang}srv1 ]; then
  SPECIAL=$BACKUP/enstore/$gang/special
  mkdir -p $SPECIAL >/dev/null 2>&1
  SF=$SPECIAL/special_files
  rm -f $SF
  echo >$SF "\
#$bang/bin/sh
thisnode=\`uname -n| sed -e 's/\([^\.]\)\..*/\1/'\`
F=/tmp/\${thisnode}.special.tgz
rm -f /tmp/precious
for precious in \\
  /etc/fstab \\
  /etc/rc.d/rc.local \\
  /etc/rc.d/rc.sysinit \\
  /etc/rc.d/init.d/pnfs \\
  /etc/rc.d/init.d/dcache-boot \\
  /etc/rc.d/rc6.d/K94pnfs \\
  /etc/rc3.d/K01aaapnfs \\
  /etc/rc3.d/K02bbbdcache-boot \\
  /etc/lilo.conf \\
  /etc/raidtab \\
  /etc/hosts.* \\
  /etc/HOSTNAME \\
  /etc/ntp.conf \\
  /etc/ntp \\
  /etc/krb5.keytab \\
  /etc/krb5.conf \\
  /etc/sysconfig/network \\
  /etc/sysconfig/network-scripts/ifcfg* \\
  /fnal/ups/kt/  \\
  /home/*/.k5login \\
  /home/*/.k5user \\
  /home/enstore/CRON/ \\
  /root/enstore/CRON/ \\
  /root/.k5login \\
  /root/.k5user \\
  /usr/local/etc/farmlets \\
  /usr/etc/ \\
  /var/adm/krb5/ \\
  /var/spool/cron/; do if [ -r \$precious ]; then echo \$precious >>/tmp/precious; fi; done
rm -fr \$F
. /usr/local/etc/setups.sh
setup enstore
tar -czf \$F \`cat /tmp/precious\`
enrcp \$F $node:$SPECIAL
"
  chmod +x $SF
  rgang -c $gang $SF /tmp/special_files
  rgang $gang /tmp/special_files
  ls -l $SPECIAL

  #now copy everything over to the cache
  for i in `ls -1 $SPECIAL`; do
      echo enrcp $SPECIAL/$i $CACHE/$d.$i
      echo disabled enrcp $SPECIAL/$i $CACHE/$d.$i
  done


fi

#On the servers, there are extra files that also need to be backed up
if [ `echo $node | grep -c mvr` -eq 0 ]; then
  SPECIALE=$BACKUP/enstore/$gang/special/$node.extra.tgz
  T=/tmp/`basename $SPECIALE`
  rm -f $T
  rm -f /tmp/special2
  for precious in \
    /home/bakken/P \
    /local/ups/db/pnfs \
    /local/ups/prd/pnfs \
    /boot /usr/src/ \
    /lib/modules \
    /usr/local/bin/rpcfix.pl \
    /usr/local/bin/find.rpc \
    /root/clear.volmap.protection \
    /root/raidcheck; 
  do if [ -r $precious ]; then echo $precious >>/tmp/special2; fi; done
  #tar -czf $T /home/bakken/P /local/ups/db/pnfs /local/ups/prd/pnfs /boot /usr/src/ /lib/modules /usr/local/bin/rpcfix.pl /usr/local/bin/find.rpc /root/clear.volmap.protection /root/raidcheck
  tar -czf $T `cat /tmp/special2`
  # I don't know how to get just the /pnfs... mountpoints or /diska /diskb... mountpoints
  cp $T $SPECIALE

  #now copy everything over to the cache
  echo enrcp $SPECIALE $CACHE/$d.`basename $SPECIALE`
  echo disabled enrcp $SPECIALE $CACHE/$d.`basename $SPECIALE`

fi
if [ $mount_failed -eq 0 ]; then
    umount $BACKUP
fi
