Skip to content

Instantly share code, notes, and snippets.

@acidprime
Created December 22, 2011 04:59
Show Gist options
  • Save acidprime/1508970 to your computer and use it in GitHub Desktop.
Save acidprime/1508970 to your computer and use it in GitHub Desktop.
Deploy Studio Users Extract Example
#!/bin/bash
# Commands used by this script
declare -x asr="/usr/sbin/asr"
declare -x awk='/usr/bin/awk'
declare -x hdiutil='/usr/bin/hdiutil'
declare -x rm="/bin/rm"
declare -x mv="/bin/mv"
# Check any commands are included on the .nbi
for COMMAND in $asr $awk $hdutil $rm ; do
if [ ! -x "$COMMAND" ] ; then
echo "Missing command: $COMMAND"
exit 1
fi
done
echo "Found DS_LAST_CREATED_DISK_IMAGE_NAME: $DS_LAST_CREATED_DISK_IMAGE_NAME"
declare -x FULL_DMG="$DS_REPOSITORY_PATH/Masters/HFS/$DS_LAST_CREATED_DISK_IMAGE_NAME.hfs.dmg"
echo "Compiled Disk Image Path: $FULL_DMG"
# Deploy Studio creates this move job for the dmg
declare -x MOVE_JOB="$DS_REPOSITORY_PATH/Masters/tmp/.dss.move.$DS_LAST_CREATED_DISK_IMAGE_NAME.hfs.dmg"
# Check to see if deploy studio move job is still happening
if [ -f "$MOVE_JOB" ] ; then
echo "Found Move Job: $MOVE_JOB"
until [ ! -f "$MOVE_JOB" ] ; do
let N++
if [ $N -gt 3600 ] ; then
echo "Timed out waiting for server to move image"
exit 1
fi
echo "Waiting for server to move image..."
sleep 1
done
else
echo "No Server side move found"
fi
# Make sure file is there and var is set :?
if [ ! -f "${FULL_DMG:?}" ] ; then
echo "Missing disk image file: $FULL_DMG"
exit 1
fi
declare -x SHADOW_FILE="${FULL_DMG%%.dmg}.shadow"
echo "Using Shadow File: $SHADOW_FILE"
echo "Mounting the full dmg read & write..."
declare -x SHADOW_MOUNT="`$hdiutil attach \
-owners on \
-noverify "${FULL_DMG:?}" \
-shadow "$SHADOW_FILE" 2>/dev/null |
$awk -F'/Volumes/' '/\/Volumes\//{print "/Volumes/"$NF}'`"
echo "Found Shadow Mount: $SHADOW_MOUNT"
# Sanity check
[ "$SHADOW_MOUNT" = '/' ] && exit 1
[ "$SHADOW_MOUNT" = '/Volumes' ] && exit 1
[ "$SHADOW_MOUNT" = '/Volumes/' ] && exit 1
IFS=$'\n'
# Delete the files except for /Users
echo "Cleaning disk image of System data..."
for FILE in "${SHADOW_MOUNT:?}"/* ; do
if [ "$FILE" == "$SHADOW_MOUNT/Users" ] ; then
echo "Skipping $FILE"
else
$rm -fr "$FILE"
fi
done
# Rename
echo "Saving Disk Image with only Home Folder"
declare -x NEW_USERS_DMG="${FULL_DMG%%.full.i386.hfs.dmg}.users.i386.hfs.dmg"
if [ "$NEW_USERS_DMG" == "$FULL_DMG" ] ; then
echo "Image name incorrect: $NEW_USERS_DMG"
exit 1
fi
# Create the new disk image
echo "Will create new disk image: $NEW_USERS_DMG"
if [ -f "$NEW_USERS_DMG" ] ; then
echo "Resultant disk image already exists: $NEW_USERS_DMG"
$mv "$NEW_USERS_DMG" "${NEW_USERS_DMG%%.dmg}.old.$$$RANDOM.dmg"
fi
# Update the disk
$hdiutil convert \
-format UDZO "$FULL_DMG" \
-shadow "$SHADOW_FILE" \
-o "${NEW_USERS_DMG:?}"
# Check if file was created
if [ -f "$NEW_USERS_DMG" ] ; then
echo "Success created users delta dmg: $NEW_USERS_DMG"
echo "Scanning image good measure"
$asr imagescan --source "$NEW_USERS_DMG"
exit 0
else
echo "Failure users delta dmg not found: $NEW_USERS_DMG"
exit 1
fi
@acidprime
Copy link
Author

So this is kind of a weird one, I was using this prior to asr not supporting file copy mode (lion) to extract users from dmg backups of users. The idea was we needed to have full backups (names username.full.dmg) but then we wanted username.users.dmg that could then be restored after a full upgrade restore. So if you put this script after your "make a master" workflow it would extract the users folder from your dmg and create a dmg in the same folder that could be graphically selected in the "restore a master" work flow. I did this with net boot as it takes a long time to backup full images of machines and shadow files enabled me to copy the same data on the final storage device , which in my case was high speed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment