#!/bin/bash
VERSION='$Id: sbc_disk_info_restore,v 1.10 2006/05/23 13:28:13 sesam Exp $'
# Restores sesam disk_info saveset of a linux computer
# (Partitions, filesystems, mount ....)
#
#   SEP AG 07.2003
#
tmp_info_dir=$1
AUTO=$2
curr_work_dir=`pwd`
system_name=`uname -s`


#############################################################
# READ INI
read_ini()
{
  SM_INI=`grep -i '^sm_ini=' /etc/sesam2000.ini|cut -d"=" -f2`
  echo `grep -i "$1=" "$SM_INI"|cut -d"=" -f2`
}
#
#
# VERBOSE
my_echo()
{
  if [ "$VERBOSE" = "-v" ]
  then
    echo $1
  fi
}

## read values from mdadm output
readmdval()
{
  grep "$2" "$1"|cut -d : -f 2|tr -d ' '
}



#
# PATH SUBROUTINE
sub_path()
{
  my_echo "VERSION=$VERSION"

  GV_RO=`read_ini gv_ro`
  export GV_RO
  GV_RW=`read_ini gv_rw`
  export GV_RW
  GV_RW_INI=`read_ini gv_rw_ini`
  export GV_RW_INI
  GV_RO_DB=`read_ini gv_ro_db`
  export GV_RO_DB
  GV_RO_BIN=`read_ini gv_ro_bin`
  export GV_RO_BIN

  PATH=:${GV_RO_BIN}:${GV_RO_DB}:${GV_RO}skel:$PATH:/sbin:/bin:/usr/bin
  export PATH
}
#############################################################

echo `date +"%Y-%m-%d %T:"`" SEPsesam disk info restore" >&2
echo `date +"%Y-%m-%d %T:"`" $VERSION" >&2

case $system_name in
 Linux)
  ;;
 *)
   echo ""
   echo "Script is not adapted to this OS: $system_name"
   exit 1
   ;;
esac
if [ ! `whoami` = "root" ] ; then
  echo ""
  echo "You should be 'root' to do this. Continuing anyway ..."
  echo ""
fi


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

sub_path
##Create cciss device

if lsmod | grep cciss >/dev/null
then
   cd /dev &&  MAKEDEV cciss
fi
 
##Shutdown lvm first
/etc/init.d/lvm stop

a=yes

cd $tmp_info_dir
#
# First make the partitioning loop
#
for i in `cat disks.sdi`; do
  j=`echo $i | cut -c6-|tr '/' '-'`
#
echo ""
echo "Partition table during backup was:"
cat sfdisk.$j.sdi
echo ""
echo "Current partition table:"
${SFDISK} -L -l $i
#sfdisk -L -R $i
echo ""
echo "This script will repartition disk $i to its previous partitioning state."
echo ""
echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
echo ""
echo -n "Are you sure you want to continue? (yes/no): "

[ $AUTO ] || read a

if [ x$a == xyes ] ; then
echo ""
echo "Partitioning disk $i"
# restore partition info
dd if=dd.mbr12.$j.sdi of=$i bs=512 count=2
# repartition
${SFDISK} -L -f $i <sfdisk.make.$j.sdi 
#
echo ""
echo "The new partitioning is:"
${SFDISK} -L -l $i
echo ""
echo "If the disk is correctly partitioned, you should"
echo "now go on with the formatting."
echo ""
fi

done

[ $AUTO ] || read a



# Create Linux software RAID

if ls *_sswr.sdi >/dev/null 2>&1
then
echo "=================================="
echo "Create SoftRAID..."
echo "----------------------------------"
for f in *_sswr.sdi
  do
    raidlevel=$( readmdval $f "Raid Level" )
    raiddevices=$( readmdval $f "Active Devices" )
    sparedevices=$( readmdval $f "Spare Devices" )
    devices=$( awk '/active.*\/dev|spare.*\/dev/ {printf("%s ", $(NF))} ;END { print ""}' $f)
    echo "mdadm --create --verbose /dev/$( basename $f _sswr.sdi ) --auto=yes --level=${raidlevel} --force --run --raid-devices=${raiddevices} --spare-devices=${sparedevices} ${devices}"
    mdadm --create --verbose /dev/$( basename $f _sswr.sdi ) --auto=yes --level=${raidlevel} --force --run --raid-devices=${raiddevices} --spare-devices=${sparedevices} ${devices}
  done
else
  echo "No SoftwareRAID configuration found"
fi

[ $AUTO ] || read a
# Create Linux logical volumes
echo "=================================="
echo "Recreate logical volumes..."
echo "Create pysical volumes..."
echo "----------------------------------"
while read pv
do
   f=( $pv )
   pvcreate -v -ff -y --restorefile ${f[2]}_slvm.sdi -u ${f[1]} ${f[0]}
done <slvm_conf.sdi

echo ""
echo "Create logical volumes..."
echo "----------------------------------"
if ls *_slvm.sdi >/dev/null 2>&1
then
  for v in *_slvm.sdi ; do
    echo "Restore LVM: $v"
    vgcfgrestore -v -f $v $( basename $v _slvm.sdi )
    vgchange -v -a y $( basename $v _slvm.sdi )
  done
fi
echo "----------------------------------"
vgdisplay
echo "----------------------------------"
echo ""

