Skip to content

Instantly share code, notes, and snippets.

@andreapavoni
Created December 4, 2014 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreapavoni/3cdb7635a7e1122c8cb9 to your computer and use it in GitHub Desktop.
Save andreapavoni/3cdb7635a7e1122c8cb9 to your computer and use it in GitHub Desktop.
Gentoo cloner: old utility to clone gentoo installation on twin machines
#!/bin/bash
# GentooCloner 0.5.1_alpha - clone your gentoo on identical/similar machines or just do a stage4 :)
AUTHOR="Copyright (c) 2006 Andrea Pavoni (http://andreapavoni.com)"
LICENSE="
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by Andrea Pavoni.
4. The name Andrea Pavoni or teknux may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
"
##### BEGIN USER CONFIGURATION ####################################
# which disk to be considered for partition table infos
DISK=/dev/sda
# name of the output file
OUT_NAME="stage4-`hostname`-`date +\%Y\%m\%d`"
# optional files/paths to be excluded (syntax examples: /dir1/:dir2/*:/path/file)
OPT_EXCLUDE_LIST="/data/*:/usr/src/*:/home/*/*:/usr/portage/*:/var/log/*/*:/root/*"
# this is your root-mount-point where to restore the system
# if you're using a gentoo live-cd keep this setting :)
RESTORE_LOCATION=/mnt/gentoo/
##### END USER CONFIGURATION #######################################
dump_stage4 ()
{
LOCATION=$1
#telinit 1
echo "Creating $LOCATION directory"
mkdir -p $LOCATION
# copy this script in $LOCATION for future restoring
cp $0 $LOCATION
echo "Saving partition table "
# backup patition table info
dd if=$DISK of=$LOCATION/mbr.save count=1 bs=446
/sbin/sfdisk -d $DISK > $LOCATION/hdtable.save
# backup filesystem info, first one is the root ('/') dir
grep $DISK /etc/fstab | awk '{ print $2"\t"$1"\t"$3 }' | sort >> $LOCATION/fs.save
# exclude some default paths
DEFAULT_EXCLUDE_LIST="/tmp/*:/var/tmp/*:/dev/*:/proc/*:/mnt/*:/sys/*:$LOCATION/*"
EXCLUDE_LIST=`get_exclude_list`
echo "Creating $OUT_NAME in $LOCATION"
tar cjvpf $LOCATION/$OUT_NAME.tar.bz2 $EXCLUDE_LIST / 1>$LOCATION/$OUT_NAME.log 2>&1
printf "\n\nDONE\n"
exit 0
}
restore_stage4 ()
{
LOCATION=$1
echo "Restoring $DISK partition table"
dd if=mbr.save of=$DISK
sfdisk $DISK < $LOCATION/hdtable.save
echo "Rebuilding filesystems"
for i in $(grep reiserfs $LOCATION/fs.save | awk '{ print $2 }') ; do
mkreiserfs -f -f $i
done
for i in $(grep ext3 $LOCATION/fs.save | awk '{ print $2 }') ; do
mkfs.ext3 $i
done
SWAP=`grep swap $LOCATION/fs.save | awk '{ print $2 }'`
mkswap $SWAP && swapon $SWAP
# mount the root partition
mount `get_root_partition $LOCATION` $RESTORE_LOCATION
echo "Creating dir(s) and mounting them"
for i in $(grep $DISK $LOCATION/fs.save | awk '{print $1}'| egrep '/.'| sed -e 's/\///g') ; do
mkdir -p $RESTORE_LOCATION/$i
echo "$i"
mount `grep $i $LOCATION/fs.save | awk '{print $2}'` $RESTORE_LOCATION/$i
done
echo "Restoring stage4 from `ls $LOCATION/*.tar.bz2` to $RESTORE_LOCATION"
tar xfjvp `ls $LOCATION/*.tar.bz2` -C $RESTORE_LOCATION
cp -Rp /dev/* $RESTORE_LOCATION/dev/
printf "\n\nDONE\n"
exit 0
}
# discover where is the / (root) partition
get_root_partition ()
{
LOCATION=$1
LINE_N=`grep $DISK $LOCATION/fs.save | awk '{ print $1 }' | egrep -n ^/$ | cut -d':' -f1`
FS_LINES=`wc -l $LOCATION/fs.save | awk '{ print $1 }'`
echo `cat $LOCATION/fs.save | head -n$FS_LINES | tail -n1 | awk '{ print $2 }'`
}
# handle the (default/optional)-exclude-list of files and dirs
get_exclude_list ()
{
EXCL_TMP=""
FINAL=$DEFAULT_EXCLUDE_LIST:$OPT_EXCLUDE_LIST
for i in $(echo $FINAL | awk '{ split($1,path,":") ;for(p in path){if(path[p]!=""){print " --exclude="path[p]}}}') ; do
EXCL_TMP="$EXCL_TMP $i"
done
echo `for i in $EXCL_TMP ; do echo -n "$i " ; done`
}
usage ()
{
echo "Usage:"
printf "\t$0 dump WHERE_TO_SAVE_STAGE4_PATH\n\n"
printf "\t$0 restore WHERE_TO_READ_STAGE4_PATH\n\n"
exit -1
}
if [ $# != 2 ] ; then
usage
fi
case "$1" in
dump ) dump_stage4 "$2";;
restore ) restore_stage4 "$2";;
* ) usage;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment