脚本欣赏----Shell Script to Clone Linux System - 20041201

发表于:2007-07-04来源:作者:点击数: 标签:
From: http://www.yick.org/website/index.php?module=documentsJAS_DocumentManager_op=viewDocumentJAS_Document_id=12 代码: #!/bin/sh # Desc: Dump and Restore Disk using dump and restore commands # Author: Swergar 脚本欣赏----Shell Script to C
From: http://www.yick.org/website/index.php?module=documents&JAS_DocumentManager_op=viewDocument&JAS_Document_id=12

代码:

#!/bin/sh

# Desc: Dump and Restore Disk using dump and restore commands
# Author: Swergar

脚本欣赏----Shell Script to Clone Linux System - 20041201

From: http://www.yick.org/website/index.php?module=documents&JAS_DocumentManager_op=viewDocument&JAS_Document_id=12

代码:

#!/bin/sh

# Desc: Dump and Restore Disk using dump and restore commands
# Author: Swergar
#
# *** Please read the bottom of this script for Help ***
#

## Configurations

# which dir to store backup files?
#BACKDIR="/mnt/ghost/`hostname -s`"
BACKDIR="/opt/backup" # which dir to store backup files?
#BACKDIR="/mnt/ghost/test"

# which dir/files want to exclude?
EXCLUDE="/var/log /var/spool/mail /opt/config_backup $BACKDIR"

# define these values for raid system
PHYDISK=hda
MIRRORDISK=hdb

################################################################################


# Detect raid
ISRAID=`grep `df / | grep ^/dev| cut -d " " -f 1 | sed 's//dev///g'` /proc/mdstat | grep -c md`

if [ "$ISRAID" -gt "0" ] ; then
  RAIDDEVICE="yes" ;
  MD=`df / | grep ^/dev| cut -d " " -f 1 | sed 's//dev///g'| sed 's/[0-9]//g'`
  DUMPPRTS=`df | grep ^/dev/$MD | sed 's/  */ /g'| cut -d " "  -f 6| grep -v "^/$" `
  rm -f /tmp/fstab.raid /tmp/df.txt
  RAID=`cat /proc/mdstat |  grep ^md| cut -d " "  -f 1,5| tr " " ":" |  sed 's/[[0-9]]//g'`
  for i in $RAID ; do
    HRD=`echo $i | cut -d ":" -f 1`
    HDEV=`echo $i | cut -d ":" -f 2| sed 's/[a-z]//g'`
    HDEV=$PHYDISK$HDEV
    cat /etc/fstab | grep $HRD |  sed s/$HRD/$HDEV/ >> /tmp/fstab.raid
    df -m -l -T --sync | grep $HRD |  sed s/$HRD/$HDEV/ >> /tmp/df.txt
  done
  FSTAB="/tmp/fstab.raid"
  DF="cat /tmp/df.txt"
else
  RAIDDEVICE="no";
  FSTAB="/etc/fstab"
  DF="df -m -l -T --sync"
  PHYDISK=`df / | grep ^/dev| cut -d " " -f 1 | sed 's//dev///g'| sed 's/[0-9]//g'`
  DUMPPRTS=`df | grep ^/dev/$PHYDISK | sed 's/  */ /g'| cut -d " "  -f 6| grep -v "^/$"`
fi

if [ "$RAIDDEVICE" == "" -o "$PHYDISK" == "" -o "$DUMPPRTS" == ""  ] ; then
  echo "$RAIDDEVICE,$PHYDISK,$DUMPPRTS"
  echo "#Some Variables are not defined"
  echo
  exit 1
fi

if [ ! -d "$BACKDIR" ] ; then 
echo
echo "#*** Backup dir '$BACKDIR' does not exist!!!"
echo
exit 1
fi

which parted > /dev/null 2>&1
if [ $? != "0" ] ; then
  echo "#parted not found, exit!"
  echo
  exit 1
fi

which dump > /dev/null 2>&1
if [ $? != "0" ] ; then
  echo "#dump not found, exit!"
  echo
  exit 1
fi

which stat > /dev/null 2>&1
if [ $? != "0" ] ; then
  echo "#stat tool not found, unset EXCLUDE"
  sleep 3
  EXCLUDE=""
fi

PARTS=`/sbin/parted /dev/$PHYDISK p | grep ^[0-9] | sed 's/ext3/ext2/g' | sed 's/, /,/g' |grep ^[0-9]| sed 's/  */:/g'`

if [ $? != "0" ] ; then
echo "#***Error in clone partition table!!!"
sleep 3
echo
exit 1
fi

