#!/bin/bash
VERSION='$Id: sbc_disk_info_backup,v 1.16 2006/03/31 09:26:05 sesam Exp $'
#   linux 'disk_info' backup
#   (partitions, format, mount and bootloader)
#
#   SEP AG 2004-09-01
#

export LANG=C
export LC_ALL=C

SMBINDIR=$( grep -i ^sm_bin_sesam /etc/sesam2000.ini|cut -d = -f 2 )
export PATH=${SMBINDIR}:$PATH


logerr()
{
echo `date +"%Y-%m-%d %T:"`" sbc-1500: Error:    $1" >&2
}
echo `date +"%Y-%m-%d %T:"`" Start SEPsesam disk info backup" >&2
echo `date +"%Y-%m-%d %T:"`" $VERSION" >&2
tmp_info_dir=$1/$2
curr_work_dir=`pwd`
system_name=`uname -s`
if [ "$#" -lt "2" ]; then
  echo "Not enough arguments" >&2
  exit 2
fi

case $system_name in
 Linux)
  ;;
 *)
   echo "" >&2
   echo "Error: Unknown operating system type: $system_name" >&2
   echo "Exiting ..." >&2
   exit 2
   ;;
esac
if [ ! `whoami` = "root" ] ; then
  echo "" >&2
  echo "You need to be root to run this, otherwise"
  echo "sfdisk produces no output. Continuing anyway ..."
  echo ""
fi

#
#  First collect the disk_info
#

SFDISK=sfdisk
[ -r /etc/redhat-release ] && SFDISK=sfdisk.sesam

echo "Using ${SFDISK}" >&2
mkdir -p $tmp_info_dir
cd $tmp_info_dir || { echo "ERROR: Could not change to $tmp_info_dir. Exiting ..." ;  exit 2 ; }
rm -f -v *.sdi
mount -l >mount.sdi || logerr "Could not save mount informations"
mount -l -t ext2 >mount.ext2.sdi || logerr "Could not save mount informations" 
mount -l -t ext3 >mount.ext3.sdi || logerr "Could not save mount informations" 
mount -l -t reiserfs >mount.reiserfs.sdi || logerr "Could not save mount informations" 
mount -l -t xfs >mount.xfs.sdi || logerr "Could not save mount informations" 
swapon -s  >swap.sdi || logerr "Could not save swap informations" 
cp -v /etc/fstab fstab.sdi
cp -v /etc/mtab  mtab.sdi
echo "------ fstab -------" >&2
cat fstab.sdi >&2
echo "--- end of fstab ---" >&2

echo "Saving SoftwareRAID configuration" >&2
cat /proc/mdstat |grep "md[0-9][0-9]*.:"|cut -d : -f 1|while read array
do
   mdadm --misc --query --detail /dev/$array >${array}_sswr.sdi
done

echo "Saving LVM configuration" >&2
pvs --noheadings -o name,uuid,vg_name >slvm_conf.sdi
for f in /etc/lvm/backup/*
do 
  echo $f
  cp -v $f $(basename $f)_slvm.sdi
done
echo "------ pvdisplay -------" >&2
pvdisplay >&2
echo "" >&2
echo "------ vgdisplay -------" >&2
vgdisplay >&2
echo "" >&2
echo "------ lvdisplay -------" >&2
lvdisplay >&2
echo "" >&2
 
#cp -v /etc/lilo.conf  lilo.sdi
#cp -v /etc/grub.conf  grub.sdi
#tar cvf grub.tar.sdi /boot/grub/* /usr/sbin/grub* /usr/lib/grub/*

df -Tl >df.sdi
${SFDISK} -s|grep -v 'md[0-9]*' >sfdisk.disks.sdi || logerr "Could not save partition table"
grep "^/dev/" sfdisk.disks.sdi | sed -n 's/\(^.*\):.*$/\1/p' >disks.sdi
for i in `cat disks.sdi`; do
   j=`echo $i | cut -c6-|tr '/' '-'` 
   ${SFDISK} -l $i >sfdisk.$j.sdi || logerr "Could not save partition table"
   ${SFDISK} -d $i >sfdisk.make.$j.sdi || logerr "Could not save partition table"
   dd if=$i of=dd.mbr12.$j.sdi bs=512 count=2 >/dev/null 2>&1 || logerr "Could not save partition table"
done
route -n >route.sdi
ifconfig >ifconfig.sdi
echo EOF >../diskinfo-eof.tmp
echo `date +"%Y-%m-%d %T:"`" SEPsesam disk info backup finished" >&2
exit 0
#
#

