LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1575|回复: 6

乾坤大挪移!

[复制链接]
发表于 2003-11-23 03:06:58 | 显示全部楼层 |阅读模式
最进换了个新硬盘,所以要将mdk大迁移。我一直都用partimage来clone我的系统,算是蛮好用的。我的mdk9.1原来在一个4G的分区上,大约3G大小,clone完的镜像可以装在两张700M的光盘上。新的硬盘我划了30G给mdk,还原之后出了问题,我现在只能在新的分区上用到4G!如果fdisk -l,我看到分区是30G,但如果df -h,则显示只有4G。本来我还没注意呢,后来是在下mdk9.2iso的时候突然发现硬盘满了。苦恼之余,在Gentoo forum里发现了这个script。Code:

#!/bin/bash
# This script will clone the root partition of your gentoo system to a selected
# partition and create the appropriate fstab file for the cloned partition

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

# Change these variables according to your prefs, for CLONE_DIR always use a path
# like /dir1/dir2, never something like /dir or /dir1/dir2/dir3...
# CLONE_DIR either have to be defined in /etc/fstab, or mounted...
# BIND_DIR is a directory to mount - bind your root partition (it is needed for your
# gentoo system which uses devfs - otherwise /dev will not be correctly copied...
# If you do not want to change the fstab file for your cloned partition, set
# FSTAB_FLAG to 0...

BIND_DIR=/mnt/oldroot
CLONE_DIR=/mnt/clone
FSTAB_FLAG=1

#################################################################################
# Do not change these:
SOURCE_DIR=/   
CREATE_BIND_DIR=0
MOUNT_CLONE_DIR=0

###################### Subroutines ##############################################
#_____________________________________________________________________________
Check_params()

# Checks whether your destination partition is mounted or exist in /etc/fstab
# and checks for the need to create mount points
{
   SOURCE_PART=`grep "$SOURCE_DIR\ " /etc/mtab | awk  '{ print $1 }'`
   grep "$CLONE_DIR\ " /etc/mtab &> /dev/null
   if [ $? -ne 0 ]
      then
         grep "^[\ \t]*.*$CLONE_DIR\ " /etc/fstab &> /dev/null
         if [ $? -ne 0 ]
         then
            echo
            echo $CLONE_DIR is not listed in /etc/fstab as a valid mount point. Please either
            echo list it in your fstab, so it can be automatically mounted, or mount a partition
            echo on it !
            echo
            Usage
            exit 1
         else
            CLONED_PART=`grep "^[\ \t]*.*$CLONE_DIR" /etc/fstab | awk  '{ print $1 }'`
            if [ ! -d $CLONE_DIR ]
               then
                  mkdir $CLONE_DIR
            fi   
            MOUNT_CLONE_DIR=1
         fi
      else CLONED_PART=`grep "$CLONE_DIR" /etc/mtab | awk  '{ print $1 }'`
   fi
   if [ ! -d $BIND_DIR ]
      then
         CREATE_BIND_DIR=1
   fi   
}
#_____________________________________________________________________________
Usage() # Prints some help
{
   echo
   echo "############## gent-clone help #####################"
   echo
   echo "This script will clone your gentoo root partition to "
   echo "another partition. Usage:"
   echo
   echo "gent-clone mode [destination mount point]"
   echo
   echo 'where mode = "-c"  Clones current root partition to a destination partition'
   echo '           = "-p"  Prentend cloning; useful to see what would happen without any changes'
   echo '           = "-h"  Prints this message...'
   echo  
   echo 'Destination mount point should be given as /dir1/dir2 or /dir format, if it is not supplied, '
   echo 'then the program uses internal defaults (can be changed at the beginning of the script).'
   echo 'A partition should be attached to the destination directory or the mount point  listed in'
   echo '/etc/fstab before using the script! '
   echo
   echo 'Normally, the /etc/fstab file on your root partition will be edited by the script:'
   echo 'A new adjusted /etc/fstab is created and copied over - this contains your destination'
   echo 'partition mounted as root. If you do not want this behavior, set the FSTAB_FLAG variable'
   echo 'from 1 to 0 by editing the first part of this script.'
   echo
   echo
   echo 'examples: gent-clone -c /mnt/clone - clone you root partititon to the partition mounted on /mnt/clone '
   echo '        : gent-clone -c            - clone your current root partition to your predefinied'
   echo '                                     partition (set this up by editing the first part of the script)'
   echo '        : gent-clone -p            - Pretend mode: Prints out what will happen when you issue a -c argument'
}

