#!/bin/ksh

# 2004-09-15 Tom Kranz <tom@siliconbunny.com>

# This script is distributed under the GNU Public License (GPL) with the
# following extra conditions:
# - attritbution must be maintained
# - CD-ROM or similar media for commercial distribution without the prior 
#   approval of the author

# Script to clone root disk
# Run this from cron nightly when things are quiet to keep so you have a
# reasonably up to date bootable clone of your root partition
#
# Assumptions:
#	- root is dks0d1s0
#	- cloner is dks0d2s0
#	- cloner's s0 is *at least* as big as root's
#
# DON'T JUST RELY ON THIS SCRIPT TO WORK! TEST BOOTING FROM YOUR CLONED
# ROOT DRIVE ON A REGULAR BASIS!

# Some vars
# Change as appropriate if your disk paths etc. are different
SOURCE=dks0d1s0
DEST=dks0d2s0
DISKTREE=/dev/dsk
MNTPOINT=/clone
SOURCEVH=/dev/rdsk/dks0d1vh
DESTVH=/dev/rdsk/dks0d2vh

# Just in case any cruft survives, we will make a new fs on the clone 
# each time
echo "Making new fs on ${DEST}"
mkfs -b size=4096 ${DISKTREE}/${DEST}

# We need somewhere to mount it
if [ ! -d ${MNTPOINT} ]
then
	echo ""
	echo "Making mount dir for clone disk"
	mkdir ${MNTPOINT} && echo "Created ${MNTPOINT}"
fi

# Now we mount
mount ${DISKTREE}/${DEST} ${MNTPOINT} && echo "Successfully mounted ${DEST}"

# Start our xfsdump/xfsrestore joy
cd ${MNTPOINT}
echo "Starting clone operation"
xfsdump -l 0 -p 5 - / | xfsrestore - .

# Umount our clone
cd /
umount ${MNTPOINT} && echo "Unmounted ${DEST}"

# Copying volume headers
cd /stand
dvhtool -v get sash sash ${SOURCEVH}
dvhtool -v get ide ide ${SOURCEVH}
dvhtool -v creat sash sash ${DESTVH}
dvhtool -v creat ide ide ${DESTVH}
dvhtool -v get symmon symmon ${SOURCEVH}
dvhtool -v create symmon symmon ${DESTVH}

# Done
echo "Clone operation finished"

exit 0