rm -f $BACKDIR/partitions.sh
for j in $PARTS ; do
  MIRROR=`echo $j | cut -d ":" -f 1`
  PSTART=`echo $j | cut -d ":" -f 2`
  PEND=`echo $j | cut -d ":" -f 3`
  TYPE=`echo $j | cut -d ":" -f 4`
  FS=`echo $j | cut -d ":" -f 5`
  FLAG=`echo $j | cut -d ":" -f 6`
  echo "parted -s /dev/$PHYDISK rm $MIRROR" >> $BACKDIR/partitions.sh
  if [ "$TYPE" != "extended" ] ; then
    echo "parted -s /dev/$PHYDISK mkpart $TYPE $FS $PSTART $PEND" >> $BACKDIR/partitions.sh
  else
    echo "parted -s /dev/$PHYDISK mkpart $TYPE $PSTART $PEND" >> $BACKDIR/partitions.sh
  fi
  if [ `echo $FLAG|grep -ci boot ` == 1 ] ; then
    echo "parted -s /dev/$PHYDISK set $MIRROR boot on" >> $BACKDIR/partitions.sh
  fi
  if [ `echo $FLAG|grep -ci raid ` == 1 ] ; then
    echo "parted -s /dev/$PHYDISK set $MIRROR raid on" >> $BACKDIR/partitions.sh
  fi
done

## extra action for raid
if [ $RAIDDEVICE == "yes" ] ; then
  cat $BACKDIR/partitions.sh | sed "s/$PHYDISK/$MIRRORDISK/g" > /tmp/part.sh
  cat /tmp/part.sh >> $BACKDIR/partitions.sh
  rm /tmp/part.sh

fi

## Export host info to a file
echo > $BACKDIR/system-info.txt
hostname >> $BACKDIR/system-info.txt
echo >> $BACKDIR/system-info.txt
date >> $BACKDIR/system-info.txt
echo >> $BACKDIR/system-info.txt
ifconfig >> $BACKDIR/system-info.txt
echo >> $BACKDIR/system-info.txt
df -h -T >> $BACKDIR/system-info.txt
echo >> $BACKDIR/system-info.txt
fdisk -l >> $BACKDIR/system-info.txt
echo >> $BACKDIR/system-info.txt
cat /proc/cpuinfo >> $BACKDIR/system-info.txt
echo >> $BACKDIR/system-info.txt
cat /proc/meminfo  >> $BACKDIR/system-info.txt
echo >> $BACKDIR/system-info.txt
cp -f $BACKDIR/.bak

echo "
if [ "" != "restore" ] ; then
  echo "Usage: restore [nopart]  - start restore"
  echo Exit
  exit
fi
" >  $BACKDIR/restore.sh

echo "cd `dirname `" >> $BACKDIR/restore.sh
echo "DIR=`pwd`" >> $BACKDIR/restore.sh


echo "
if [ "" != "nopart" ] ; then
  sh $DIR/partitions.sh
else
  echo Please create partition manually
  echo If partition is not created, the restore will be fail.
  echo please refer to partition.sh for partition creation.
  echo restore will start within 5 sec.
  sleep 5
fi
" >> $BACKDIR/restore.sh

echo "echo Start Restoring ... " >> $BACKDIR/restore.sh



## Dump data and mount partition in client
echo "mkdir /restore" >> $BACKDIR/restore.sh
DATA=`$DF | grep $PHYDISK |sed 's/  */ /g'| cut -d " "  -f 1-3,7| tr " " ":"`
for i in $DATA ; do
  DEV=`echo $i | cut -d ":"  -f 1`
  LABEL=`echo $i | cut -d ":"  -f 4`
  FS=`echo $i | cut -d ":"  -f 2`
  if [ "$LABEL" == "/" ] ; then
    echo "
mkfs.$FS -L '$LABEL' $DEV
mount $DEV /restore/
cd /restore/
echo Restoring root partition
restore rf $DIR/file-root.dump /restore/
    ">>  $BACKDIR/restore.sh   
  fi
done

for i in $DATA ; do
  DEV=`echo $i | cut -d ":"  -f 1`
  LABEL=`echo $i | cut -d ":"  -f 4`
  FS=`echo $i | cut -d ":"  -f 2`
  if [ "$LABEL" != "/" ] ; then
    echo "mkfs.$FS -L '$LABEL' $DEV" >> $BACKDIR/restore.sh
    echo "mount $DEV /restore$LABEL" >> $BACKDIR/restore.sh
  fi
done

for i in $EXCLUDE ; do
    if [ -e "$i" ] ; then
     ISLOCAL=`df $i | grep -ci DEV `
     if [ "$ISLOCAL" -gt 0 ] ; then NEWEXCLUDE="$i $NEWEXCLUDE"; fi
  fi
done
EXCLUDE="$NEWEXCLUDE"

## Create script to Restore from dump backups
echo "sync"
true > /tmp/dumpexclude-root

if [ "$EXCLUDE" !="" ] ; then
  for E in $EXCLUDE ; do
    if [ `df $E | grep -c /$` == "1" ]; then
      stat $E |  grep Inode | sed 's/  */ /g'| cut -d " " -f 3 >> /tmp/dumpexclude-root
    fi
  done
