#!/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
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-`"
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}
if [ "$pb" != "$cbd" ]; then
rm -rf $cbd
mv $pb $cbd
+ log continuing $pb as $cbd ...
fi
rsrc=""
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
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