#!/bin/sh
#Script autobackup, please read manual backupfile.txt
#written by newbiefan (c)2007,2008 for asus users, published under gnu gpl2
#autobackup V0.9: 17.11.2008
#changes to V0.8;  	stdout and error messages directed to logfile
#			tar started with nice default(10), no cpu overload recognized
#usage: autobackup

#use name of your backupfile
 bfname=/opt/etc/backup/backupfile

#use your loglocation, usually logs are stored in /opt/var/log
 loglocation=/opt/var/log/autobackup.log

#if you do not need any log-file, uncomment
#loglocation=/dev/null
#if you want to use logger, exchange any echo command with 'logger -t' and remove '>> $loglocation'

 echo "=======================================================================" >> $loglocation
 echo "Autobackup started at" `date` >> $loglocation
#we count all lines of your backupfile
 lincount=`wc -l $bfname | cut -d " " -f1`
  let hmlines=$lincount-1
   echo "Your backupfile has $hmlines entries" >> $loglocation
   echo >> $loglocation
 
i=1 #we must have 1 because we start with 2nd line

while [ $i -ne $lincount ] ; do  #start the loop over each line in backupfile, except first line
	let i=$i+1
	#grab data from backupfile
	ipaddress=`head -n$i $bfname|tail -n1|cut -d: -f1`
	sharename=`head -n$i $bfname|tail -n1|cut -d: -f2`
	mountto=`head -n$i $bfname|tail -n1|cut -d: -f3`
	backupfrom=`head -n$i $bfname|tail -n1|cut -d: -f4`
	backupto=`head -n$i $bfname|tail -n1|cut -d: -f5`
	filesystem=`head -n$i $bfname|tail -n1|cut -d: -f6`
	user=`head -n$i $bfname|tail -n1|cut -d: -f7`
	passwd=`head -n$i $bfname|tail -n1|cut -d: -f8`
 echo "--------autobackup $backupfrom to $backupto------------" >> $loglocation
 #uncomment one of the countping lines (they have the same result)
 #countping=`ping -c 1 $ipaddress | grep 'received' | awk -F',' '{ print $2}' | awk '{ print $1}'`
  countping=`ping -c 1 $ipaddress | grep received | cut -d " " -f4`
    if [ $countping -eq 0 ] ; then
        # failed Host is not available 
        echo "Host $ipaddress is down, no backup possible at" `date` >> $loglocation
      else
        echo "Host $ipaddress is up at" `date` ", starting backup" >> $loglocation
        echo "Used data:" `head -n$i $bfname|tail -n1` >> $loglocation
              #check for existing mountpoint-directory
		if [ -d $mountto ] ; then
		   echo "Mountpoint-directory $mountto exists, nothing to do" >> $loglocation 
		  else
		   mkdir $mountto >>$loglocation 2>&1 
		   echo "Directory $mountto created" >> $loglocation
		fi #end of mountpoint-check
              #Now we have to mount your share if necessary
		 dslash="//"
		if mount | grep $dslash$ipaddress$sharename ; then   #share is already mounted
   		   echo "Share $dslash$ipaddress$sharename is already mounted to $mountto" >> $loglocation
 		  else # is not mounted, therefore we have to mount the share  
		   /opt/bin/smbmount $dslash$ipaddress$sharename $mountto -o rw,guest >> $loglocation 2>&1
		   sleep 1
		   echo "Share $dslash$ipaddress$share mounted to $mountto" >> $loglocation #
		fi #end of mount your share check
		if [ $filesystem = "FAT" ]; then #see documentation for FAT and rsync
		   echo "Backup is done for following files:" >> $loglocation
  		   its_a_tar=.tar.gz              
                 nice tar cvzf $backupto$its_a_tar $backupfrom >> $loglocation 2>&1
		   echo "Stored to $backupto$its_a_tar" >> $loglocation
		  else  # its a regular filesystem like ext2 ext3 
		   echo "Backup is done for following files:" >> $loglocation
#                 rsync -ruv delete progress $backupfrom $backupto
		    rsync --stats -h --verbose -r -t --log-file=$loglocation $backupfrom $backupto >> $loglocation 2>&1
		   echo "Stored to $backupto" >> $loglocation
		fi
 	echo >> $loglocation #just a emty line as separator
    fi #end of host_is_alivecheck
done #end of each line in backupfile, job is done
echo "Script autobackup ends at" `date` >> $loglocation
echo >> $loglocation