#	@(#)	fy_backup by Joe Thielen
#	@(#)	Started 04/10/2000, Last Modified 04/14/2000
#	@(#)	Text-Console tool
#	@(#)	Complete file AND data backup for Freemed-YiRC.

# IMPORTANT NOTE - IMPORTANT NOTE - IMPORTANT NOTE
#
# If the database type is set to 'psql', then the user who owns the database
# MUST RUN THIS BACKUP SCRIPT.  For instance, if the fydb_user in 
# freemed_yirc-vars.inc is set to 'postgres', then the user 'postgres' must 
# run this script!!!  The PostgreSQL database dump program DOES NOT ALLOW
# a user to log in as someone else (like MySQL can).  THEREFORE... great care
# must be taken to insure that this user has COMPLETE access to EVERY FILE
# and SUBDIRECTORY in the Freemed-YiRC directory structure or else some part
# of the backup may FAIL!
#
# IMPORTANT NOTE - IMPORTANT NOTE - IMPORTANT NOTE

todaydate=`date +%m``date +%d``date +%Y`
bkupfile="fy_file_bkup_""$todaydate"
bkupdata="fy_data_bkup_""$todaydate"
errorlog="/tmp/fy_bkup_errorlog.log"

# MAKE SURE that freemed_yirc-vars.inc is in the current directory!!!
if test -f freemed_yirc-vars.inc
then :
else
	echo ""
	error=`date`
	error=$error":FATAL ERROR:  Cannot find the file 'freemed_yirc-vars.inc'!!!"
	echo $error
	echo $error 1>>$errorlog 2>>$errorlog
	echo ""
	exit
	exit
fi

# GET the SQL Database Type (psql or mysql) from freemed_yirc-vars.inc
sqlprogram=`cat freemed_yirc-vars.inc | grep "fydb_type" | grep -v "#"`
sqlprogram=`echo $sqlprogram | cut -d= -f2 | tr -d "\"" | tr -d ";" | tr -d " "`
# GET the SQL Database Name (usually freemed_yirc) from freemed_yirc-vars.inc
sqldbname=`cat freemed_yirc-vars.inc | grep "fydb_name" | grep -v "#"`
sqldbname=`echo $sqldbname | cut -d= -f2 | tr -d "\"" | tr -d ";" | tr -d " "`
# GET the SQL Database User Name from freemed_yirc-vars.inc
sqldbuser=`cat freemed_yirc-vars.inc | grep "fydb_user" | grep -v "#"`
sqldbuser=`echo $sqldbuser | cut -d= -f2 | tr -d "\"" | tr -d ";" | tr -d " "`
# GET the login name of the current user...
logname=`logname`

# IF $sqldbprogram='psql', TEST to MAKE SURE $logname = $sqldbuser, or else
# problems may occur!!!
if test "$sqlprogram" = "psql"
then
	if test "$sqldbuser" != "$logname"
	then
		echo ""
		echo "FATAL ERROR: You must run this script as '$sqldbuser' or"	
		echo "             the database will not backup correctly!!!!!"
		echo ""
		exit
		exit
	fi
fi

# FIRST make sure that there are no files from today that might mess up
# the script...
if test -f $bkupfile.tar
then
	rm -r -f $bkupfile.tar
fi
if test -f $bkupfile.tar.gz
then
	rm -r -f $bkupfile.tar.gz
fi
if test -f $bkupdata.tar
then
	rm -r -f $bkupdata.tar
fi
if test -f $bkupdata.tar.gz
then
	rm -r -f $bkupdata.tar.gz
fi

ef=0

# -------------BACKUP FILES--------------
echo `date`": Backing up Freemed-YiRC files..." >> $errorlog
echo `date`": TARing files..." >> $errorlog
tar -cvf $bkupfile.tar * 1>>$errorlog 2>>$errorlog
temp=$?
if test $temp -ne 0
then
	echo `date`": -ERROR-TAR-$bkupfile" >> $errorlog
	ef=1
else
	echo `date`": File TAR Complete" >> $errorlog
fi
echo `date`": GZIPping TARed files..." >> $errorlog
gzip $bkupfile.tar 1>>$errorlog 2>>$errorlog
temp=$?
if test $temp -ne 0
then
	echo `date`": -ERROR-GZIP-$bkupfile" >> $errorlog
	ef=1
else
	echo `date`": File GZIP Complete" >> $errorlog
fi

# -------------BACKUP DATA--------------

echo `date`": Backing up Freemed-YiRC SQL data..." >> $errorlog
if test $sqlprogram = "psql"
then
	pg_dump $sqldbname -f $bkupdata 1>>$errorlog 2>>$errorlog
	temp=$?
else
	mysqldump -u $sqldbuser $sqldbname > $bkupdata 2>>$errorlog
	temp=$?
fi
if test $temp -ne 0
then
	echo `date`": -ERROR-SQLDUMP-$bkupdata" >> $errorlog
	ef=1
fi
echo `date`": TARing files..." >> $errorlog
tar -cvf $bkupdata.tar $bkupdata 1>>$errorlog 2>>$errorlog
temp=$?
if test $temp -ne 0
then
	echo `date`": -ERROR-TAR-$bkupdata" >> $errorlog
	ef=1
else
	echo `date`": Data TAR Complete" >> $errorlog
	rm -r -f $bkupdata 1>/dev/null 2>/dev/null
fi
echo `date`": GZIPping TARed files..." >> $errorlog
gzip $bkupdata.tar 1>>$errorlog 2>>$errorlog
temp=$?
if test $temp -ne 0
then
	echo `date`": -ERROR-GZIP-$bkupdata" >> $errorlog
	ef=1
else
	echo `date`": Data GZIP Complete" >> $errorlog
fi

# ------- COMPLETE ---------

if test $ef -ne 0
then
	echo `date`": THERE WERE ERRORS.. please see $errorlog!"
	echo `date`": Backup INCOMPLETE!" >> $errorlog
else
	echo `date`": Backup complete!" >> $errorlog
fi
