From 2e7a9906433dc6a6eab5359ad2428c8759a56afa Mon Sep 17 00:00:00 2001
From: root <root@beagle.fritz.box>
Date: Sat, 3 Oct 2015 09:12:57 +0000
Subject: [PATCH] more features + trigger fixed

---
 bin/backup      | 80 ++++++++++++++++++++++++++++++++++++++++++-------
 config/tx2.conf |  5 +++-
 2 files changed, 74 insertions(+), 11 deletions(-)

diff --git a/bin/backup b/bin/backup
index f69dfa0..6b04a56 100755
--- a/bin/backup
+++ b/bin/backup
@@ -1,5 +1,10 @@
 #!/bin/bash
 
+function log {
+	conf=`basename $config | sed 's/\.config$//'`
+	[ "$dolog" = "1" ] && echo "`date` - $@" >> $logdir/${conf}.log
+}
+
 if [ -z "$1" ]; then
 	echo usage: $0 configfile
 	exit -1
@@ -13,6 +18,7 @@ fi
 config=$1
 
 host=`grep ^host $config | cut -d ' ' -f 2`
+aliases="`grep ^aliases $config | cut -d ' ' -f 2-`"
 user=`grep ^user $config | cut -d ' ' -f 2`
 homedirs="` grep ^homedirs $config | cut -d ' ' -f 2-`"
 extradirs="`grep ^extradirs $config | cut -d ' ' -f 2-`" 
@@ -20,21 +26,66 @@ replicas=`grep ^replicas $config | cut -d ' ' -f 2`
 oldest=`grep ^oldest $config | cut -d ' ' -f 2`
 data=`grep ^data $config | cut -d ' ' -f 2`
 bandwidth=`grep ^bandwidth $config | cut -d ' ' -f 2`
+bwconn="`grep ^bandwidth $config | cut -d ' ' -f 3-`"
+compression=`grep ^compression $config | cut -d ' ' -f 2`
+compconn="`grep ^compression $config | cut -d ' ' -f 3-`"
+logdir=`grep ^logdir $config | cut -d ' ' -f 2`
 
-ping -c1 $host > /dev/null 2>&1
-ret=$?
+dolog=0
+if [ ! -z "$logdir" ]; then
+	mkdir -p $logdir
+	[ -d $logdir ] && dolog=1
+fi
 
-if [ "$ret" = "0" ]; then
-	echo "host $host is online ..."
-else
-	echo "host $host unreachable ..."
+hit=0
+for conn in $host $aliases; do
+	ping -c1 $conn > /dev/null 2>&1
+	ret=$?
+	if [ "$ret" = "0" ]; then
+		remote=$conn
+		break
+	fi
+done
+if [ -z "$remote" ]; then
+	log "host $host ($aliases) unreachable ..."
 	exit -3
+else
+	log "host $host (via $remote) is online ..."
 fi
 
+comp=6
+if [ ! -z "$compression" ]; then
+	comp=$compression
+	for cpair in "$compconn"; do
+		ch=`echo $cpair | cut -d ':' -f 1`
+		if [[ "$remote" == "$ch"* ]]; then
+			cl=`echo $cpair | cut -d ':' -f 2`
+			[[ "$cl" == [0-9] ]] && comp=$cl
+		fi
+	done
+fi
+rcomp="-z --compress-level $comp"
+log using compression level $comp ...
+
+bw=0
+if [ ! -z "$bandwidth" ]; then
+	bw=$bandwidth
+	for bwpair in "$bwconn"; do
+		ch=`echo $bwpair | cut -d ':' -f 1`
+		if [[ "$remote" == "$ch"* ]]; then
+			bwl=`echo $cpair | cut -d ':' -f 2`
+			[ ! -z "$bwl" ]] && bw=$bwl
+		fi
+	done
+fi
+rbw="--bandwidth=$bw"
+log applying bandwidth of $bw ...
+
 if [ ! -d $data ]; then
-	echo "no data directory ..."
+	log no data directory ...
 	exit -4
 fi
+log backing up to $data ...
 
 today=`date -I`
 backupdir=$data/${user}_at_${host}
@@ -51,6 +102,7 @@ for pb in $backupdir/[0-9]*; do
 		if [ "$pb" != "$cbd" ]; then
 			rm -rf $cbd
 			mv $pb $cbd
+			log continuing $pb as $cbd ...
 		fi
 
 		rsrc=""
@@ -61,10 +113,18 @@ for pb in $backupdir/[0-9]*; do
 		for dir in $extradirs; do
 			rsrc="$rsrc :$dir"
 		done
-		$rsync $user@$host$rsrc $cbd
+		[ ! -z "$homedirs" ] && \
+			log backing up home directories $homedirs ...
+		[ ! -z "$extradirs" ] && \
+			log backing up directories $extradirs ...
+		$rsync $user@$remote$rsrc $cbd
 		ret=$?
-		[ "$ret" != "0" ] && exit -50
+		if [ "$ret" != "0" ]; then
+			log backup terminated before completion ...
+			exit -50
+		fi
 		touch $backupdir/.$pb
+		log "backup completed :)"
 	fi
 	ob="$ob $pb"
 done
@@ -73,7 +133,7 @@ cob=`echo $ob | wc -w`
 if [ $cob -gt $replicas ]; then
 	((numdel=cob-replicas))
 	todel="`echo $ob | cut -d ' ' -f 1-${numdel}`"
-	echo "to delete: $todel"
+	log to delete: $todel
 	#for dirdel in $todel; do
 	#	bdd=`basename $dirdel`
 	#	rm -r $dirdel
diff --git a/config/tx2.conf b/config/tx2.conf
index 4d950df..23ce69b 100644
--- a/config/tx2.conf
+++ b/config/tx2.conf
@@ -1,8 +1,11 @@
 host tx2
+aliases tx2vpn
 user hackbard
 homedirs .config bin briefe moritz private projects ssl
 extradirs /scratch 
 replicas 3
 oldest 60
 data /mnt/wd/ext/backups
-bandwidth 5m
+bandwidth 20m tx2vpn:5m
+compression 1 tx2vpn:4
+logdir /mnt/wd/ext/backups/log
-- 
2.39.5