#!/bin/sh
#
# SYNTAX: sh rjc_bak.sh full_floppy 19991231
# SYNTAX: sh rjc_bak.sh full_cd     19991231
# SYNTAX: sh rjc_bak.sh incremental


# Miscellaneous files to backup.
tar_files_from=/root/.tar_files_from
dpkg_listing=/root/.dpkg_listing
args_valid="full_floppy incremental"
dot_incremental=~/.incremental
dot_increm_log=~/.increm_backup_log

# Whole subdirectory trees to backup.
spam=""
spam="$spam /home /root"
spam="$spam /etc/apt"
spam="$spam /etc/ssh /etc/ssh-nonfree"
spam="$spam /var/spool/exim /var/log/exim /var/mail /var/spool/mail"
spam="$spam /etc/ppp/peers /etc/ppp/resolv /etc/chatscripts"
# What about newsgroups? -2001.06.28

#
folds2bak=""    
for dir in $spam; do if [ -d $dir ]; then folds2bak="$folds2bak $dir"; fi
done

#
if [ ! $1 ]; then
	echo "Pick one: ${args_valid}"
	exit 1
fi

# Update files-from.
function files_from_update {
    files_from_tmp=/tmp/files.from.$$

    # List of specific files found anywhere under / (but not
    #   small edits: see rjc_deb.py).
    #
    if [ -e $tar_files_from ]; then
	echo "Including ${tar_files_from}..."
	for file in `cat $tar_files_from`
	do
	    if [ -e $file ]; then
		switch=on
		if [ "$1" == "incremental" ]; then
		    if [ $file -ot $dot_incremental ]; then switch=off;
		    fi
		fi
		if [ "$switch" == "on" ]; then echo $file >> $files_from_tmp;
		fi
	    fi
	done
    fi
}

# Shared by full_* functions.
function prepare_for_full {
    # echo "Updating ${dpkg_listing}..."; dpkg -l > $dpkg_listing
    echo "Removing all previous logs..."; rm -i /home/homeroot_*.log

    full_log=/home/homeroot_${ymd}.log
    du --human-readable --total ${folds2bak} > ${full_log}
}

# Full floppy backup.
function full_floppy {
    sort -o $files_from_tmp $files_from_tmp

    echo "Creating tar archive..."

    tar_gz=${full_log%.log}.tar.gz
    tar cvf - --files-from=${files_from_tmp} ${folds2bak%\/} \
	--exclude $tar_gz \
	--exclude $full_log 2>>${full_log} | \
	gzip -9c \
	| floppybackup /dev/fd0 HomeRoot_${ymd}

	# --HARD-DRIVE
	# > $tar_gz
	# ... then use mkisofs to prepare for CD burn!

	# --FLOPPY
	# | floppybackup /dev/fd0 HomeRoot_${ymd}

    touch -t ${ymd}0001 ${dot_incremental}
    less $files_from_tmp
    less $full_log
}

# Full CD backup.
function full_cd {
    # Put folders into path-file list and put just one folder
    # on command line.
    #
    # for fold in $folds2bak; do echo ${fold%/}/ >> $files_from_tmp
    # done
    sort -o $files_from_tmp $files_from_tmp

    # tar cvf - --files-from=${files_from_tmp} ${folds2bak%\/} \
	# --exclude $full_log 2>>${full_log} | \
	# gzip -9c | floppybackup /dev/fd0 HomeRoot_${ymd}

    isofs=/tmp/isofs.$$

    # Duplicate file names... see posting to debian-user week of
    # July 27, 2001.
    #
    exit 1

    mkisofs -R -L \
	-o $isofs -nobak \
	-log-file ${full_log} -path-list ${files_from_tmp} \
	-P "213_365_0960__ROBERT_CYMBALA__POB_861117_LA_CA_90086_USA" \
	-m ${full_log} \
	${folds2bak}

    # touch -t ${ymd}0001 ${dot_incremental}
    # less $files_from_tmp
    # less $full_log
    ls -l $isofs
}

# Incremental floppy backup.
function incremental {
    # echo "i am incremental"

    if [ ! -e ${dot_incremental} ]; then
	echo "File not found: ${dot_incremental}"; exit 1
    fi

    for spam in ${folds2bak}
    do
	echo "Finding files newer than ${dot_incremental}: $spam..."
	find $spam -type f -newer ${dot_incremental} >> ${files_from_tmp}
    done

    sort -o $files_from_tmp $files_from_tmp
    tar cvf - --files-from=${files_from_tmp} \
	--exclude $dot_increm_log 2> ${dot_increm_log} | \
	gzip -9c | floppybackup /dev/fd0
    less $files_from_tmp
    less $dot_increm_log
}

# Date check.
case $1 in
    full_*)
	ymd=$2
	if [ ! "${ymd/[1-2][0-9][0-9][0-9][0-9][0-9][0-9][0-9]}" == "" ] || \
		[ "${ymd}" == "" ]; then
	    echo "Need a Year/Month/Day (example, 20010416): ${ymd}"
	    exit 1
	fi;;
esac

# MAIN:
pushd /
case $1 in
    "full_floppy")
	prepare_for_full
	files_from_update $1
	full_floppy $* ;;
    "full_cd")
	prepare_for_full
	files_from_update $1
	full_cd $* ;;
    "incremental")
	echo "Updating ${dpkg_listing}..."; dpkg -l > $dpkg_listing
	files_from_update $1
	incremental $* ;;
    *)
	echo "Pick one: ${args_valid}"
	exit 1;;
esac

popd
# rm -f $files_from_tmp
exit 0

###
#