echo "If the disk is correctly partitioned, you should"
echo "now go on with the formatting."
echo -n "Are you sure you want to continue? (yes/no): "
[ $AUTO ] || read a

echo ""
echo "Create swap..."
echo "----------------------------------"
grep '^/dev/' swap.sdi | cut -d " " -f 1 | xargs -n 1 mkswap
#
# Now make the formatting loop
#
for i in `cat disks.sdi;echo /dev/mapper`; do
  j=`echo $i | cut -c6-|tr '/' '-'`
echo ""
echo "This script will format all partitions on disk $i."
echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
echo -n "Are you sure you want to continue? (yes/no): "

[ $AUTO ] || read a

if [ x$a == xyes ] ; then
   # Find swap partitions in output from sfdisk
   # if [ "$j" != "mapper" ];then 
   # k=`grep "^$i.*82  Linux [Ss]wap" sfdisk.$j.sdi | cut -d ' ' -f 1`
   # for disk in $k; do
   #   echo "Formatting $disk -- swap partition"
   #   mkswap $check $disk
   #   echo ""
   # done
   # fi
   # Find ext2 partitions in mount output
   k=`grep "^$i" mount.ext2.sdi | cut -d ' ' -f 1`
   for disk in $k; do
      echo "Formatting $disk -- ext2 partition"
      label=`grep "^$disk" mount.ext2.sdi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
      if [ x$label = x ] ; then
         mke2fs -v $check $disk
      else
         mke2fs -v $check -L $label $disk
      fi
      tune2fs -O ^dir_index $disk
      echo ""
   done
   # Find ext3 partitions in mount output
   k=`grep "^$i" mount.ext3.sdi | cut -d ' ' -f 1`
   for disk in $k; do
      echo "Formatting $disk -- ext3 partition"
      label=`grep "^$disk" mount.ext3.sdi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
      if [ x$label = x ] ; then
         mke2fs -v -j $check $disk
      else
         mke2fs -v -j $check -L $label $disk
      fi
      tune2fs -O ^dir_index $disk
      echo ""
   done
   # Find reiserfs partitions in mount output
   k=`grep "^$i" mount.reiserfs.sdi | cut -d ' ' -f 1`
   for disk in $k; do
      echo "Formatting $disk -- reiserfs partition"
      label=`grep "^$disk" mount.reiserfs.sdi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
      if [ x$label = x ] ; then
         ${GV_RO_BIN}/mkreiserfs.suse -d -f -f $disk
      else
         ${GV_RO_BIN}/mkreiserfs.suse -d -f -f -l $label $disk
      fi
      echo ""
   done
   # Find xfs partitions in mount output
   k=`grep "^$i" mount.xfs.sdi | cut -d ' ' -f 1`
   for disk in $k; do
      echo "Formatting $disk -- xfs partition"
      label=`grep "^$disk" mount.xfs.sdi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
      if [ x$label = x ] ; then
         mkfs.xfs -f $disk
      else
         mkfs.xfs -f -L $label $disk
      fi
      echo ""
   done
fi

done

# goto work directory
cd $1

cat >sbc_mount_drives <<END_OF_DATA
#!/bin/sh
#
#  Mount disk drives  -- created by sbc_disk_info_restore
#
END_OF_DATA
cat >sbc_umount_drives <<END_OF_DATA
#!/bin/sh
#
#  Unmount disk drives  -- created by sbc_disk_info_restore
#
END_OF_DATA
echo "#" >>sbc_mount_drives
echo "#" >>sbc_umount_drives
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/sbc_mkmnt ext2 \1 \/mnt\/disk\2/p' $tmp_info_dir/mount.ext2.sdi >/tmp/1$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/sbc_mkmnt ext3 \1 \/mnt\/disk\2/p' $tmp_info_dir/mount.ext3.sdi >>/tmp/1$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/sbc_mkmnt reiserfs \1 \/mnt\/disk\2/p' $tmp_info_dir/mount.reiserfs.sdi >>/tmp/1$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/sbc_mkmnt xfs \1 \/mnt\/disk\2/p' $tmp_info_dir/mount.xfs.sdi >>/tmp/1$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/umount -v \/mnt\/disk\2/p' $tmp_info_dir/mount.ext2.sdi >/tmp/2$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/umount -v \/mnt\/disk\2/p' $tmp_info_dir/mount.ext3.sdi >>/tmp/2$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/umount -v \/mnt\/disk\2/p' $tmp_info_dir/mount.reiserfs.sdi >>/tmp/2$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/umount -v \/mnt\/disk\2/p' $tmp_info_dir/mount.xfs.sdi >>/tmp/2$$
# sort so that root is mounted first and unmounted last
sort -k 4 </tmp/1$$ >>sbc_mount_drives
sort -r -k 3 </tmp/2$$ >>sbc_umount_drives
rm -f /tmp/1$$ /tmp/2$$

chmod 755 sbc_mount_drives sbc_umount_drives
./sbc_mount_drives

#create the proc directory
mkdir -v -p /mnt/disk/proc
mkdir -v -p /mnt/disk/sys

echo ""
echo "Current mount status:"
echo "============================================================"
mount|sed -n '/\/mnt\/disk/p'
echo "============================================================"
echo ""
echo "Partitions should be formated and mounted correctly"
echo "Please check these before continue with FULL restore of DATA"
echo ""
echo -n "Are you sure you want to continue? (yes/no): "

[ $AUTO ] || read a
echo ""
exit 0