#_______________________________________________________________________
Config() # Create /etc/fstab for cloned partition
{
cp /etc/fstab /etc/fstab.cloned

SLICE_CLONED=`echo $CLONED_PART | cut -d "/" -f3`   
SLICE_SOURCE=`echo $SOURCE_PART | cut -d "/" -f3`   

SED_FROM='^[\ \t]*\/dev\/'$SLICE_CLONED
SED_TO='\/dev\/clonepart'
cat /etc/fstab.cloned | sed -e "s/$SED_FROM/$SED_TO/" > /etc/fstab.cloned

SED_FROM='^[\ \t]*\/dev\/'$SLICE_SOURCE
SED_TO='\/dev\/rootpart'
cat /etc/fstab.cloned | sed -e "s/$SED_FROM/$SED_TO/" > /etc/fstab.cloned

SED_FROM='^\/dev\/clonepart'
SED_TO='\/dev\/'$SLICE_SOURCE
cat /etc/fstab.cloned | sed -e "s/$SED_FROM/$SED_TO/" > /etc/fstab.cloned  

SED_FROM='^\/dev\/rootpart'
SED_TO='\/dev\/'$SLICE_CLONED
cat /etc/fstab.cloned | sed -e "s/$SED_FROM/$SED_TO/" > /etc/fstab.cloned
cp /etc/fstab.cloned $CLONE_DIR/etc/fstab

}
#_________________________________________________________________________
Clone() # Copies $SOURCE_PART to CLONED_PART
{
      if [ $CREATE_BIND_DIR -eq "1" ]
      then
         mkdir $BIND_DIR
      fi
      if [ $MOUNT_CLONE_DIR -eq "1" ]
      then
         mount $CLONE_DIR
      fi
      mount --bind $SOURCE_DIR $BIND_DIR
      rm -rf $CLONE_DIR/*
      cd $BIND_DIR
      find -mount -print | cpio -pdm $CLONE_DIR
      if [ $FSTAB_FLAG -eq "1" ]
      then
         Config
      fi
      echo
      echo "All done. Partition $SOURCE_PART was cloned to partition $CLONED_PART."
}
#____________________________________________________
Report() # What will happen; Used in pretend mode...
{
      echo

      if [ $CREATE_BIND_DIR -eq "1" ]
      then
         echo Creating $BIND_DIR...
      fi

      if [ $MOUNT_CLONE_DIR -eq "1" ]
      then
         echo Mounting $CLONED_PART on $CLONE_DIR...
      fi

      echo 'Mounting '$SOURCE_DIR' ('$SOURCE_PART') with -bind on '$BIND_DIR'...'
      echo 'Deleting all files in '$CLONE_DIR' ('$CLONED_PART')...'
      echo 'Changing dir to '$BIND_DIR'...'
      echo 'Copying everything in '$BIND_DIR' ('$SOURCE_PART') to '$CLONE_DIR' ('$CLONED_PART')...'
      echo

      if [ $FSTAB_FLAG -eq "1" ]
      then
         echo "Creating and copying a modified fstab, where $CLONED_PART is mounted as / ..."
      fi
}

######################### Main Program Module ##################################

case "$#" in
0)   
   Usage
   exit 1
   ;;
1)
   case "$1" in
   -p)
      Check_params
      Report
      exit 0
      ;;
   -c)
      Check_params
      Clone
      exit 0
      ;;
   -h)
      Usage
      exit 0
      ;;
   *)
      echo
      echo "Unknown parameter..."
      echo
      Usage
      exit 1
      ;;
   esac
   ;;
2)
   case "$1" in
   -p)
      CLONE_DIR=$2
      Check_params
      Report
      exit 0
      ;;
   -c)
      CLONE_DIR=$2
      Check_params
      Clone
      exit 0
      ;;
   -h)
      echo
      echo 'The "-h" parameter cannot be used with additional arguments...'
      echo
      Usage
      exit 1
      ;;
   *)
      echo
      echo "Unknown parameter..."
      echo
      Usage
      exit 1
      ;;
   esac
   ;;
*)
   echo
   echo 'This 2 is the maximum number of params!'
   echo
   Usage
   exit 1
   ;;
esac

############### End #####################################
存档为foo-clone(或是mdk-clone,随自己喜欢),然后敲foo-clone -h看怎么用。
(原贴:http://forums.gentoo.org/viewtop ... t=clone&start=0)
好在我新硬盘的分区多,结果搬运成功!
当然搬完之后要改一下fstab,lilo.conf或grub.conf啦。

另外发现一篇mdk软件管理的文章,值得推荐:http://www.zebulon.org.uk/ICML0.HTML
发表于 2003-11-23 17:06:54 | 显示全部楼层
暂时用不着,不过建议版主加精,便于日后查找方便。
发表于 2003-11-23 17:24:47 | 显示全部楼层
很不错;)
发表于 2003-11-24 09:34:44 | 显示全部楼层
为什么不用tar,方便简单,而且很实用。
 楼主| 发表于 2003-11-24 21:58:50 | 显示全部楼层
You mean something like this?
Code:
cd /
tar cplf - | (cd <destination_partition>; tar xvpf -)
发表于 2003-12-1 16:09:11 | 显示全部楼层
是哦,而且根本不用那么多的脚本。
发表于 2003-12-9 02:32:16 | 显示全部楼层
I think a better way to do this is to boot your machine with Knoppix CD, mount relevant partitions, and then do:
cp -a /mnt/old /mnt/new
It works for me.  I don’t think it’s a good idea to copy everything from inside a running system; lots of things don’t get copied properly (such as /dev), some things that should not be copied may get copied (such as /proc).
On the other hand, if you use tar with pipe like above, the tar program actually packages things into memory before it extracts them onto the new partition.  This may cause problems when the package gets too big (such as this partition moving thing would be the case) and your memory may not be sufficient (do you have more than 1G ram?), the whole process may break down.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表