首 页 ┆ 源码下载 ┆ IT学院 ┆ 字体下载 ┆ 模板下载 ┆ 源码发布 ┆ 广告合作 ┆ 网站地图
► 设为首页
► 加入收藏
► 联系我们
源码下载 >> ASP源码 | PHP源码 | ASP.net源码 | JSP源码 | CGI源码 | VC/C++源码 | VB源码 | Delphi源码 | Flash源码
文章学院 >> 网络编程 | 网页设计 | 图形图象 | 数据库 | 服务器 | 网络媒体 | 网络安全 | 操作系统 | 办公软件 | 软件开发 | 黑客知识
字体下载 >> 精制字体 | 非英字体 | 艺术字体 | 著名字体 | 哥特式 | 简单字体 | 手写体 | 节假日 | 图案字体 | 精度像素 | 中文字体
模板下载 >> 企业门户 | 数码网络 | 休闲娱乐 | 影视音乐 | 旅游名胜 | 文化艺术 | 电子商务 | 个性展示 | 登陆导航 | Flash模板
►►您当前的位置:源码园 → IT学院 → 操作系统 → Linux → 文章内容

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

作者:佚名  来源:网上收集  发布时间:2006-5-22 18:16:54
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 - 20041201From: 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 success. 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

[] [返回上一页] [打 印]
  • 上一篇文章:Linux字体安装与简单美化(实践篇)
  • 下一篇文章:选择设置好ext3日志模式

  • 相关文章:
  • 脚本欣赏----Shell Script to Clone Linux System -...
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 源码发布
Copyright © 2003-2009 Ymyasp.Com. All Rights Reserved .
备案序号:粤ICP备07029071号