#!/bin/sh

# Version 2.6, 15th November 2002
# Author: ingmar.koecher@netikus.net
#
# This script generates a .tar.gz archive which contains
# "configuration.txt" -> includes system information (hostname, uptime, ...)
# most of the important configuration files
#
# The archive file should contain most everything needed to repair or re-install
# a standard Linux or Solaris system
#
# This script has been tested with Redhat 7.x and Solaris 8
#
# Feel free to extended this script - but please send your extensions to
# ingmar.koecher@netikus.net - so that others can benefit as well.
#
#

# INITIALIZE VARIABLES #
LINUX=0
SOLARIS=0
IPTABLES=0
IPCHAINS=0

UNIQUE_ID=$$
CONFIG_DIR=/root/$UNIQUE_ID.config
# INITIALIZE VARIABLES #

# DETERMINE OPERATING SYSTEM #
OS=`uname`
[ $OS = "SunOS" ] && SOLARIS=1
[ $OS = "Linux" ] && LINUX=1
# DETERMINE OPERATING SYSTEM #

# CREATE /root DIRECTORY IF NOT THERE YET (SOLARIS) #
[ ! -d /root ] && mkdir /root
# CREATE /root DIRECTORY IF NOT THERE YET (SOLARIS) #

# CREATE TEMP DIR #
[ ! -d $CONFIG_DIR ] && mkdir $CONFIG_DIR
# CREATE TEMP DIR #

CONFIG_FILE=$CONFIG_DIR/configuration.txt

echo "Extracting system information ..."
# GENERAL INFORMATION #
printf "REPORT GENERATED ON: " > $CONFIG_FILE
date >> $CONFIG_FILE

printf "\n\n===== HOSTNAME: " >> $CONFIG_FILE
hostname >> $CONFIG_FILE

printf "\n===== UNAME -a: " >> $CONFIG_FILE
uname -a >> $CONFIG_FILE

# REDHAT RELEASE
if [ -f /etc/redhat-release ]; then
	printf "\n===== Version:  " >> $CONFIG_FILE
	cat /etc/redhat-release >> $CONFIG_FILE
fi

printf "\n===== UPTIME: " >> $CONFIG_FILE
uptime >> $CONFIG_FILE
# GENERAL INFORMATION #

# FIREWALL CONFIGURATION #
ipchains --version >/dev/null; [ $? = 0 ] && IPCHAINS=0
iptables --version >/dev/null; [ $? = 0 ] && IPTABLES=1

if [ $IPCHAINS = 1 ]; then
	printf "\n===== IPCHAINS: " >> $CONFIG_FILE
	ipchains --version >> $CONFIG_FILE
	printf "\n" >> $CONFIG_FILE
	iptables --list >> $CONFIG_FILE
fi
if [ $IPTABLES = 1 ]; then
	printf "\n===== IPTABLES: " >> $CONFIG_FILE
	iptables --version >> $CONFIG_FILE
	printf "\n" >> $CONFIG_FILE
	iptables --list >> $CONFIG_FILE
fi
# FIREWALL CONFIGURATION #

# NETWORKING INFORMATION #
printf "\n===== IFCONFIG:\n" >> $CONFIG_FILE
ifconfig -a >> $CONFIG_FILE

printf "\n===== ROUTES:\n" >> $CONFIG_FILE
netstat -r >> $CONFIG_FILE

printf "\n===== NETSTAT:\n" >> $CONFIG_FILE
netstat -a >> $CONFIG_FILE
# NETWORKING  INFORMATION #

# DEFAULT RUN LEVEL #
printf "\n===== DEF RUNLEVEL:\n" >> $CONFIG_FILE
[ $LINUX = 1 ] && cat /etc/inittab | grep id >> $CONFIG_FILE
[ $SOLARIS = 1 ] && who -r >> $CONFIG_FILE
# DEFAULT RUN LEVEL #

