#!/bin/bash  
# http://mkreder.com/2011/06/18/backup-script/

echo $0 is not yet configured
exit -1	# is not yet configured

export DISPLAY=:0

#Vars for remote computer

DSTUSR=user
DSTROOT=root
DSTIP=192.168.1.2
DSTFOLDER=/path/to/destination/backup/folder/

#Vars for my OpenWrt router
LINKSYS=192.168.1.1
LINKSSHPORT=22
LINKUSER=root

PCTRL=0
PCTRL2=0

#this part checks if I'm on my LAN, if not it tries to connect to
# my OpenVPN server running in my Linksys with OpenWrt

echo "Checking if my router is on this network"
ssh $LINKSYS -p $LINKSSHPORT -l $LINKUSER ls &> /dev/null
PSTATE1=$?;
while [ ! $PSTATE1 -eq 0 ]; do
    echo "it isn't"
    if [ ! -f /var/lock/subsys/openvpn ]; then
    echo "openvpn is down, let's try to connect" 
    $HOME/vpn $(Xdialog --stdout --title "OpenVPN password" --no-close \
--screen-center --password  --inputbox "Password" 10 100);
    fi
    sleep 20;
    ssh $LINKSYS -p $LINKSSHPORT -l $LINKUSER ls &> /dev/null;
    PSTATE1=$?;
    PCTRL=`expr $PCTRL + 1`
    if [ $PCTRL -eq 5 ]; then
        notify-send "connection failed" 
        exit 1
    fi  
done

#this part checks if my home computer is up, if not, it will 
#try to turn it on by WOL from my OpenWrt router.

echo "hmmm, is my Computer up?"
ssh $DSTROOT@$DSTIP ls
PSTATE2=$?;
while  [ ! $PSTATE2 -eq 0 ]; do
      echo "it's not.."
      ssh -l $LINKUSER -p $LINKSSHPORT $LINKSYS /root/wakeup &> /dev/null
      sleep 30;
      ssh $DSTROOT@$DSTIP ls
      PSTATE2=$?;
      PCTRL2=`expr $PCTRL2 + 1`       
      if [ $PCTRL2 -eq 5 ]; then
        notify-send "cannot power on remote computer";
        exit 2 ;
      fi
done
echo "it's now"

# backup part

echo "starting backup..."
notify-send "starting backup..."

date
rsync -vaHx --progress --numeric-ids $HOME/ $DSTUSR@$DSTIP:$DSTFOLDER  
notify-send "backup done"
#here it asks me if I want to turn off my home computer

Xdialog --title --stdout "remote computer is on..."  --screen-center  --yesno \
"Turn off computer?" 10 50
if [ $? -eq 0 ]; then 
        #the following line will set something in my Ethernet card that allows me to
        #wake up my computer with WOL
    ssh $DSTROOT@$DSTIP /sbin/ethtool -s eth0 wol g &> /dev/null
    ssh $DSTROOT@$DSTIP poweroff &> /dev/null
fi


syntax highlighted by Code2HTML, v. 0.9.1