diskBackup.sh

Currently this is in Korn shell. I need to get rid of the "[[" test, but need something for the NetBSD kernel test. This has been tested on Solaris 7 and NetBSD1.6N. Note that it will *not* work on NetBSD1.6H (the regular NetBSD on a cd) printed Sep 02. There was a kernel bug that has been fixed. The bug caused problems with dump/restore.

#!/bin/ksh

# This assumes the filessystems are already created
# and prepared. If not, use format/disklabel/fdisk and newfs to do so.

# Need to get away from the ksh "[[" ism.

## Set the Variables
 
PATH=/usr/sbin:/usr/bin:/sbin:/bin

# Count the input variables
if [ "$#" -ne 2 ]
then
        echo "Need \$1 (PRIMARY_DISK) and \$2 (ALTERNATE_DISK)"
        echo "$0 c0t1d0 c0t3d0"
        echo "$0 sd0 sd1"
        exit 1
fi

OS_BASE=`uname -s`
OS_VER=`uname -r`

if [ "$OS_BASE" = "NetBSD"  ] 
then
	if [[ "$OS_VER" > "1.6H" ]]
	then
		echo "You can use dump, it is okay."
	else
		echo "Sorry, use a non-dump program."
		exit 1
	fi

	FSTAB=/etc/fstab
	DEV=/dev/
	RDEV=/dev/r
	BOOTBLK=/usr/mdec/bootblk
	BOOTBLKPART=a
	DUMP=dump
	RESTORE=restore
	DUMPARGS="-0f"

elif [ "$OS_BASE" = "SunOS" ]
then
	FSTAB=/etc/vfstab
	DEV=/dev/dsk/
	RDEV=/dev/rdsk/
	BOOTBLK=/usr/platform/`uname -m`/lib/fs/ufs/bootblk
	BOOTBLKPART=s0
	DUMP=ufsdump
	RESTORE=ufsrestore
	DUMPARGS=0uf

fi 


# Set the disks.
 
PRIDISK=$1
ALTDISK=$2

# Make sure the disks are valid

for i in $PRIDISK $ALTDISK
do
	if ( ! ls ${DEV}/${i}* > /dev/null 2>&1 ) 
	then
		echo "$i is invalid."
		exit 1
	fi
done



## Functions

# Make sure we're not copying onto a mounted disk.

sanityCheck() {
# NOTE: On the old one SunOS expects an "s" before $SLICE.
# make sure this still works there, too.

if ( df -k | grep ${ALTDISK}${SLICE} )
then
	echo "Are you sure about ${ALTDISK}${SLICE}? It is mounted."
	exit 1
fi
}

# Need to make sure FSTAB is changed to the ALTDISK partitions
editFstab() {
if [ -f /mnt${FSTAB} ]
then	
	sed s/$PRIDISK/$ALTDISK/g $FSTAB > /mnt$FSTAB
fi
}

# fsck the slices before they're written to.
fsckSlice() {
count=0
while [ $count -lt 2 ]
do
	fsck -y $1 
	count=`expr $count + 1`
done
}

installBoot() {
if [ -x /usr/sbin/installboot -a -f $BOOTBLK ]
then
	case $OS_VER in 
	SunOS)	/usr/sbin/installboot $BOOTBLK  ${RDEV}${ALTDISK}${BOOTBLKPART}
		;;
	NetBSD)	/usr/sbin/installboot ${RDEV}${ALTDISK}${BOOTBLKPART} $BOOTBLK 
		;;
	esac
fi
}

### Main

# The slice loop.
for i in `grep "^${DEV}" ${FSTAB} | awk '{ print $1 }'`
do
	PRISLICE=`basename $i`
	SLICE=`echo $PRISLICE | sed s#${PRIDISK}##`	

	sanityCheck
	fsckSlice ${RDEV}${ALTDISK}${SLICE}

## Do the ufsdump work

	if ( mount ${DEV}${ALTDISK}${SLICE} /mnt ) 
	then
		$DUMP $DUMPARGS - ${RDEV}${PRIDISK}${SLICE} | ( cd /mnt; $RESTORE rvf - )
		editFstab
		umount /mnt
	fi
done

### Clean up
sync;sync

installBoot
sync; sync