#!/bin/bash

###########################################################################
#
#	Shell program to backup /etc directory to a floppy.
#
#	Copyright 2000 - 2001, Jogeir Lindseth <jog-l@frisurf.no>.
#
#	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.
#
#	This software is part of the LinuxCommand.org project, a site for
#	Linux education and advocacy devoted to helping users of legacy
#	operating systems migrate into the future.
#
#	You may contact the LinuxCommand.org project at:
#
#		http://www.linuxcommand.org
#
#	Description: 
#
#		version 0.5
#
#		This script make backup of you /etc dir on a floppy,
#		and creating a logfile.
#
#	NOTE: You must be the superuser to run this script.
#
#	Usage:
#
#		backupetc [ -h | --help ]
#
#		bckabckup [ -v | --version ]
#
#		
#	Options:
#
#		-h, --help	Display help message and exit.
#
#		-v, --version   Display version and exit.
#
#
#	Revisions: See CHANGELOG.backupetc
#
#	11/05/2000	File created
#
#	$Id: backupetc,v 1.1.1.1 2001/03/24 13:50:18 bshotts Exp $ backupetc
#
###########################################################################


###########################################################################
#	Constants
###########################################################################

PROGNAME=$(basename $0)
VERSION="version 0.5"
EMAIL="jog-l@frisurf.no"

# Change this to your floppy device if needed.
FLOPPY_DRIVE=/dev/fd0
#FLOPPY_DRIVE=/dev/fd1

# Find floppy disk mount point
FLOPPY_MOUNT_POINT=		# Define this if not using SuSE or Red Hat
if [ ! -d "$FLOPPY_MOUNT_POINT" ]; then
	if [ -d /floppy ]; then
		FLOPPY_MOUNT_POINT=/floppy	# SuSE
	elif [ -d /mnt/floppy ]; then
		FLOPPY_MOUNT_POINT=/mnt/floppy	# Red Hat
	else
		echo "${PROGNAME}: Invalid floppy mount point" >&2
		exit 1
	fi
fi

NOW=`date`
TITLE="Doing full backup of /etc, from $NOW"
TODAY=`date "+%Y%m%d"`
LOGFILE="$FLOPPY_MOUNT_POINT/${TODAY}.etc.log"
NAME="${TODAY}.etc.tar.gz"


###########################################################################
#	Functions.
###########################################################################


function graceful_exit
{
	#####
	#	Function called for a graceful exit.
	#	No arguments.
	#####

	clean_up
	exit
}


function error_exit 
{
	#####	
	# 	Function for exit due to fatal program error.
	# 	Accepts 1 argument
	#		string containing descriptive error message.
	#####

	
	echo "${PROGNAME}: ${1:-"Unknown Error"}" >&2
	clean_up
	exit 1
}


function term_exit
{
	#####
	#	Function to perform exit if termination signal is trapped.
	#	No arguments.
	#####

	echo "${PROGNAME}: Terminated"
	clean_up
	exit
}


function int_exit
{
	#####
	#	Function to perform exit if interrupt signal is trapped.
	#	No arguments.
	#####

	echo "${PROGNAME}: Aborted by user"
	clean_up
	exit
}


function usage
{
	#####
	#	Function to display usage message (does not exit).
	#	No arguments.
	#####

	echo "Usage: ${PROGNAME} [-h | --help]"
}


function helptext
{
	#####
	#	Function to display help message for program.
	#	No arguments.
	#####
	
	local tab=$(echo -en "\t\t")
		
	cat <<- -EOF-

	${PROGNAME} ${VERSION}	
	This is a program to backup /etc directory to a floppy.
	
	$(usage)
	
	Options:
	
	-h, --help	Display this help message and exit.

	-v, --version   Display version and exit.

			
	NOTE: You must be the superuser to run this script.
		
	-EOF-
}	


function mount_medium
{
	#####
	#       Function to mount and format backup medium.
	#       No arguments.
	#####

	# Writing info on the screen.
	echo
	echo "WARNING all data on the $FLOPPY_DRIVE will be deleted."
	echo
	echo "Insert backup floppy disk into "$FLOPPY_DRIVE
	echo "Hit Enter to continue."
	echo "Ctrl-C to abort."

	# Waiting for keyboard input.
	read

	# Making mke2fs filsystem on the floppydisk.	
	mke2fs -L backupetc $FLOPPY_DRIVE || error_exit "cannot create filesystem"

	# Mounting the backup medium.
	mount -t ext2 $FLOPPY_DRIVE $FLOPPY_MOUNT_POINT || error_exit "cannot mount floppy"
}


function tar_to_floppy
{
        #####
        #       Function to perform tar of /etc directory
        #       No arguments.
        #####

	# Writing info on screen.
	echo -e "\n$TITLE"

	# tar the /etc filsystem on the backup medium and write to log file.
	echo  "Writing tar file to floppy... "
	echo "$TITLE" > $LOGFILE 
	tar -czf $FLOPPY_MOUNT_POINT/$NAME /etc 2>> $LOGFILE
}

function version
{
        #####
        #       Function to display version.
        #       No arguments.
        #####

	echo "${PROGNAME} ${VERSION} by Jogeir Lindseth ${EMAIL}"
}

###########################################################################
#	Program starts here.
###########################################################################

# Trap TERM, HUP, and INT signals and properly exit.

trap term_exit TERM HUP
trap int_exit INT


# Check if the user is root.

if [ `id -ru` -gt 0 ]; then
	echo "You must be root to run this script."
	graceful_exit
fi;


# Process command line arguments.

if [ "$1" = "--help" ]; then
	helptext
	graceful_exit
fi

# Print version.
        if [ "$1" = "--version" ]; then
                version
        graceful_exit
        fi


# Process arguments.

while getopts ":h:v" opt; do
	case $opt in

		h )	helptext
			graceful_exit ;;
		v )	version
			graceful_exit ;;
		* )	usage
			exit 1
	esac
done


mount_medium

tar_to_floppy

umount $FLOPPY_DRIVE

echo -e "\nBackup done!"

graceful_exit

# End file backupetc


syntax highlighted by Code2HTML, v. 0.9.1