fi
echo "dump -E /tmp/dumpexclude-root -0 -z -f $BACKDIR/file-root.dump / 2>&1 | tee -a $BACKDIR/dump.log "
if [ "$DUMPPRTS"!="" ] ; then
for i in $DUMPPRTS ; do
    if [ -d "$i" ] ; then
      i=`$DF | grep -w $i | grep $PHYDISK | sed 's/  */ /g' | cut -d " "  -f 7| tr -d "/"`
      FILE=`echo $i | tr -d "/" `
      if [ "$i" != "" ] ; then
        true > /tmp/dumpexclude-$i
        if [ "$EXCLUDE"!="" ] ; then
          for E in $EXCLUDE ; do
            if [ `df $E | grep -c $i$` == "1" ]; then
              stat $E |  grep Inode | sed 's/  */ /g'| cut -d " " -f 3 >> /tmp/dumpexclude-$i
            fi
          done
        fi
        echo "dump -E /tmp/dumpexclude-$FILE -0 -z -f $BACKDIR/file-$FILE.dump /$i 2>&1 | tee -a $BACKDIR/dump.log "
        echo cd /restore/$i >>  $BACKDIR/restore.sh
        echo restore rf $DIR/file-$FILE.dump /restore/$i/ >>  $BACKDIR/restore.sh
      fi
    fi
done
fi

## create dummy exclude files and directories
if [ "$EXCLUDE"!="" ] ; then
echo "sleep 3" >> $BACKDIR/restore.sh 2>/dev/null
find $EXCLUDE -xdev -type d -exec echo "mkdir -p /restore{}" ; >> $BACKDIR/restore.sh 2>/dev/null
find $EXCLUDE -xdev -type f -exec echo "touch /restore{}" ; >> $BACKDIR/restore.sh 2>/dev/null
for i in `find $EXCLUDE` ; do
  MyUID=`stat $i| grep Uid | sed 's/  */ /g' |cut -d " " -f 5| tr -d "/"`
  MyGID=`stat $i| grep Uid | sed 's/  */ /g' |cut -d " " -f 9| tr -d "/"`
  echo "chown $MyUID:$MyGID /restore$i" >> $BACKDIR/restore.sh
  PERM=`stat $i | grep Uid | sed 's/  */ /g' | cut -d "("  -f 2| cut -d "/" -f 1`
  echo "chmod $PERM /restore$i" >> $BACKDIR/restore.sh
done
fi

## create swap
echo "mkswap `grep swap $FSTAB | cut -d " " -f 1`" >> $BACKDIR/restore.sh

## Restore boot records
if [ -e "/etc/lilo.conf" ] ; then
  echo "chroot /restore lilo -vv" >> $BACKDIR/restore.sh
fi
if [ -e "/boot/grub/menu.lst" ] ; then
  echo "chroot /restore grub-install /dev/$PHYDISK" >> $BACKDIR/restore.sh
fi

echo "echo ... Done " >> $BACKDIR/restore.sh
echo "chmod 644 $BACKDIR/*.*"
echo "rm -f /tmp/dumpexclude*"

#echo "umount /mnt/ghost"

exit 0



################################################################################

Instruction:

   Dump:
    1. edit BACKDIR and EXCLUDE in the configuration part of this script
    2, mount/create the $BACKDIR defined in the script
    3. run the sciprt once to see any errors
    4. run again and pile to sh : i.e. ./dumpdisk.sh | sh
    5. dump.log can be found in the $BACKDIR dir

   Restore:
    1. boot from redhat (fedora is recommanded) cd as rescure mode, skip hd detetion.
    2. setup ip address
    3. mount to remote nfs server or backups in cd/dvd
    4. cd to the dir with backup source
    5. if you want to modify the partition table to be restore, edit partition.sh
    6. if you do not want to modify existing partition table in the target disk,
       run "./restore restore nopart" to skip running partition.sh.
            or
       For normal restore, use "./restore restore".
    7. reboot
    8. fsck will be run automatically for first boot up

Remarks:

   1. excluded dir will also be created but the files will becomes zero size.
   2. Both grub-install & lilo will run at the end, but only one will be suclearcase/" target="_blank" >ccess.
      It depends on source config.
   3. root partition "/" is dump by default and cannot be disable.
   4. this script can be used in linux with ext2/3 fs only.
   5. All partitions will be created and formated as described in fstab
   6. For compressed image size > 2G, please use nfs, otherwise smb is recommanded
   7. Raid is detected automactically.

   example command for mount nfs dir
     mount -t nfs 128.2.13.28:/opt3/ghost /mnt/ghost

   example command for mount smb dir
     mount -t smbfs -o username=YOURNAME //128.2.13.28/sebcg$ /mnt/ghost


change log:

  2004-10-00  Last Release
  2004-11-01  Add dumping log
  2004-11-15  use parted to replace fdisk and sfdisk
  2004-11-16  Add raid-1 support
              rewrite function that use stat tool
              rewirte partitioning script and is seperated to partition.sh
              PHY disk & Raid & partition auto detection
              exclude function enchanced
              lilo/grub auto detection
  2004-11-18  phy and mirror disk of raid1 must be defined manaully
  2004-11-20  fix bugs while grep mount point with similiar name
  2004-12-01  fix bugs when stat not found

原文转自:http://www.ltesting.net