# STARTUP CONFIGURATION #
printf "\n===== LINKS FOR RUN LEVEL 0:\n" >> $CONFIG_FILE
\ls /etc/rc0.d >> $CONFIG_FILE
printf "\n===== LINKS FOR RUN LEVEL 1:\n" >> $CONFIG_FILE
\ls /etc/rc1.d >> $CONFIG_FILE
printf "\n===== LINKS FOR RUN LEVEL 2:\n" >> $CONFIG_FILE
\ls /etc/rc2.d >> $CONFIG_FILE
printf "\n===== LINKS FOR RUN LEVEL 3:\n" >> $CONFIG_FILE
\ls /etc/rc3.d >> $CONFIG_FILE
if [ $LINUX = 1 ]; then
	printf "\n===== LINKS FOR RUN LEVEL 5:\n" >> $CONFIG_FILE
	\ls /etc/rc5.d >> $CONFIG_FILE
	printf "\n===== LINKS FOR RUN LEVEL 6:\n" >> $CONFIG_FILE
	\ls /etc/rc6.d >> $CONFIG_FILE
fi
# STARTUP CONFIGURATION #

# SOLARIS rc5 & rc6 scripts are copied later

printf "\n===== FILE SYSTEMS:\n" >> $CONFIG_FILE
df -k >> $CONFIG_FILE

# MEMORY REPORT #
printf "\n===== FREE MEMORY:\n" >> $CONFIG_FILE
vmstat >> $CONFIG_FILE

if [ $LINUX = 1 ]; then
	printf "\n===== MORE MEMORY:\n" >> $CONFIG_FILE
	free >> $CONFIG_FILE
fi
# MEMORY REPORT #

# HARDWARE INFO + INSTALLED PACKAGES #
# LINUX #
if [ $LINUX = 1 ]; then
	printf "\n===== CPU INFO /proc/cpuinfo :\n" >> $CONFIG_FILE
	cat /proc/cpuinfo >> $CONFIG_FILE

	printf "\n===== MEM INFO /proc/meminfo :\n" >> $CONFIG_FILE
	cat /proc/meminfo >> $CONFIG_FILE

	printf "\n===== INSTALLED RPM PACKAGES:\n" >> $CONFIG_FILE
	rpm -qa | sort >> $CONFIG_FILE
fi
# SOLARIS #
if [ $SOLARIS = 1 ]; then
	printf "\n===== PRTCONF OUTPUT:\n" >> $CONFIG_FILE
	prtconf >> $CONFIG_FILE

	printf "\n===== SYSDEF OUTPUT:\n" >> $CONFIG_FILE
	sysdef >> $CONFIG_FILE

	printf "\n===== INSTALLED PACKAGES:\n" >> $CONFIG_FILE
	pkginfo >> $CONFIG_FILE
fi
# HARDWARE INFO + INSTALLED PACKAGES #

printf "\n===== ACTIVE PROCESSES:\n" >> $CONFIG_FILE
ps -ef >> $CONFIG_FILE

printf "\n===== ENVIRONMENT VARIABLES:\n" >> $CONFIG_FILE
set >> $CONFIG_FILE


echo "Copying configuration files ..."
# COPY CONFIGURATION FILES #

mkdir $CONFIG_DIR/etc

