#!/bin/sh
# Backup program
# Copyright (c) 2006 by James F. Carter (jimc@math.ucla.edu>
# 2006-03-28, for Maemo-2.0 (Nokia 770)

# 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.
#
# You can find the GNU General Public License in your Maemo distribution
# as /usr/share/common-licenses/GPL-2; if it's missing, write to the 
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
# MA 02111-1307, USA.


# To back up, do this as root on a machine with a ssh agent:
#	ssh -A selen /root/bin/backup-selen

# Strategy: 
#   Use Gnu "find" to iterate over all files.
#   Restrict to files whose mtime OR ctime are newer than a timestamp file.
#	For the intial dump, artifically jigger the timestamp to just before
#	the machine was purchased.
#   Take the dirname, so .rsync-filter in each directory will be effective.
#	In other words, tha above test may pick files that we really don't want
#	to back up -- specifically the multimedia sample files.
#   Use rsync in "filenames from file" mode to send over the changed files.
#	-F switch: $dir/.rsync-filter does per-directory exclusions
#   If a file is deleted on the destination, this script will not know it
#	unless it's otherwise to be backed up.  Too bad; there's a limit to 
#	what we can do.

# Files are backed up if newer than a timestamp.  If nonexistent, the timestamp
# is created with a date 1 day younger than the distro (so no distro files
# will be backed up).
stamp=/root/backup.stamp
if [ ! -f $stamp ] ; then 
    for f in /etc/osso_software_version /etc/SuSE-release /sbin/init ; do
	if [ ! -f $f ] ; then continue ; fi
	tzero=`find $f -printf "%A@\n"`
	tzero=$(($tzero+86400))
	touch $stamp
	perl -e "utime $tzero, $tzero, '$stamp'"
	break
    done
fi
if [ ! -f $stamp ] ; then 
    echo "Can't initialize $stamp from distro files, exiting"
    exit 8
fi
fil=`mktemp /tmp/backup.XXXXXX`
cd /

# "Find" command line options:
#   -xdev		Do not traverse into a different filesystem.
#   -cnewer $file	Target's ctime is greater (more recent) than $file.
#   -mnewer $file	Same for modification time.
# %h means the directory containing the target file, without the basename.

find home etc root usr/sbin usr/share/certs usr/share/osso-bookmarks \
	usr/lib/ssl var/lib/gconf \
    -xdev \( -type f -o -type l \) \( -newer $stamp -o -cnewer $stamp \) \
    -printf "%h\n" | sort | uniq > $fil
if [ -s $fil ] ; then : ; else 
    echo "All files are up to date."
    rm $fil
    exit 0
fi

# rsync command line options:
#   --archive		Preserve lptgoD (symlinks, perms, times, group, owner,
#			devices).  Normally implies -r (recurse into dirs) but
#			--files-from suppresses this.
#   -r			Yes we do want to recurse into explicitly requested
#			directories (but only one level).
#   --omit-dir-times	Do not bother to update directory modification times.
#   --delete		Backup files removed on the host are removed in the
#			backup.  I want --delete-during, but [in rsync-2.6.6]
#			it causes freeing an invalid pointer.
#   --files-from=$fil	Get source file names from the file. Implies -R, meaning
#			"relative" paths, meaning append the source relative 
#			path name to the destination dirname to get the 
#			destination path name.
#   -F			Look for .rsync-filter in each directory and obey it
#			(dir-merge) as a set of filter rules if found.
#			/.rsync-filter says "- /*/" which means not to recurse
#			into subdirectories of the named dir, but this can be
#			overridden in deeper directories (if they're selected
#			explicitly to be backed up).

#    --exclude "/*/" 	The restriction to only match subdirs wasn't working.
#    --delete \

if [ -z "$HOSTNAME" ] ; then HOSTNAME=`uname -n` ; fi
rsync  \
    --rsh=ssh \
    --archive -r \
    --omit-dir-times \
    -F \
    --log-format="%o %8l %f" \
    --files-from=$fil \
    /  fafnir.cft.ca.us:/home/backup/$HOSTNAME
rc=$?
if [ $rc -ne 0 ] ; then
    echo "rsync exited with code $rc (BAD)"
    echo "directories to be dumped are listed in $fil"
else
    echo "Backup done OK:" > $stamp.new
    date >> $stamp.new
    mv $stamp.new $stamp
    rm $fil
fi
exit $rc
