Installation et sécurisation d'une station Debian 3.0 stable15/05/2004 
  
	
	
	
	
	 
	 ANNEXE 9. Script de restauration
			
			
			
		
	
  
	
	
	
	
	 
	
 ANNEXE 9. Script de restauration 
  
			
			#!/bin/sh
#
# Debian-secinst v0.1.6 : ANNEXE 9 - Script de restauration
# Simon Castro
#
# NDR : Beware of the command syntax, the script will be executed by ash.
#
#############
# VARIABLES #
#############
# The disk to use ...
DISK=`cat /backup/disk_name`
# Informations about the system this restore disk was created for
SERVER_INFOS="/backup/server_infos"
### Backuped configuration files used to restore the system
# The backuped partition table
BACKUPED="/backup/partitions_table"
# The backuped /etc/fstab file
FSTABED="/backup/fstab_file"
# The textfile with the fdisk script : fdisk /dev/xda < $FDISK_SCRIPT
FDISK_SCRIPT="/backup/fdisk_script"
# The backuped lilo configuration file to use to lilo the system.
LILO="/backup/lilo_file"
### Backuped system files
# Path to the file with the system archives information file (the cdrom is mounted on the /mnt mountpoint of the root filesystem)
ARCHIVES="/mnt/archives/infos.txt"
#####################
# DISPLAY FUNCTIONS #
#####################
display_menu()
{
/bin/cat <<EOF
Debian-Secinst v0.1.6 Restore script
Enter alt-[F1-F4] to : have this menu, have a shell, look at the logs.
To reboot : ctrl-alt-del  
			/bin/cat $SERVER_INFOS
Menu
(1) : Show actual partition table.
(2) : Show backuped partition table.
(3) : Show backuped fstab file.
*****
(4) : Write the backuped partition table to disk.
(5) : Format disk using backuped fstab file.
(6) : Mount disk partitions using backuped fstab file.
(7) : Restore the system.
(8) : Lilo the system using backuped lilo file.
(9) : Syncing and unmounting restored system.
*****
(A) : Automated script (4-5-6-7-8-9).
EOF
}
wait_user()
{
/usr/bin/test $AUTOMATED = 0 &&
  {
	echo "Press enter when you're done with this"
	read waituser
  }
}
clear_and_title()
{
/usr/bin/clear
echo "Debian-Secinst v0.1.6 Restore script"
echo
}
##########################
# NON CONSEQUENT ACTIONS #
##########################
show_actual()
{
clear_and_title
echo "Actual partition table is :"
/sbin/fdisk -lu $DISK
echo
wait_user
}
show_backuped()
{
clear_and_title
echo "Backuped partition table is ($BACKUPED) :"
echo
/bin/cat $BACKUPED
echo
wait_user
}
show_fstabed()
{
clear_and_title
echo "Backuped fstab file is ($FSTABED) :"
echo
/bin/cat $FSTABED
echo
wait_user
}
######################
# CONSEQUENT ACTIONS #
######################
write_backuped()
{
/usr/bin/test $AUTOMATED = 0 && clear_and_title 
echo "You're about to write a new partitions table..."
echo "This will delete any data présent on the disk."
echo -n "Are you sure you want to do that (N/y) ? "
/usr/bin/test $AUTOMATED = 0 && read userissure 
/usr/bin/test $AUTOMATED = 1 || /usr/bin/test "$userissure" = "y" &&
  {
	echo
	echo
	echo "  I am deleting the actual partitions table..."
	/bin/dd if=/dev/zero of=$DISK bs=512 count=1 > /dev/null
	/bin/sync
	echo "  I'm creating the new partitions table..."
	fdisk $DISK < $FDISK_SCRIPT > /dev/null
	echo "  Done..."
	echo
	/usr/bin/test $AUTOMATED = 0 && wait_user
  }
}
#
# NDR : The search command for labels on ext partitions is really bad...
#      Couldn't find another way to get the label if fstab file contains /dev/xdaX with X > 9
#
format_disk()
{
/usr/bin/test $AUTOMATED = 0 && clear_and_title 
echo "You're about to format the disk using the backuped fstab file..."
echo "This will delete any data présent on the disk."
echo -n "Are you sure you want to do that (N/y) ? "
/usr/bin/test $AUTOMATED = 0 && read userissure 
/usr/bin/test $AUTOMATED = 1 || /usr/bin/test "$userissure" = "y" &&
  {
	echo
	echo
	echo "Formatting Swap partition(s) : "
	for i in `/bin/grep swap $FSTABED | /usr/bin/tr '\t' ' ' | /usr/bin/cut -d ' ' -f 1`
	  do
		echo "  $i"
		/sbin/mkswap $i > /dev/null
	done
	echo
	echo "Formatting Ext2 partition(s) : "
	for i in `/bin/grep ext2 $FSTABED | /usr/bin/cut -d ' ' -f 1`
	  do
		LABEL=`/bin/grep "$i " $FSTABED | /usr/bin/cut -d ' ' -f 2`
		echo "  $i on $LABEL"
		/sbin/mke2fs -L $LABEL $i > /dev/null
	done
	echo
	/bin/sync
	echo "  Done..."
	echo
	/usr/bin/test $AUTOMATED = 0 && wait_user
  }
}
mount_partitions()
{
/usr/bin/test $AUTOMATED = 0 && clear_and_title 
echo "You're about to mount the backuped's fstab file partition on the current disk..."
echo -n "Are you sure you want to do that (N/y) ? "
/usr/bin/test $AUTOMATED = 0 && read userissure 
/usr/bin/test $AUTOMATED = 1 || /usr/bin/test "$userissure" = "y" &&
  {
	echo
	echo "Mounting / partition on /target : "
	echo
	SLASH=`/bin/grep " / " $FSTABED | /usr/bin/cut -d ' ' -f 1`
	MOUNTED=0
	/bin/mount $SLASH /target && MOUNTED=1
	/usr/bin/test $MOUNTED = 0 && {
	  echo
	  echo "FAILED ... Waiting for user action ..."
	  AUTOMATED=0
	  read useraction
	} 
	/usr/bin/test $MOUNTED = 1 &&  {
	  echo "  $SLASH / partition is now on /target..."
	  echo
	  for i in `/bin/grep ext2 $FSTABED | /bin/grep -v " / " | /usr/bin/cut -d ' ' -f 1`
		do
		  LABEL=`/bin/grep "$i " $FSTABED | /usr/bin/cut -d ' ' -f 2`
		  echo -n "  Creating /target$LABEL mountpoint ... "
		  /bin/mkdir "/target$LABEL"
		  echo "Mounting $i on /target$LABEL"
		  /bin/mount -t ext2 $i "/target$LABEL"
	  done
	  echo
	  echo "  Done..."
	  echo
	  /usr/bin/test $AUTOMATED = 0 && wait_user
	}
 }
}
restore_system()
{
/usr/bin/test $AUTOMATED = 0 && clear_and_title 
echo "You're about to restore the system files on the current disk..."
echo -n "Are you sure you want to do that (N/y) ? "
/usr/bin/test $AUTOMATED = 0 && read userissure 
/usr/bin/test $AUTOMATED = 1 || /usr/bin/test "$userissure" = "y" &&
  {
	echo
	echo "  Mounting cdrom on /mnt"
	/bin/mount /dev/hdc /mnt -t iso9660 && MOUNTED=1
	/usr/bin/test $MOUNTED = 0 && {
	  echo
	  echo "FAILED ... Waiting for user action ..."
	  AUTOMATED=0
	  read useraction
	} 
	/usr/bin/test $MOUNTED = 1 &&  {
	  echo "  Cdrom is now on /mnt..."
	  echo
	  echo "Going to /target"
	  cd /target
	  echo
	  echo "Restoring the system with .tar.gz archives : "
	  for archive in `cat $ARCHIVES | /bin/grep "tar.gz"`
		do
		  echo "  Restoring $archive..."
	  /bin/tar zxvfp $archive > /dev/null
	  done
	  /bin/sync
	  echo
	  echo "Restoring the system with .cpio archives : "
	  for archive in `cat $ARCHIVES | /bin/grep ".cpio"`
		do
		  echo "  Restoring $archive..."
		  /bin/cpio -id < $archive > /dev/null
	  done
	  /bin/sync
	  echo
	  echo "Unmounting cdrom..."
	  /bin/umount /dev/hdc
	  echo
	  echo "  Done..."
	  echo
	  /usr/bin/test $AUTOMATED = 0 && wait_user
	  }
   }
}
lilo_system()
{
/usr/bin/test $AUTOMATED = 0 && clear_and_title 
echo "You're about to lilo the current disk with the backuped lilo file..."
echo -n "Are you sure you want to do that (N/y) ? "
/usr/bin/test $AUTOMATED = 0 && read userissure 
/usr/bin/test $AUTOMATED = 1 || /usr/bin/test "$userissure" = "y" &&
  {
	echo
	echo "Lilo-ing current disk..."
	/sbin/lilo -C $LILO
	echo "  Done..."
	echo
	/usr/bin/test $AUTOMATED = 0 && wait_user
  }
}
sync_system()
{
/usr/bin/test $AUTOMATED = 0 && clear_and_title 
echo "You're about to sync the current disk and umount /target restored file system..."
echo -n "Are you sure you want to do that (N/y) ? "
/usr/bin/test $AUTOMATED = 0 && read userissure 
/usr/bin/test $AUTOMATED = 1 || /usr/bin/test "$userissure" = "y" &&
  {
	echo
	echo
	echo "Syncing..."
	/bin/sync
	echo
	echo "Umounting partitions : "
	CPT=1
	while /usr/bin/test $CPT -le `/bin/cat $FSTABED | /bin/grep -v " / " | /usr/bin/wc -l`
	  do
		LABEL=`/usr/bin/tail -n $CPT $FSTABED | /usr/bin/head -n 1 | /usr/bin/cut -d ' ' -f 1`
		echo "  Unmounting $LABEL"
	/bin/umount $LABEL
		CPT=$(($CPT+1))
	done
	echo
	echo "Unmounting '/' partition... "
	/bin/umount /target
	echo
	echo "  Done..."
	echo
	/usr/bin/test $AUTOMATED = 0 && wait_user
  }
}
##################
# AUTOMATED CALL #
##################
automated()
{
  AUTOMATED=1
  clear_and_title
  write_backuped
  format_disk
  mount_partitions
  restore_system
  lilo_system
  sync_system
  echo "Automated script done..."
  echo 
  echo "You can now check the restored system with a shell or reboot the system..."
  read waituser
  AUTOMATED=0
}
########
# MAIN #
########
while [ 1 ]
do
  /usr/bin/clear
  AUTOMATED=0
  display_menu
  echo -n "What do you want ? "
  read userinput
  case $userinput in 
	  1) show_actual ;;
	  2) show_backuped ;;
	  3) show_fstabed ;;
	  4) write_backuped ;;
	  5) format_disk ;;
	  6) mount_partitions ;;
	  7) restore_system ;;
	  8) lilo_system ;;
	  9) sync_system ;;
	  A) automated ;;
  esac
  /usr/bin/clear
done  
		
	
 
 
		Copyright (c) 2003 Simon Castro, scastro [ at ] entreelibre.com. 
		 
		Permission is granted to copy, distribute and/or modify this document under the
		terms of the GNU Free Documentation License, Version 1.2  or  any later version
		published by the Free Software Foundation; with  the  Invariant  Sections being
		LIST THEIR  TITLES,  with  the  Front-Cover  Texts  being  LIST, and   with the
		Back-Cover Texts being LIST. 
		You must have received a copy of the license with this document and  it  should
		be présent in the fdl.txt file.  
		If you did not receive this file or if you don't think this  fdl.txt license is
		correct,  have  a  look  on  the  official  http://www.fsf.org/licenses/fdl.txt
		licence file.
	 
       |