Tuesday, March 1, 2011

Add-on to copy changed images for the previous script

Integrate this into my previous script or run it standalone (I run it standalone as a separate LaunchDaemon). This compares the auto generated image with an image with the same name in DeployStudios "Masters" directory, and if checksum is different, it renames the image in the Masters directory and copies the new one. Very simple, but wanted to share it more like an idea than a script itself.

#!/bin/bash

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

DSSDIR="/Shared Items/dsrepo/Masters/HFS"
INSTADIR="/Work area/InstaDMG/10.6/OutputFiles"
DATESTAMP=$(date "+%y%m%d")
LOGF="/var/log/lnu-copyimages.log"

# Loop that searches $INSTADIR for files ending with .dmg
for i in $(find "$INSTADIR" -type f -print | egrep -i '.dmg$')
do
echo -e "Working on image: $i" >> "$LOGF"
FILENAME=$(basename $i)
if [ -f "$DSSDIR/$FILENAME" ]
then
echo -e "Found the image \"$FILENAME\" in \"$DSSDIR\"\nwith the same name as in \"$INSTADIR\"" >> "$LOGF"
echo -e "Checksumming both files and comparing them" >> "$LOGF"
DSSSHA=$(shasum "$DSSDIR/$FILENAME" | awk '{print $1}')
INSTASHA=$(shasum "$i" | awk '{print $1}')
if [ $INSTASHA = $DSSSHA ]
then
echo -e "The image \"$FILENAME\" is the same in \"$DSSDIR\"\nas in \"$INSTADIR\". Moving on." >> "$LOGF"
else
echo -e "The image \"$FILENAME\" in \"$DSSDIR\" has a different checksum.\nRenaming \"$FILENAME\" in \"$DSSDIR\"\nand copying \"$FILENAME\" from\n\"$INSTADIR\" to \"$DSSDIR\"" >> "$LOGF"
mv "$DSSDIR/$FILENAME" "$DSSDIR/$FILENAME-disabled-$DATESTAMP"
cp "$i" "$DSSDIR/"
fi
else
echo -e "Did not find an image called \"$FILENAME\" in \"$DSSDIR\"\nCopying \"$FILENAME\" to \"$DSSDIR\"" >> "$LOGF"
cp "$i" "$DSSDIR/"
fi
done

IFS=$SAVEIFS

exit 0

Can also be downloaded here: http://db.tt/Gr0qYkI