PATH=$PATH:/usr/bin:/bin:/usr/local/bin
export PATH
dir="c:\rsyncd"
#wincmd="/usr/local/bin/winexe -A /home/backuppc/creds"
#testing kerberos auth
/usr/kerberos/bin/kinit -kt /home/backuppc/backuppc2.keytab backuppc/backuppc.int@DOM.INT
wincmd="/usr/local/bin/winexe --option=winsserver=wins.server.int -k 1"
#winver=`$wincmd //$1 "cmd.exe /c ver"|cut -d "[" -f 2|cut -d " " -f 2|cut -d "." -f 1,2|sed '1,1d'`
buserver=`hostname -i`
#this error comes back to stderr, so get rid of it using sed. has no effect on the script.
err="EPOLL_CTL_ADD failed (Operation not permitted) - falling back to select()"

echo *****Start of pre-backup script*****

echo This is Vista.

echo Checking to see if the client is installed...
cltinstalled=`$wincmd //$1 "cmd.exe /c if exist c:\rsyncd\rsync.exe (echo 1) else echo 0"|sed -e "s/$err//g"|tr [:cntrl:] :|sed 's/://g'`
#echo $cltinstalled
if [ "$cltinstalled" != "1" ]; then
        echo Client is not installed, running install script.
        /home/backuppc/dev-install_client.sh $1
        echo Continuing backup...
else
       echo Client is installed.
fi

echo Cleaning up from \(potentially\) failed previous backups

echo Removing old drive mapping...
$wincmd  //$1 "cmd.exe /c $dir\dosdev -r -d x:"| sed -e "s/$err//g"
echo Killing running processes...
$wincmd  //$1 "cmd.exe /c $dir\pskill rsync"| sed -e "s/$err//g"| grep -i killed
echo Done cleaning up.
echo 

echo Open host firewall to allow rsyncd connections from $buserver...
echo Allowing rsyncd application...
$wincmd  //$1 "cmd.exe /c netsh firewall set allowedprogram program = $dir\rsync.exe name = rsync mode = enable scope = CUSTOM addresses = $buserver"| sed -e "s/$err//g"
echo Allowing port 873...
$wincmd  //$1 "cmd.exe /c netsh firewall set portopening protocol = TCP port = 873 name = rsyncd mode = enable scope = CUSTOM addresses = $buserver"| sed -e "s/$err//g"
echo Allowing ICMP echo...
$wincmd  //$1 "cmd.exe /c netsh firewall set icmpsetting 8 enable"| sed -e "s/$err//g"

echo Mounting latest snapshot for backup...
shadowVol=`$wincmd  //$1 "cmd.exe /c vssadmin list shadows"|sed -e "s/$err//g"|grep "Shadow Copy Volume"|/usr/bin/tail -n 1|/bin/cut -d ":" -f 2|/bin/sed 's/^[ \t]*//;s/[ \t]*$//'`
$wincmd  //$1 "cmd.exe /c echo set SHADOW_DEVICE_1=$shadowVol >$dir\rsyncd-setvar.cmd && start $dir\rsyncd-exec.cmd"|sed -e "s/$err//g"

echo Sleep for 15 seconds to allow snapshot to mount
/bin/sleep 15

echo Checking to make sure the processes have started...
plist=`$wincmd  //$1 "tasklist /FI \"USERNAME eq backuppc\"\"| sed -e "s/$err//g"|/bin/sed '1,3d'|/bin/grep -iv tasklist|/usr/bin/wc -l`
counter=1
newsleep=15
while [ "$plist" -lt "2" -a "$counter" -lt "6" ]; do
        echo 
        echo Processes did not start, retry number $counter
        #Better kill these just in case we didn't wait long enough...
        echo Removing old drive mapping...
        $wincmd  //$1 "cmd.exe /c $dir\dosdev -r -d x:"| sed -e "s/$err//g"
        echo Killing running processes...
        $wincmd  //$1 "cmd.exe /c $dir\pskill rsync"| sed -e "s/$err//g"| grep -i killed
        echo Done cleaning up.
        echo 
        echo Attempting to start a new snapshot
        shadowVol=`$wincmd  //$1 "cmd.exe /c vssadmin list shadows"|sed -e "s/$err//g"|grep "Shadow Copy Volume"|/usr/bin/tail -n 1|/bin/cut -d ":" -f 2|/bin/sed 's/^[ \t]*//;s/[ \t]*$//'`
        $wincmd  //$1 "cmd.exe /c echo set SHADOW_DEVICE_1=$shadowVol >$dir\rsyncd-setvar.cmd && start $dir\rsyncd-exec.cmd"|sed -e "s/$err//g"
        let newsleep+=10
        echo Sleeping for $newsleep seconds to allow snapshot creation... A little longer to see if we are just slow
        /bin/sleep $newsleep
        plist=`$wincmd  //$1 "tasklist /FI \"USERNAME eq backuppc\""| sed -e "s/$err//g"|/bin/sed '1,3d'|/bin/grep -iv tasklist|/usr/bin/wc -l`
        let counter+=1
done


listen=`$wincmd  //$1 "netstat -an" | sed -e "s/$err//g"|grep 873|/usr/bin/wc -l`
listencounter=1
while [ "$listen" -lt "1" -a "$listencounter" -lt "6" ]; do
        echo rsyncd is not listening yet, sleeping 15 seconds to see if it comes alive....
        /bin/sleep 15
        listen=`$wincmd  //$1 "netstat -an" | sed -e "s/$err//g"|grep 873|/usr/bin/wc -l`
        let listencounter+=1
        done

#Okay lets just make sure that the RIGHT processes are running now...
rsyncdRunning=`$wincmd  //$1 "tasklist /FI \"USERNAME eq backuppc\""| sed -e "s/$err//g"|/bin/sed '1,3d'|/bin/grep -ic rsync`
cmdRunning=`$wincmd  //$1 "tasklist /FI \"USERNAME eq backuppc\""| sed -e "s/$err//g"|/bin/sed '1,3d'|/bin/grep -ic cmd`
vshadowRunning=`$wincmd  //$1 "tasklist /FI \"USERNAME eq backuppc\""| sed -e "s/$err//g"|/bin/sed '1,3d'|/bin/grep -ic vshadow`

if [ "$rsyncdRunning" -eq "0" -o "$cmdRunning" -eq "0" ]; then
        echo Processes still did not start:
        echo rsync: $rsyncdRunning
        echo cmd: $cmdRunning
        echo Dying...
        exit 1
fi

if [ "$plist" -gt "1" -a "$listen" -eq "1" ]; then
        echo Processes started, rsyncd is listening, ready for backup
else
        echo Something went wrong, see above... Dying
        exit 1
fi

echo *****End of pre-backup script*****

exit 0
