#!/bin/bash
cd /opt/euro-beta
nice ./systools/autobackup

E3G_ADDR=`cat /etc/network/interfaces | grep -A4 ' eth0 '| grep address | awk '{ print $2;}'`
TMP_REPLICA=/tmp/$1"_replica.sql"

if [ "A$2" = "A$E3G_ADDR" ]
then
  echo `date`" - Replica DB: Failed (same host) -  Exit 2" > log/replica.log
  exit 2
fi
echo "### Generated by replica_db $1 from $2 on "`date` > $TMP_REPLICA
echo "drop database if exists $1;" >> $TMP_REPLICA
echo "create database $1;" >> $TMP_REPLICA
echo "use $1" >> $TMP_REPLICA
if nice mysqldump -f -c --add-drop-table --add-locks -h$2 -ueuro3g -peuro3g $1 >> $TMP_REPLICA 2>/dev/null
then
  if nice cat $TMP_REPLICA | nice mysql -f -ueuro3g -peuro3g
  then
    echo `date`" - Replica DB: Exit OK" > log/replica.log
    exit 0
  fi
fi        

echo `date`" - Replica DB: Failed -  Exit 1" > log/replica.log
exit 1


