#!/bin/bash
# CVS:     $Id: beckibackup,v 1.10 2009/01/20 16:39:43 becki Exp $
# Author:  Stefan Beckert ( http://wiki.think-deep.com/becki:start )
# License: Public Domain
# Description:
#   see README


# build path to configuration directory. There may be different configurations
# for different backup targets. The configuration variant may be specified as
# the first command line argument:

if [ -d "$1" ]; then
    # if first argument is a directory uses this as config dir
    pfad="$1"
else
    # otherwise take first arg as subdir in /home/<user>/.beckibackup/ and look
    # there:
    if [ $# -ge 1 ]; then
        pfad="$HOME/.beckibackup/$1"
    else
        pfad="$HOME/.beckibackup/default"
    fi
fi

# test if configuration file exists:
configfile="$pfad/conf"
if [ ! -r "$configfile" ]; then
    echo "Error: Cant read configfile $configfile!"
    exit 1
fi

# test if sources file exists:
. $configfile
sorcefile="$pfad/sources"
if [ ! -r "$sorcefile" ]; then
    echo "Error: Cant read sorces-file $sorcefile!"
    exit 2
fi


# test if target computer is reachable:
if [ "$ttype" != 'local' ] && [ "$doPing" == 'true' ] ; then
    # test if arping instead of ping can be used:
    pingcmd=arping
    #if ! which $pingcmd >/dev/null 2>/dev/null ; then
    if ! which $pingcmd &> /dev/null ; then
        pingcmd=ping
        echo "No arping found, using ping instead"
    fi

    # ping target computer:
    if ! $pingcmd -c1 -w1 $targetHost > /dev/null ; then
        echo "Error: $targetHost is not reachable!"
        exit 3
    fi
fi

# print start time to stdout:
date -Iseconds

# rsync options:
options="-avR --delete-excluded --progress"
if [ "$dryrun" == 'true' ]; then
    options="$options --dry-run"
fi
excludefile="$pfad/exclude"
if [ -r "$excludefile" ]; then
    options="$options --exclude-from $excludefile"
fi

if [ "$ttype" == 'rsyncd' ]; then
    remoteHost="$targetUser@$targetHost::"
elif [ "$ttype" == 'local' ]; then
    remoteHost=""
else
    options="$options -e $ttype"
    remoteHost="$targetUser@$targetHost:"
fi

if [ "$copyDataFromSeverToClient" == 'true' ]; then
    target="$targetDir"
else
    target="$remoteHost$targetDir"
fi

# do the work:
for sourcepath in $(cat $sorcefile) ; do
    if [ "$copyDataFromSeverToClient" == 'true' ]; then
        source="$remoteHost$sourcepath"
    else
        source="$sourcepath"
    fi
    cmd="rsync $options $source $target"
    echo $cmd
    # set DEBUG=true in your invironment, thus the conf-file wont be cluttered
    if [ "$DEBUG" != 'true' ]; then $cmd ; fi
done

# print end time to stdout:
date -Iseconds | tee $pfad/lastrun

# RSYNC OPTIONS:
#
# source directory/      copy only content of directory
# source directory       copy directory (with -r)
#
# -a --archive    archive mode, equivalent to -rlptgoD; symbolic links, devices,
#                 attributes, permissions, ownerships, etc. are preserved
#                 -rptgoD is -a without -l (no symlinks)
# -v --verbose
# -r --recursive  recurse into directories; not -R !
# -z --compress   compress file data
# -R --relative   The full path name is preserved
# --stats         give some file transfer stats
# --progress      show progress during transfer
# --delete        delete files that don't exist on the sending side
#
# after :: has to be a valid module name from /etc/rsyncd.conf of target pc
# the module name is case sensitive!
# after the module name may be specified a target directory
