#!/bin/sh
#
# Rubi's Ultimate Backup Implementation
#
# Version 4.0, (C) 3.1999 by Beat Rubischon <beat@rubis.ch>
#                  8.1999 by Beat Rubischon <beat.rubischon@trivadis.com>
#                  2.2000 by Beat Rubischon <beat@rubis.ch>
#                  2.2004 by Beat Rubischon <rubischon@phys.ethz.ch>
#                  7.2007 by Beat Rubischon <beat@0x1b.ch>
#                 10.2010 by Beat Rubischon <beat.rubischon@dalco.ch>
#                 12.2015 by Beat Rubischon <beat.rubischon@dalco.ch>
#
# 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 should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# $Id: functions.sh,v 1.13 2015/12/01 15:49:29 beat Exp $
#
# Subroutines for rubi

# -----------------
# do the dirty work
# -----------------
doit () {

#
# do backup
    RET=0

    mkdir -p $DSTDIR
    cd $DSTDIR

    echo "* * * Backup report * * *"
    echo "----------------------------------------------------------------------------"
    echo "Backing up:             $SRCHOST:$SRCPART"
    echo "        to:             $DSTDIR"
    if [ "$OLDDST" = "" ]; then
        echo "last backup in:         no previus backup done"
    else
        echo "last backup in:         $OLDDST"
    fi


    if [ ! -f rsync.log ]; then
        touch rsync.log

        echo "Start time:             `date`"

        for p in $SRCPART; do
          # add trainling slash
          case "$p" in
            */)
            ;;
        
            *)
              p="$p/"
            ;;
          esac

          # prepare rsync command    
          c="${RSYNC} -axH --stats --numeric-ids"

          # add link destination if available
          if [ "$OLDDST" != "" ]; then
              c="$c --link-dest='${OLDDST}${p}'"
          fi
   
          # local copy for localhost
          if [ "${SRCHOST}" = "localhost" ]; then
              c="$c '${p}' '${DSTDIR}${p}'"
          else
              c="$c '${SRCHOST}:${p}' '${DSTDIR}${p}'"
          fi
          # create target directory if it does not yet exist
          if [ ! -d '${DSTDIR}${p}' ]; then
            mkdir -p "${DSTDIR}${p}"
          fi 
          # show and run command
          echo
          echo "$c"
          eval "$c" 2>&1 | tee -a rsync.log
          ret=${PIPESTATUS[0]}
          # check return value
          if [ $ret -ne 0 -a $ret -ne 24 ]; then
            RET=1
            break
          fi
        done

        # make sure cleanup.pl finds a recent timestamp
        touch "${DSTDIR}"

        echo
        echo "End time:               `date`"

#        echo "----------------------------------------------------------------------------"
#        echo
    
        # summarize statistics
        echo
        $PREFIX/lib/report.pl rsync.log
        rm -f rsync.log
        echo
    else
        echo
        echo "Backup allready running!"
        echo
        echo "Check for orphan rubi and rsync processes and delete"
        echo "$PWD/rsync.log in case no other backup is running."
        RET=1
    fi

    if [ $RET -eq 0 ]; then
        echo $DSTDIR > $BASEDST/lastdst

#
# expunge
        if [ "$KEEPG" != "" ]; then
            echo "Available backups:"
            $PREFIX/lib/cleanup.pl "$BASEDST" $KEEPG $KEEPD
        else
            if [ "$KEEPS" = "" ]; then
	        $FIND $BASEDST -maxdepth 1 -ctime +$KEEPD -type d \
	          | (while read dir; do
	            echo -n "deleting $dir..."
	            rm -Rf $dir
	            rm -f $dir.log
	            echo "done."
	            done)
            else
	        $FIND $BASEDST -maxdepth 1 -ctime +$KEEPD -type d \
	          | egrep -v "$KEEPS" \
	          | (while read dir; do
	            echo -n "deleting $dir..."
	            rm -Rf $dir
	            rm -f $dir.log
	            echo "done."
	            done)
            fi
        fi

        echo
	echo "* * * Backup successfull * * *"
	echo
#
# failed
    else
	rmdir $DSTDIR 2>/dev/null
	echo
	echo "* * * Backup failed * * *"
	echo
    fi
}
