#!/bin/sh
###############################################################################
#                                                                             #
#                             NightlyCVSBackup.sh                             #
#                                                                             #
###############################################################################
#                                                                             #
#  Description:     This script can be called from cron to automatically      #
#                   backup the CVS repository and email it to multiple        #
#                   recipients for a distributed backup solution.             #
#                                                                             #
#  Author:          Mark Zieg <mark@zieg.com>                                 #
#                                                                             #
#  Date:            Jan 28, 2003                                              #
#                                                                             #
###############################################################################

################################## globals ####################################


############################### Globals #######################################

# CVS info
CVSROOT=/usr/local/cvsroot

# binaries
BASE64_BIN=/usr/local/bin/base64

# email headers
TO="mark@zieg.com, someone.else@somewhere.com"
SUBJECT="Automated CVS backup"
FROM="mark@zieg.com"
BDY="----ATTACHMENT"

# base extension of scratch files
BASE="/tmp/NightlyCVSBackup.tmp"
TARBALL="$BASE.tbz"
BASEBALL="$BASE.b64"
TIMESTAMP=`date +%Y%m%d`
DATE=`date`
FILENAME="cvsroot-$TIMESTAMP.tbz"

# outgoing email message
MSG="$BASE.msg"

################################ main() #######################################

# init

# create tarball
cd /usr/local
tar jcf $TARBALL cvsroot

# base64 tarball
$BASE64_BIN -e < $TARBALL > $BASEBALL

# compose message
rm -f $MSG
touch $MSG
echo "To: $TO"                                    >> $MSG
echo "Subject: $SUBJECT"                          >> $MSG
echo "From: $FROM"                                >> $MSG
echo "Mime-Version: 1.0"                          >> $MSG
echo "Content-Type: multipart/mixed;"             >> $MSG
echo "  boundary=\"$BDY\""                        >> $MSG
echo ""                                           >> $MSG
echo "--$BDY"                                     >> $MSG
echo "Content-type: text/plain; "                 >> $MSG
echo "  charset=iso-8859-1"                       >> $MSG
echo "Content-transfer-encoding: 7BIT"            >> $MSG
echo ""                                           >> $MSG
echo ""                                           >> $MSG
echo "Automated CVS repository backup for $DATE"  >> $MSG
echo ""                                           >> $MSG
echo ""                                           >> $MSG
echo "--$BDY"                                     >> $MSG
echo "Content-type: application/octet-stream; "   >> $MSG
echo "  name=$FILENAME"                           >> $MSG
echo "Content-transfer-encoding: BASE64"          >> $MSG
echo "Content-disposition: attachment; "          >> $MSG
echo "  filename=$FILENAME"                       >> $MSG
echo ""                                           >> $MSG
cat $BASEBALL                                     >> $MSG
echo ""                                           >> $MSG
echo "--$BDY--"                                   >> $MSG
                                                  
# send message
sendmail -t < $MSG

# cleanup 
# rm -f $MSG $TARBALL $BASEBALL
