#!/bin/sh
#
# backupconfigs - backup important system config files to a .tar file. 
#	Solaris, tuneable for other Unix/Linux.
#
# 20-Jun-2003	ver 1.10
#
# USAGE: backupconfigs [-vh][-f outfile | -d outdir][-i inlist | files ...]
#
#  eg,  backupconfigs -v                # create backup, verbose.
#       backupconfigs /etc /var/adm	# backup /etc and /var/adm instead.
#       backupconfigs -d /var/tmp       # create a backup in /var/tmp.
#       backupconfigs -f /dev/rmt/0h    # send backup to the tape drive
#
# By default this backups up common Solaris system config files. It creates
# a tar file which has a meaningful structure (retains the original
# directory tree, with "hostname.date/" prepended), and a meaningful
# filename "hostname.date.tar".
#
# 	-d	Change output directory. Default is current. Or,
#	-f	Change output filename. Defaule is "hostname.date.tar".
#	-v	Verbose. Not suited for crontabs.
#	-h	Usage help.
#	files	A list of files and dirs to include in the backup (pathnames 
#		must begin with "/", wildcards also work). By default it uses 
#		a "files" variable listed in the script below. By using custom 
#		files and dirs, this script can be quickly adapted to other 
#		OS's such as Linux.
#	-i	In list file. A text file containing the list mentioned above.
#		Useful if the list grows to be large.
#
# Standard Disclaimer: this is freeware, use at your own risk.
#
# 20-Jun-2003	Brendan Gregg	Created this.


#
# --- Setup Vars and Subs ---
#
# Your own files or directories can be added to this list. Wildcards also work.
# eg "/var/named/*.zone". Always use absolute filenames (begin with a "/"), 
# and please don't place comments "#" within the list. 

files="
	/etc/passwd		 /etc/shadow
	/etc/groups		 /etc/system
	/etc/vfstab		 /etc/inet/hosts
	/etc/nodename		 /etc/*.conf
	/etc/dfs		 /etc/ssh
	/etc/inet/inetd.conf
	/etc/mail/aliases
	/etc/defaultdomain
	/etc/default/login
	/var/spool/cron
	/opt/sfw/squid/etc/squid.conf
"
workdir=/tmp				# Working dir, use "." for current
worktmp=$workdir/.bctmp.$$		# Dir where tmp files go
PATH=/bin:$PATH
hostname=`uname -n`
date=`date +%Y%m%d`			# Date format YYYYMMDD
outputfile=$PWD/$hostname.$date.tar	# Final output file
verbose=0

# usage - print a usage message.
#
usage() {
	echo >&2 "USAGE: $0 [-vh][-f outfile | -d outdir][-i inlist | files ...]
   eg, $0 -v                # create backup, verbose.
       $0 /etc /var/adm     # backup /etc and /var/adm instead.
       $0 -d /var/tmp       # create a backup in /var/tmp.
       $0 -f /dev/rmt/0h    # send backup to the tape drive"
}

# The following cleans up temporary files on exit or a signal.
#
trap "
	cd /
	rm -rf $worktmp
" 0 2 3 15


#
# --- Parse Options ---
#
set -- `getopt vhf:d:i: $*`
if [ $? -ne 0 ]; then
        usage
        exit 1
fi

while [ $# -ne 0 ]
do
	case "$1" in
	-v)	verbose=1
		;;
	-h)	usage
		exit 0
		;;
	-f)	outputfile=$2
		shift
		;;
	-d)	dest=$2
		if [ ! -d $dest ]; then
		   echo >&2 "ERROR: $dest, is not a destination directory."
		   exit 1
		fi
		outputfile=$dest/$hostname.$date.tar
		shift
		;;
	-i)	infile=$2
		if [ ! -r $infile ]; then
		   echo >&2 "ERROR: $infile, is not readable."
		   exit 2
		fi
		files=`cat $infile` 	# Use infile for list to backup
		shift
		;;
	--)	shift
		break
		;;
	esac
	shift
done
if [ "$1" != "" ]; then			# files were on the command line
        files=$*
fi


#
# --- Copy files ---
#
mkdir -p $worktmp/$hostname.$date
cd /
echo $files | sed 's/^/./;s: /: ./:g' \
	| xargs tar cf - 2> /dev/null  \
	| (cd $worktmp/$hostname.$date; tar xf - 2> /dev/null)
# The sed makes absolutes relative (don't want to chroot broken tar files)


#
# --- Create final .tar file ---
#
cd $worktmp
if [ $verbose -eq 1 ]; then
	echo "Creating file: $outputfile\n"
	tar cvf $outputfile $hostname.$date
else
	tar cf $outputfile $hostname.$date
fi


#
# --- Security ---
#
chmod 600 $outputfile

# Security Note: The tar file contains /etc/shadow. At times this is very
# useful to have backed up, but be careful with the backup file permissions!
# (ie don't leave the backup files world readable). A cracker with /etc/shadow 
# may run tools like Crack or John the Ripper and break passwords in minutes.


#
# --- Print filename ---
#
if [ $verbose -eq 1 ]; then
	echo "\nFile: $outputfile created.\n"
	ls -l $outputfile
fi
