#!/bin/sh
# Parameters
PATH=/bin:/sbin:/usr/bin:/usr/sbin
DATE=`date -j "+%Y%m%d"`
OPTIONS=-cPvjf
BKPATH=/usr/backup/$DATE
EXFILE=.sujournal
SUFFIX=tbz
# Removes the existing file/directory
# if it exists
remove_exist()
{
if [ -e $BKPATH ] ; then
rm -Rf $BKPATH
fi
}
# Creates a new directory, if needed
create_dir()
{
if [ ! -e $BKPATH ]; then
mkdir $BKPATH
chmod 0700 $BKPATH
elif [ ! -d $BKPATH ] ; then
rm -Rf $BKPATH
mkdir $BKPATH
chmod 0700 $BKPATH
fi
}
# Prints instructions
usage()
{
echo ''
echo 'usage: backup [ all | home | etc | src |'
echo ' obj | doc | ports | local ]'
echo ''
echo 'Any of the above combinations will'
echo 'be recognized.'
echo ''
echo 'The options above will back up the'
echo 'following components:'
echo ''
echo ' all: Everything'
echo ' home: Home directories'
echo ' etc: System configuration'
echo ' src: System source code'
echo ' obj: System object code'
echo ' doc: System documentation'
echo ' ports: The ports tree'
echo ' local: Local software configuration'
echo ''
echo 'If no options are given, then'
echo 'all is assumed.'
echo ''
}
# Performs the backup
exec_archive()
{
tar $OPTIONS $1 $2 $3 $4
}
# Archives all the home directories
tar_home()
{
local exclude
local target
local archive
target=/home
archive=$BKPATH/home.$SUFFIX
exclude="--exclude $target/$EXFILE"
exec_archive $archive $exclude $target
}
# Archives the kernel config
tar_i386conf ()
{
local exclude
local target
local archive
target=/usr/src/sys/i386/conf
archive=$BKPATH/usr.i386.$SUFFIX
exclude="--exclude $target/$EXFILE"
exec_archive $archive $exclude $target
}
# Archives the system config
tar_etc()
{
local exclude
local target
local archive
target=/etc
archive=$BKPATH/etc.$SUFFIX
exclude="--exclude $target/$EXFILE"
exec_archive $archive $exclude $target
}
# Archives the compiled base system
tar_usrobj()
{
local exclude
local target
local archive
target=/usr/obj
archive=$BKPATH/usr.obj.$SUFFIX
exclude="--exclude $target/$EXFILE"
exec_archive $archive $exclude $target
}
# Archives the base system source code
tar_usrsrc()
{
local exclude
local target
local archive
target=/usr/src
archive=$BKPATH/usr.src.$SUFFIX
exclude="--exclude $target/$EXFILE"
exec_archive $archive $exclude $target
}
# Archives the documentation
tar_usrdoc()
{
local exclude
local target
local archive
target=/usr/doc
archive=$BKPATH/usr.doc.$SUFFIX
exclude="--exclude $target/$EXFILE"
exec_archive $archive $exclude $target
}
# Archives the ports tree
tar_usrports()
{
local exclude
local target
local archive
target=/usr/ports
archive=$BKPATH/usr.ports.$SUFFIX
exclude="--exclude $target/$EXFILE"
exec_archive $archive $exclude $target
}
# Archives the installed software
tar_usrlocaletc()
{
local exclude
local target
local archive
target=/usr/local/etc
archive=$BKPATH/usr.local.etc.$SUFFIX
exclude="--exclude $target/$EXFILE"
exec_archive $archive $exclude $target
}
# Saves a list of the current packages installed
# on the system
pkg_save()
{
pkg info > $BKPATH/pkginfo.txt
}
# Archives everything
tar_everything()
{
remove_exist
create_dir
pkg_save
tar_home
tar_i386conf
tar_etc
tar_usrobj
tar_usrsrc
tar_usrdoc
tar_usrports
tar_usrlocaletc
}
# Begins processing
process()
{
create_dir
if [ $1 ]; then
for loopvar in $*
{
case "$loopvar" in
[Hh][Oo][Mm][Ee])
create_dir
pkg_save
tar_home
;;
[Ee][Tt][Cc])
create_dir
pkg_save
tar_etc
;;
[Ss][Rr][Cc])
create_dir
pkg_save
tar_usrsrc
;;
[Oo][Bb][Jj])
create_dir
pkg_save
tar_usrobj
;;
[Dd][Oo][Cc])
create_dir
pkg_save
tar_usrdoc
;;
[Pp][Oo][Rr][Tt][Ss])
create_dir
pkg_save
tar_usrports
;;
[Ll][Oo][Cc][Aa][Ll])
create_dir
pkg_save
tar_usrlocaletc
;;
[Aa][Ll][Ll])
tar_everything
;;
*)
usage
;;
esac
}
else
tar_everything
fi
}
# Entry Point
process $*