#!/bin/sh


# Backup Script for Dawn of Time Based MUD`s
# Written by Graham McKoen AKA -Balo-.
# of Stormbringer Mud stormbringer.sytes.net:4500
# http://www.stormbringer.sytes.net.
# Permission to Use, Modify and Distribute this script is freely granted
# although it would be greatley appriciated of this header is kept
# in tact.
# This Script is far from perfect ! use at your own risk, however suggestions
# for improvement and fixes are welcomed.
# e-mail balo@stormbringer.sytes.net .

#Do not set .. False is perposley a NULL string.
FALSE=
TRUE=1

#********************* Begin User Config *************************

# Set PREFIX to point at the Directory you run the MUD From

PREFIX="/usr/home/myhomedir/dawn/"

# Set Mudname to the name of your Mud (must be a single word) this will 
# be used in the name of the resulting backup file.

MUDNAME="mymud"


# Set BACKUPLOC to the directory you wish to create backps in ( this dir must already exist)
#for static backups set this dir to the place you wish to store the files localy
# When No BLOC dir is specified the script will automaticaly delete all local backup
# files generated.  

BACKUPLOC=$FALSE

# BACKUPLOC=/usr/home/myhomedir/dawn/mybackups

# USEFTP Must Be set to =$TRUE when BACKUPLOC or USEMAIL is not used, However you may use both options together
# to perform both a remote & a local backup.

USEFTP=$FALSE

#Specify the address (IP) of the ftp server you wish to send your backup too as well as the username and password
#the FTPDIR is used to change to a different directory on the remote server.
#FTPADDRESS, FTPUSER & FTPPASS must be set when USEFTP is enabled.

FTPADDRESS=ftp.mydomain.com
FTPUSER=MyUsername
FTPPASS=mypassword
# FTPDIR=/backups/

# Use E-mail as the Method to recieve your backups.
# Using this method requires that your server has sendmail installed and configured
# The method used here also requires that the popular mutt MTU program be installed on the
# server, however it should be relativley simple to change this script to work with others
# such as metamail , mpack or Z-mail for example. information on the mutt program cam be found
# at http://www.mutt.org/

# Set this option to $TRUE if you wish to ese the e-mail option (this may be used along side the 
# local backups option i.e BACKUPLOC is specified , however it is not recomended to set this option
# as well as the USEFTP option.
# note :- some of the files sent may be quite large, especial if the logs and src options are 
# specified, ensure that your server admin is happy with large files being sent via thier server
# and that you are capable of recieving large emal attachments.

USEEMAIL=$TRUE

# Message Subject Change as needed.

SUBJECT="${MUDNAME} Backup"

# Email address to send the backup to.

MAILTO=me@myeamail.addres.com



#Backup Options - Set these to either TRUE or FALSE depending wether you wish
#the associated directory and its files to be included in the backup.

#Uncomment this to do a Full Backup, This option overides all the folowing dir options!

#FULL="$TRUE"

# Root - these are the files in your dawn dir (usualy your gameset.txt) and are well worth including

ROOT="$TRUE"

# Area Files & areas.txt

AREA="$TRUE"

# Player Files

PLAYER="$TRUE"

# The Notes spool

NOTES="$TRUE"

# Races classes and many other mud specific files are kept here. 

SYSTEM="$TRUE"

# Your help files

HELP="$TRUE"

# Data Dir This holds the laston database etc (worth a backup)

DATA="$TRUE"

# All of the game logs, Note - thes files can get quite large , dont back up unless you realy need to.

LOGS="$FALSE"

# Your source code

SRC="$FALSE"

#  ******* Advanced options **********

# This is the format of the Date that is appended to the backup files

DATEFORM="`date +%b%d-%Y-%H.%M`"

# Ignorefiles , the list of files which are not to be backed up , note this affects all directories

IGNOREFILES="*~ *.o #* /.#* *.core *.tar *.gz *.tgz"

#*********************** End User config *****************************

## Set our backup directories

if [ -n "$FULL" ] || [ -n "$ROOT" ]
then
ROOTDIR="*.txt *.giobak"
ro="-ro"
fi
if [ -n "$FULL" ] || [ -n "$AREA" ]
then
AREADIR="area"
ar="-ar"
fi
if [ -n "$FULL" ] || [ -n "$PLAYER" ]
then
PLAYERDIR="player"
pl="-pl"
fi
if [ -n "$FULL" ] || [ -n "$NOTES" ]
then
NOTESDIR="notes"
no="-no"
fi
if [ -n "$FULL" ] || [ -n "$SYSTEM" ]
then
SYSTEMDIR="system"
sy="-sy"
fi
if [ -n "$FULL" ] || [ -n "$HELP" ]
then
HELPDIR="help"
he="-he"
fi
if [ -n "$FULL" ] || [ -n "$LOGS" ]
then
LOGSDIR="logs"
lo="-lo"
fi
if [ -n "$FULL" ] || [ -n "$DATA" ]
then
DATADIR="data"
da="-da"
fi
if [ -n "$FULL" ] || [ -n "$SRC" ]
then
SRCDIR=$TRUE
sr="-sr"
fi

