#!/bin/sh -
#
# backupvol Version: 20040507/01
#
# Copyright, all rights reserved by cilly @ http://www.cilly.dyndns.org/
#
PATH=/bin:/sbin:/usr/sbin:/usr/bin
#
# input variables
#
###################
SOURCE="${1}"
DEST="${2}"
VOLUME="${3}"
# if source is not given print syntax and exit
if [ -z "${SOURCE}" ]; then
echo ""
echo "***** Usage: $0 source destination name *****"
echo ""
exit 1
fi
if [ -z "${DEST}" ]; then
# if destination is not given use the current working dir
echo -n "The current working directory will be used "
echo "for the temporary and final Image ..."
DEST="./"
fi
if [ -z "$VOLUME" ]; then
# if volume is not given use untitled
VOLUME="Untitled"
fi
VERSION="`date '+%Y%m%d'`"
NAME="${VOLUME}_${VERSION}"
########################################
# set the filename and check if it exists
#
FILE="${DEST}/${NAME}.dmg"
if [ -e "${FILE}" ] ; then
echo ""
echo "***** "${FILE}" already exists! *****"
echo ""
exit 2
fi
# make sure the destinatino directory exists
#
mkdir -p "${DEST}"
# set tempfile
#
DEST="${TMP:-$DEST}"
TMPFILE="${DEST}/$_Temporary.sparseimage"
# create image
#
echo "Creating "${TMPFILE}" from "${SOURCE}" ..."
hdiutil create -type SPARSE -size 250G -fs HFS+ "${TMPFILE}" -volname "${NAME}"
if [ $? -ne 0 ] ; then
echo ""
echo "***** Error while creating image ... *****"
echo ""
rm -f "${TMPFILE}"
exit 5
fi
# setup the image
#
DEVICES=`hdid "${TMPFILE}"`
if [ $? -ne 0 ] ; then
rm -f "${TMPFILE}"
echo ""
echo "***** Error, image not mountable! *****"
echo ""
exit 6
fi
DEVMASTER=`echo $DEVICES| awk '{ print $1 }'`
# use apple software restore to backup volume
#
asr -source "${SOURCE}" -target /Volumes/"${NAME}" -erase -noprompt
if [ $? -ne 0 ]; then
echo ""
echo -n "***** Error while copying or the source is not a volume, "
echo "ejecting "${DEVMASTER}" and deleting "${TMPFILE}" ... *****"
echo ""
hdiutil eject "${DEVMASTER}"
rm -f "${TMPFILE}"
exit 9
fi
# uncheck the option "Ignore privileges"
#
DEVHFS=`echo $DEVICES| awk '{ print $5 }'`
disktool -A `echo $DEVHFS| sed -e 's,/dev/,,'`
if [ $? -ne 0 ]; then
echo ""
echo -n "***** Error on enabling permissions, "
echo "ejecting "${DEVMASTER}" and deleting "${TMPFILE}" ... *****"
echo ""
hdiutil eject "${DEVMASTER}"
rm -f "${TMPFILE}"
exit 7
fi
# if source is root bless system folder
#
BLESS="/Volumes/${NAME}/System/Library/CoreServices"
if [ -e "${BLESS}" ] ; then
echo "Blessing the Volume "${NAME}" and creating the BootX file..."
#
bless -folder /Volumes/"${NAME}"/System/Library/CoreServices \
-bootinfo /usr/standalone/ppc/bootx.bootinfo
if [ $? -ne 0 ] ; then
echo ""
echo "***** Error on blessing, will leave unblessed ... *****"
echo ""
fi
fi
# deleting unneccessary files, just do it and don't throw errors...
#
echo "Deleting unneccessary files ..."
#
rm -f /Volumes/"${NAME}"/var/db/volinfo.database
rm -f /Volumes/"${NAME}"/var/db/BootCache.playlist
rm -f /Volumes/"${NAME}"/var/vm/swapfile*
rm -f /Volumes/"${NAME}"/.DS_Store
rm -rf /Volumes/"${NAME}"/private/tmp/*
rm -rf /Volumes/"${NAME}"/private/tmp/.*
rm -rf /Volumes/"${NAME}"/"Temporary Items"/*
rm -rf /Volumes/"${NAME}"/"Temporary Items"/.*
rm -rf /Volumes/"${NAME}"/.Trashes/*
rm -rf /Volumes/"${NAME}"/.Trashes/.*
rm -rf /Volumes/"${NAME}"/Trash/*
rm -rf /Volumes/"${NAME}"/Trash/.*
rm -f /Volumes/"${NAME}"/.FBCIndex
rm -rf /Volumes/"${NAME}"/.FBCLockFolder
# ejecting ...
#
hdiutil eject "${DEVMASTER}"
if [ $? -ne 0 ]; then
rm -f "${TMPFILE}"
echo ""
echo " ***** Error while ejecting "${DEVMASTER}" *****"
echo ""
exit 10
fi
# convert to compressed image and write output to file
#
echo "Compressing "${NAME}" to "${FILE}" ..."
#
hdiutil convert "${TMPFILE}" -format UDZO -o "${FILE}"
#hdiutil convert "${TMPFILE}" -format UDZO -imagekey zlib-level=9 -o "${FILE}"
#hdiutil convert "${TMPFILE}" -format UDZO \
# -imagekey zlib-level=9 -o "${FILE}" -segmentSize 695m
if [ $? -ne 0 ]; then
rm -f "${FILE}"
mv "${TMPFILE}" "${FILE}"
echo ""
echo -n "***** Compression failed. "
echo "The image "${FILE}" not compressed. *****"
echo ""
exit 11
fi
# remake Tempfile ...
#
rm -f "${TMPFILE}"
#
# end
#