#!/bin/sh

#   MBackup - Back up utility by Manna!!   // Manna is the nickname of Makarand Hazarika
#   This script was written by Makarand Hazarika.

#   Copyright (C) 2006  Makarand Hazarika

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#    You can contact the author at <makarand.hazarika@gmail.com>


init() {

	script_name="$0"
	VERSION=1.0
	OLD_PATH=`echo $PATH`
	PATH=$PATH:/sbin:/usr/sbin
	BACKUPDIR=/root/MBackup
	TEMP=$BACKUPDIR/mbackup.tmp
	ans='n'
	
	if [ ! -w /root ]
	then
		echo -e "\nPLEASE RUN THE SCRIPT AS ROOT\n"
		exit 0
	fi
	
	if [ -d "$BACKUPDIR" ] 
	then 
		echo > /dev/null
	else
		mkdir "$BACKUPDIR"
	fi
	
	echo -e "\nInitializing Graphical Mode... Successful\n\nPress OK to continue.\n(Press 'Esc' and click 'OK' to enter text mode)" > $TEMP
	dialog --title "WELCOME TO MBACKUP $VERSION" --backtitle "Author - Makarand Hazarika"  --exit-label "OK" --textbox "$TEMP" 19 72 2> /dev/null
		
	if [ "$?" -eq 0 ]
	then
		gui_support=y
	else
		gui_support=n
		clear
		echo -e "\nInitializing Graphical Mode... Failed!!\nThis can even happen if you press Esc during initialization of graphical mode.\nThe program will run in text mode\n\n"
		echo "Press Enter to continue...."
		read
	fi

}



exit_msg() {

	if [ "$gui_support" = 'y' ]
	then
		dialog --clear --title "THANK YOU FOR USING THIS PROGRAM" --backtitle "Author - Makarand Hazarika" --msgbox "\nMBackup $VERSION\nCopyright (C) 2006  Makarand Hazarika\nThis is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE\nAuthor:-\nMakarand Hazarika  <makarand.hazarika@gmail.com>\n" 15 62
		clear
	else
		#clear
		echo -e "\n\n\t\t   THANK YOU FOR USING THIS PROGRAM\n\n"
		echo -e "\nMBackup Version $VERSION\nCopyright (C) 2006  Makarand Hazarika\nThis is free software; see the source for copying conditions.\nThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE\n\nAuthor:-\nMakarand Hazarika  <makarand.hazarika@gmail.com>\n"
		
	fi
		
}


quit() {

	rm -f $TEMP
	exit_msg
	exit 0
}