## create the OPTIONS string for use in the filename

if [ -n "$FULL" ]
then
OPTIONS="-full"
else
OPTIONS="${ro}${ar}${pl}${sy}${no}${hl}${lo}${dat}${sr}"
fi

## create the list of files and directorys to be backed up

BACKUPLIST="$ROOTDIR $AREADIR $PLAYERDIR $NOTESDIR $HELPDIR $SYSTEMDIR $DATADIR $LOGSDIR $SRCDIR"


## Move To the Base location

cd ${PREFIX}/

## If using FTP set up the logs

if [ -n "$USEFTP" ]
then
echo "Using FTP Mode checking Logs Dir"
if [ -d logs ]
then
echo "found logs dir ok"
cd logs

if [ -d ftplog ]
then
echo "found ftplogs dir ok"
else
echo "creating ftplog dir in ${PREFIX}/logs"
mkdir ftplog
fi
LOG=${PREFIX}/logs/ftplog/ftplog-sucess.${DATEFORM}.txt
FLOG=${PREFIX}/logs/ftplog/ftplog-failed.${DATEFORM}.txt
else
echo "Error: Can not find logs or create ftplog dir Please check your PREFIX setting"
fi
cd ${PREFIX}/
fi 


if [ -n "$BACKUPLOC" ] 
then
echo "Local Backup Enabled"
echo "Local Mode creating ${BACKUPLOC}/${MUDNAME}-backup${OPTIONS}.${DATEFORM}.tar.gz"
gtar -cvzf ${BACKUPLOC}/${MUDNAME}-backup${OPTIONS}.${DATEFORM}.tar.gz --exclude '${IGNOREFILES}' ${BACKUPLIST}

    if [ -n "$USEEMAIL" ]
    then
    ATTFILE=${BACKUPLOC}/${MUDNAME}-backup${OPTIONS}.${DATEFORM}.tar.gz
    fi
else
echo "Remote Mode creating ${PREFIX}/${MUDNAME}-backup${OPTIONS}.${DATEFORM}.tar.gz"
echo "No Local File will be stored"
gtar -cvzf ${PREFIX}/${MUDNAME}-backup${OPTIONS}.${DATEFORM}.tar.gz --exclude '${IGNOREFILES}' ${BACKUPLIST}
    if [ -n "$USEEMAIL" ]
    then
    ATTFILE=${PREFIX}/${MUDNAME}-backup${OPTIONS}.${DATEFORM}.tar.gz
    fi
fi


if [ -n "$BACKUPLOC" ] 
then
cd ${BACKUPLOC}
echo "going to ${BACKUPLOC}"
else
cd ${PREFIX}
echo "going to ${PREFIX}"
fi

if [ -n "$USEFTP" ]
then

ftp -i -n -v ${FTPADDRESS} > $LOG 2> $FLOG << END_SCRIPT
user ${FTPUSER} ${FTPPASS}
binary
bell
cd ${FTPDIR}
put ${MUDNAME}-backup${OPTIONS}.${DATEFORM}.tar.gz
quit
END_SCRIPT

EXITSTATUS=$?

if [ $EXITSTATUS != "0" ]
then
echo " Error: Ftp Transfer Failed - check the ftplogs for details"
else
echo " File transfered sucsessfully via FTP"
fi 
else
echo "FTP mode is disabled"
fi

if [ -n "$USEEMAIL" ]
then
echo "Using E-Mail mode"
# A text message with a simple preface message
MSGTXT="Backup of ${MUDNAME} on ${DATEFORM} with the Following Options ${OPTIONS}"
# Quick and dirty hack, if you know a better way e-mail me balo@stormbringer.sytes.net
TEXTFILE=${PREFIX}/scripts/mailmsg.txt
echo "$MSGTXT" 1>$TEXTFILE
echo "sending backupfile ${MUDNAME}-backup${OPTIONS}.${DATEFORM}.tar.gz Via email to $MAILTO"
mutt -a $ATTFILE -s "$SUBJECT" $MAILTO < $TEXTFILE
echo "Sent! Plz check your mail in a few minutes"
echo "Removing Temporary Mail File $TEXTFILE"
rm $TEXTFILE
else
echo "E-mail mode is disabled"
fi

if [ -d ${PREFIX}/backuptmp ] 
then
echo "Deleting Temporary files & directories ${PREFIX}/backuptmp"
rm -fR ${PREFIX}/backuptmp
fi

if [ -n "$BACKUPLOC" ]
then
echo " Local mode Keeping file ${BLOC}/${MUDNAME}-backup${OPTIONS}.${DATEFORM}.tar.gz"
else
echo " Remote mode deleting temporary local backup file ${PREFIX}/${MUDNAME}-backup${OPTIONS}.${DATEFORM}.tar.gz"
rm ${PREFIX}/${MUDNAME}-backup${OPTIONS}.${DATEFORM}.tar.gz
fi

echo "All done Goodbye!"

#
#end of script
