#!/bin/bash

# create a dump on a external harddisk
# or better said: create a dump on the disk/dir where
# this file is placed!

prefix=/usr
exec_prefix=/usr

##
# For each HOST you should define the directories to backup 
##
case $HOSTNAME in
        elektron*)
        DIRS="/home/miekg/Documents"
        ;;
        *)
        # no such host
        zenity --error --title "rdup @ $HOSTNAME" --text "No backups defined for <b>$HOSTNAME</b>"
        exit 1
esac

echo "You need to edit this script and remove the next line, exiting" >&2
exit 1 # REMOVE THIS

##
# Don't need to edit anything below this line
##

if [ ! -x /usr/sbin/rdup ]; then 
        zenity --error --title "rdup @ $HOSTNAME" --text "rdup can not be found"
        exit 1
fi

# get the path were we live
if [[ $0 =~ ^/ ]]; then
        p=$0
 else
        p=`pwd`/$0
fi

d=`date +%Y%m`
mountpath=`dirname $p`

if [[ -z $DIRS ]]; then
         zenity --error --title "rdup @ $HOSTNAME" --text "No backup directories defined"
         exit 1
fi

# only to get root 
gksudo -m "Perform backup of <b>$HOSTNAME</b> to $mountpath as root?" -t "rdup @ $HOSTNAME" "cat /dev/null"
if [[ $? -ne 0 ]]; then
        exit
fi

## Set the directories ##
STAMP="$mountpath/$HOSTNAME/$HOSTNAME.timestamp"
LIST="$mountpath/$HOSTNAME/$HOSTNAME.list"
BACKUPDIR="$mountpath/$HOSTNAME"
BACKUPDIR_DATE="$mountpath/$HOSTNAME/$d"

# create top-level backup dir
sudo mkdir -m 755 -p $BACKUPDIR
# check if the date exists
if [[ ! -d "$BACKUPDIR_DATE" ]]; then
        # kill the timestamp and inc list
        sudo rm -f "$LIST"
        sudo rm -f "$STAMP"
        TEXT="<i>Full</i> dump of <b>$HOSTNAME</b>: $DIRS"
else
        TEXT="<i>Incremental</i> dump of <b>$HOSTNAME</b>: $DIRS"
fi

sudo /usr/sbin/rdup -N $STAMP $LIST $DIRS |\
sudo /usr/sbin/rdup-mirror -v -b $BACKUPDIR_DATE 2>&1 |\
zenity --progress --pulsate --title "rdup @ $HOSTNAME" --text "$TEXT"

exit 0