main() {

if [ "$gui_support" = "y" ]
then
	dialog --title "M-BACKUP $VERSION" --backtitle "Author - Makarand Hazarika" --yesno "This utility will take backup of some important files in your system. The output of this script will go to $BACKUPDIR. Are you ready?" 12 47
	
	if [ "$?" = "0" ]
	then 
		ans='y'
	fi
		
else
	clear
	echo -e "Author - Makarand Hazarika\n\n\n\n\n"
	echo -ne "This utility will take backup of some important files in your system.\nThe output of this script will go to $BACKUPDIR.\nAre you ready?(y/n)"
	read ans
fi


case "$ans" in
	[yY]*)
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Checking and deleting older MBackup archives" 7 72
		else
			echo -e "Checking and deleting older MBackup archives..."
		fi
		test -e /root/oldmbackup.tar.gz.old && rm -f /root/oldmbackup.tar.gz.old
		
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Checking and moving existing MBackup archives" 7 72
		else
			echo -e "Checking and moving existing MBackup archives..."
		fi
		test -e $BACKUPDIR/oldmbackup.tar.gz &&  mv  $BACKUPDIR/oldmbackup.tar.gz /root/oldmbackup.tar.gz.old
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Checking and renaming existing MBackup archives" 7 72
		else
			echo -e "Checking and renaming existing MBackup archives..."
		fi
		test -e /root/oldmbackup.tar.gz.old && rm -f /root/oldmbackup.tar.gz.old
		test -e /root/oldmbackup.tar.gz && mv /root/oldmbackup.tar.gz /root/oldmbackup.tar.gz.old
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Archiving existing MBackup files" 7 72
		else
			echo -e "Archiving existing MBackup files..."
		fi
		if [ `ls -al $BACKUPDIR/ | wc -l` -gt 3 ]
		then
			tar -zcf /root/oldmbackup.tar.gz $BACKUPDIR/*
		fi
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Removing existing MBackup files" 7 72
		else
			echo -e "Removing existing MBackup files..."
		fi
		rm -rf $BACKUPDIR/*
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Writing to log file $BACKUPDIR/mbackup.log" 7 72
		else
			echo -e "Writing to log file $BACKUPDIR/mbackup.log..."
		fi
		echo " " > $BACKUPDIR/mbackup.log
		echo -e " ************************************************** " >> $BACKUPDIR/mbackup.log
		echo -e 'MBackup by Manna!!' >> $BACKUPDIR/mbackup.log
		echo -e "Version $VERSION" >> $BACKUPDIR/mbackup.log
		echo -e "Author -- Makarand Hazarika" >> $BACKUPDIR/mbackup.log
		echo -e "Copyright (C) 2006  Makarand Hazarika" >> $BACKUPDIR/mbackup.log
		echo -e "This is free software; see the source for copying conditions." >> $BACKUPDIR/mbackup.log
		echo -e "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE" >> $BACKUPDIR/mbackup.log
		echo -ne "Log file created on : " >> $BACKUPDIR/mbackup.log
		date >> $BACKUPDIR/mbackup.log
		echo " **************************************************\n\n\n " >> $BACKUPDIR/mbackup.log
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Moving the oldmbackup file to $BACKUPDIR/" 7 72
		else
			echo -e "Moving the oldmbackup file to $BACKUPDIR/..."
		fi
		mv /root/oldmbackup.tar.gz $BACKUPDIR/
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Retrieving Machine And System Information" 7 72
		else
			echo -e "Retrieving Machine And System Information..."
		fi
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Retrieving Machine And System Information ---> Kernel Version" 7 72
		else
			echo -e "Retrieving Machine And System Information ---> Kernel Version..."
		fi
		echo -en "   Kernel Version            : " >> $BACKUPDIR/mbackup.log
		uname -r >> $BACKUPDIR/mbackup.log
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Retrieving Machine And System Information ---> Hostname" 7 72
		else
			echo -e "Retrieving Machine And System Information ---> Hostname..."
		fi
		echo -en "   Hostname                  : " >> $BACKUPDIR/mbackup.log
		uname -n >> $BACKUPDIR/mbackup.log
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Retrieving Machine And System Information ---> OS" 7 72
		else
			echo -e "Retrieving Machine And System Information ---> OS..."
		fi
		echo -en "   System OS                 : " >> $BACKUPDIR/mbackup.log
		uname -s >> $BACKUPDIR/mbackup.log
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Retrieving Machine And System Information ---> Release" 7 72
		else
			echo -e "Retrieving Machine And System Information ---> Release..."
		fi
		echo -en "   Release                   : " >> $BACKUPDIR/mbackup.log
		for i in /etc/*[rv]e[lr][es][ai][so][en]
		do 
			if [ -L "$i" ]
			then 
				echo > /dev/null
			else  
				cat "$i" 
			fi
		done 2> /dev/null | sed '/LSB/d' | head -n 1 >> $BACKUPDIR/mbackup.log
		echo -e "Version Information" >> $BACKUPDIR/mbackup.log
		cat /proc/version >> $BACKUPDIR/mbackup.log
		echo -e "\n\n" >> $BACKUPDIR/mbackup.log 
	
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Taking a sapshot of the whole filesystem\nIt will take long time" 7 72
		else
			echo -e "Taking a sapshot of the whole filesystem (It will take long time)..."
		fi
		test ! -d $BACKUPDIR/misc && mkdir $BACKUPDIR/misc
		ls -AlRH / >> $BACKUPDIR/misc/filesystem_snapshot
		rm -f $BACKUPDIR/misc/filesystem_snapshot.gz
		gzip $BACKUPDIR/misc/filesystem_snapshot
		echo -e "\nFilesystem snapshot saved in $BACKUPDIR/misc/filesystem_snapshot.gz" >> $BACKUPDIR/mbackup.log
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system files" 7 72
		else
			echo -e "Backing up important system files..."
		fi
		echo -e "\nTaking backup of important information:-" >> $BACKUPDIR/mbackup.log
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system files -- /proc/cpuinfo" 7 72
		else
			echo -e "Backing up important system files -- /proc/cpuinfo..."
		fi
		echo -e "/proc/cpuinfo" >> $BACKUPDIR/mbackup.log
		cat /proc/cpuinfo >> $BACKUPDIR/cpuinfo.proc
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system files -- /proc/meminfo" 7 72
		else
			echo -e "Backing up important system files -- /proc/meminfo..."
		fi
		echo -e "/proc/meminfo" >> $BACKUPDIR/mbackup.log
		cat /proc/meminfo >> $BACKUPDIR/meminfo.proc
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system files -- /proc/partitions" 7 72
		else
			echo -e "Backing up important system files -- /proc/partitions..."
		fi
		echo "/proc/partitions" >> $BACKUPDIR/mbackup.log
		cat /proc/partitions >> $BACKUPDIR/partitions.proc
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system files -- /proc/modules" 7 72
		else
			echo -e "Backing up important system files -- /proc/modules..."
		fi
		echo "/proc/modules" >> $BACKUPDIR/mbackup.log
		cat /proc/modules >> $BACKUPDIR/modules.proc
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system files -- /etc/fstab" 7 72
		else
			echo -e "Backing up important system files -- /etc/fstab..."
		fi
		test ! -d $BACKUPDIR/etcfiles && mkdir $BACKUPDIR/etcfiles
		echo -e "/etc/fstab" >> $BACKUPDIR/mbackup.log
		cat /etc/fstab >> $BACKUPDIR/etcfiles/fstab
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system files -- /etc/mtab" 7 72
		else
			echo -e "Backing up important system files -- /etc/mtab..."
		fi
		echo -e "/etc/mtab" >> $BACKUPDIR/mbackup.log
		cat /etc/mtab >> $BACKUPDIR/etcfiles/mtab
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system files -- /etc/inittab" 7 72
		else
			echo -e "Backing up important system files -- /etc/inittab..."
		fi
		echo -e "/etc/inittab" >> $BACKUPDIR/mbackup.log
		cat /etc/inittab >> $BACKUPDIR/etcfiles/inittab
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system files -- /etc/profile" 7 72
		else
			echo -e "Backing up important system files -- /etc/profile..."
		fi
		echo -e "/etc/profile" >> $BACKUPDIR/mbackup.log
		cat /etc/profile >> $BACKUPDIR/etcfiles/profile
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system files -- configuration files" 7 72
		else
			echo -e "Backing up important system files -- configuration files..."
		fi
		echo -e "configuration files" >> $BACKUPDIR/mbackup.log
		cp -r /etc/*onf* $BACKUPDIR/etcfiles
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system files -- host files" 7 72
		else
			echo -e "Backing up important system files -- host files..."
		fi
		echo -e "host files" >> $BACKUPDIR/mbackup.log
		cp /etc/host* $BACKUPDIR/etcfiles
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system information -- disk drive information" 7 72
		else
			echo -e "Backing up important system information -- disk drive information..."
		fi
		echo -e "output of fdisk -l" >> $BACKUPDIR/mbackup.log
		fdisk -l >> $BACKUPDIR/fdisk.output
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system information -- disk usage" 7 72
		else
			echo -e "Backing up important system information -- disk usage..."
		fi
		echo -e "output of df" >> $BACKUPDIR/mbackup.log
		df -l >> $BACKUPDIR/df.output
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system information -- network" 7 72
		else
			echo -e "Backing up important system information -- network..."
		fi
		echo -e "output of ifconfig" >> $BACKUPDIR/mbackup.log
		ifconfig >> $BACKUPDIR/ifconfig.output
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system information -- kernel ring buffer" 7 72
		else
			echo -e "Backing up important system information -- kernel ring buffer..."
		fi
		echo -e "output of dmesg" >> $BACKUPDIR/mbackup.log
		dmesg >> $BACKUPDIR/dmesg.output
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up important system information -- mount points" 7 72
		else
			echo -e "Backing up important system information -- mount points..."
		fi
		echo -e "output of mount " >> $BACKUPDIR/mbackup.log
		mount >> $BACKUPDIR/mount.output
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up some other files" 7 72
		else
			echo -e "Backing up some other files..."
		fi
		echo -e "\n\nTaking backup of some other information:-" >> $BACKUPDIR/mbackup.log
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up some other files -- X Server Configuration Informatioin" 7 72
		else
			echo -e "Backing up some other files -- X Server Configuration Informatioin..."
		fi
		echo -e "XFree86 config file" >> $BACKUPDIR/mbackup.log
		cp -r /etc/X11/*onf* $BACKUPDIR/misc/
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up some other information -- environment variables" 7 72
		else
			echo -e "Backing up some other information -- environment variables..."
		fi
		echo -e "output of env" >> $BACKUPDIR/mbackup.log
		env > $BACKUPDIR/misc/env.output
		
		
		if [ "$gui_support" = "y" ]
		then
			dialog --backtitle "Author - Makarand Hazarika" --title "Performing Requested Task, Please wait..." --infobox "Backing up some other information -- system wide aliases" 7 72
		else
			echo -e "Backing up some other information -- system wide aliases..."
		fi
		echo -e "output of alias" >> $BACKUPDIR/mbackup.log
		alias >> $BACKUPDIR/misc/alias.output
		
		if [ "$gui_support" = 'y' ]
		then
			dialog --clear --title "MBackup $VERSION" --backtitle "Author - Makarand Hazarika" --msgbox "\nBackup created in $BACKUPDIR\nFor a list of backed up files, see $BACKUPDIR/mbackup.log" 15 62
			
		else
		#clear
			echo -e "\nBackup created in $BACKUPDIR\nFor a list of backed up files, see $BACKUPDIR/mbackup.log\n"
		
		fi
	
		
		;;
		
	[nN]*)
		quit
		;;
	*)
		echo "Please ans yes or no"
		;;
esac



}









######################################################################################################################
##############********************   EXECUTION OF THE SCRIPT STARTS HERE   ********************#######################
######################################################################################################################

trap "quit ; " 1 2 3 7 9 11 15
init 

trap "quit ; " 1 2 3 7 9 11 15
main 

quit

######################################################################################################################
###############********************   EXECUTION OF THE SCRIPT ENDS HERE   *********************#######################
######################################################################################################################