# SYSCONFIG DIR #
if [ -d /etc/sysconfig ]; then
	mkdir -p $CONFIG_DIR/etc/sysconfig && cp -R /etc/sysconfig/* $CONFIG_DIR/etc/sysconfig/
	if [ -d /etc/sysconfig/network-scripts ]; then
		mkdir -p $CONFIG_DIR/etc/sysconfig/network-scripts && cp /etc/sysconfig/network-scripts/* $CONFIG_DIR/etc/sysconfig/network-scripts/
	fi
fi
# SYSCONFIG DIR #

# DETERMINE "init.d" LOCATION
if [ -d /etc/init.d ]; then
        INIT_DIR="/etc/init.d"
fi
if [ -d /etc/rc.d/init.d ]; then
        INIT_DIR="/etc/rc.d/init.d"
fi

# COPY DAEMON STARTUP SCRIPTS
if [ ! -d $CONFIG_DIR$INIT_DIR ]; then
        mkdir -p $CONFIG_DIR$INIT_DIR && cp -R $INIT_DIR/* $CONFIG_DIR$INIT_DIR/
fi

if [ $SOLARIS = 1 ]; then
	cp /etc/rc5 $CONFIG_DIR/etc/
	cp /etc/rc6 $CONFIG_DIR/etc/
fi

# LILO / GRUB #
[ -f /etc/lilo.conf ] && cp /etc/lilo.conf $CONFIG_DIR/etc/
[ -f /etc/grub.conf ] && cp /etc/grub.conf $CONFIG_DIR/etc/
# LILO / GRUB #

# CRON LINUX #
if [ $LINUX = 1 ]; then
	# RedHat global crontab file
	cp /etc/crontab $CONFIG_DIR/etc/

	[ -f /etc/cron.allow ] && cp /etc/cron.allow $CONFIG_DIR/etc/
	[ -f /etc/cron.deny ] && cp /etc/cron.deny $CONFIG_DIR/etc/

	# RedHat specific	
	[ -d /etc/cron.d ] && cp -R /etc/cron.d $CONFIG_DIR/etc/
	[ -d /etc/cron.hourly ] && cp -R /etc/cron.hourly $CONFIG_DIR/etc/
	[ -d /etc/cron.daily ] && cp -R /etc/cron.daily $CONFIG_DIR/etc/
	[ -d /etc/cron.weekly ] && cp -R /etc/cron.weekly $CONFIG_DIR/etc/
	[ -d /etc/cron.monthly ] && cp -R /etc/cron.monthly $CONFIG_DIR/etc/

	# Copy user cron files
	if [ -d /var/spool/cron ]; then
		mkdir -p $CONFIG_DIR/var/spool/cron && cp -R /var/spool/cron/* $CONFIG_DIR/var/spool/cron/
	fi
fi
# CRON LINUX #
# CRON SOLARIS #
if [ $SOLARIS = 1 ]; then
	[ -d /etc/cron.d ] && mkdir -p $CONFIG_DIR/etc/cron.d
	[ -f /etc/cron.d/cron.allow ] && cp /etc/cron.d/cron.allow $CONFIG_DIR/etc/cron.d/
	[ -f /etc/cron.d/cron.deny ] && cp /etc/cron.d/cron.deny $CONFIG_DIR/etc/cron.d/
	
	# Copy user cron files
	mkdir -p $CONFIG_DIR/var/spool/cron/crontabs
	for crondir in /var/spool/cron/crontabs/*
	do
		cp $crondir $CONFIG_DIR/var/spool/cron/crontabs/
	done
fi
# CRON SOLARIS #

[ -f /etc/fstab ] && cp /etc/fstab $CONFIG_DIR/etc/
[ -f /etc/vfstab ] && cp /etc/vfstab $CONFIG_DIR/etc/

cp /etc/inittab $CONFIG_DIR/etc/

cp /etc/hosts $CONFIG_DIR/etc/
cp /etc/passwd $CONFIG_DIR/etc/
cp /etc/group $CONFIG_DIR/etc/
cp /etc/services $CONFIG_DIR/etc/

cp /etc/syslog.conf $CONFIG_DIR/etc/
cp /etc/resolv.conf $CONFIG_DIR/etc/
cp /etc/nsswitch.conf $CONFIG_DIR/etc/

# MORE SOLARIS FILES
if [ $SOLARIS = 1 ]; then
	cp /etc/hostname.* $CONFIG_DIR/etc/
	cp /etc/defaultrouter $CONFIG_DIR/etc/
fi

[ -f /etc/profile ] && cp /etc/profile $CONFIG_DIR/etc/
[ -f /etc/bashrc ] && cp /etc/bashrc $CONFIG_DIR/etc/

# STARTUP SCRTIPS
if [ ! -d $CONFIG_DIR/etc/init.d ]; then
	mkdir $CONFIG_DIR/etc/init.d && cp /etc/init.d/* $CONFIG_DIR/etc/init.d/
fi

# XINET INSTALLED? #
if [ -d /etc/xinetd.d ]; then
	mkdir $CONFIG_DIR/etc/xinetd.d && cp /etc/xinetd.d/* $CONFIG_DIR/etc/xinetd.d/
fi
# SOLARIS #
[ -f /etc/inet.conf ] && cp /etc/inet.conf $CONFIG_DIR/etc/
# XINET INSTALLED? #

# PAM CONFIGURATION #
if [ -d /etc/pam.d ]; then
	mkdir $CONFIG_DIR/etc/pam.d && cp /etc/pam.d/* $CONFIG_DIR/etc/pam.d/
fi
# SOLARIS #
[ -f /etc/pam.conf ] && cp /etc/pam.conf $CONFIG_DIR/etc/
# PAM CONFIGURATION #

# SSH INSTALLED? #
if [ -d /etc/ssh ]; then
	mkdir $CONFIG_DIR/etc/ssh && cp /etc/ssh/ssh*_config $CONFIG_DIR/etc/ssh/
fi
# SSH INSTALLED? #

# APACHE INSTALLED? #
if [ -f /etc/httpd/conf/httpd.conf ]; then
	mkdir -p $CONFIG_DIR/etc/httpd/conf && cp /etc/httpd/conf/*.conf $CONFIG_DIR/etc/httpd/conf/
fi
if [ -f /usr/local/apache/conf/httpd.conf ]; then
	mkdir -p $CONFIG_DIR/usr/local/apache/conf && cp /usr/local/apache/conf/*.conf $CONFIG_DIR/usr/local/apache/conf/
fi
# APACHE INSTALLED? #

# SAMBA INSTALLED? #
if [ -f /etc/smb.conf ]; then
	cp /etc/smb.conf $CONFIG_DIR/etc/
fi
if [ -f /etc/samba/smb.conf ]; then
	mkdir -p $CONFIG_DIR/etc/samba/ && cp /etc/samba/smb.conf $CONFIG_DIR/etc/samba/
fi
if [ -f /usr/local/samba/lib/smb.conf ]; then
	mkdir -p $CONFIG_DIR/usr/local/samba/ && cp /usr/local/samba/lib/smb.conf $CONFIG_DIR/usr/local/samba
fi
# SAMBA INSTALLED? #

# COCOON INSTALLED?
if [ -f /var/tomcat/webapps/cocoon/sitemap.xmap ]; then
	mkdir -p $CONFIG_DIR/var/tomcat/webapps/cocoon && cp /var/tomcat/webapps/cocoon/sitemap.xmap $CONFIG_DIR/var/tomcat/webapps/cocoon/
fi

# IP FILTER INSTALLED?
if [ -f /etc/opt/ipf/ipf.conf ]; then
	mkdir -p $CONFIG_DIR/etc/opt/ipf && cp /etc/opt/ipf/ipf.conf $CONFIG_DIR/etc/opt/ipf/
fi

# NFS INSTALLED?
if [ -f /etc/exports ]; then
	cp /etc/exports $CONFIG_DIR/etc/
fi

# APPLE TALK EMULATION INSTALLED?
if [ -d /etc/atalk ]; then
	mkdir $CONFIG_DIR/etc/atalk && cp -R /etc/atalk/* $CONFIG_DIR/etc/atalk/
fi

# SQUID INSTALLED?
if [ -d /etc/squid ]; then
	mkdir $CONFIG_DIR/etc/squid && cp /etc/squid/* $CONFIG_DIR/etc/squid/
fi

echo "Creating archive ..."

# SETUP ARCHIVE NAME (=hostname.TAR)
ARCHIVE=`hostname`.tar

# REMOVE OLD ARCHIVE
rm -f /root/$ARCHIVE.gz

# CREATE ARCHIVE
cd $CONFIG_DIR
tar cf /root/$ARCHIVE *
gzip /root/$ARCHIVE

# REMOVE TEMP DIR
cd /root
rm -rf $UNIQUE_ID.config

# CHANGE PERMISSIONS
chown 0:0 /root/$ARCHIVE.gz
chmod 400 /root/$ARCHIVE.gz

echo "Done."

