#!/bin/bash
#
# ========================================================================
#
# *
# * This file is part of MARS, the MAGIC Analysis and Reconstruction
# * Software. It is distributed to you in the hope that it can be a useful
# * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
# * It is distributed WITHOUT ANY WARRANTY.
# *
# * Permission to use, copy, modify and distribute this software and its
# * documentation for any purpose is hereby granted without fee,
# * provided that the above copyright notice appear in all copies and
# * that both that copyright notice and this permission notice appear
# * in supporting documentation. It is provided "as is" without express
# * or implied warranty.
# *
#
#
#   Author(s): Daniela Dorner  10/2006 <mailto:dorner@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2008
#
#
# ========================================================================
#
# This script backups the Magic Database. 
# In addition it writes out the commands how to create the database. 
#

today=`date +%F`
olday=`date +%F --date="-30day"`

source `dirname $0`/sourcefile
printprocesslog "INFO starting $0"

set -C

path=/home/`whoami`/budb
logpath=$path/log
mkdir -pv $logpath
logfile=$logpath/budb$today.log
oldlog=$logpath/budb$olday.log
file=$path/alldatabases
end=.sql.bz2
zipfile=$file$today$end
oldzip=$file$olday$end

dbname=MyMagic
command=$path/Create$dbname$today.txt
oldcommand=$path/Create$dbname$olday.txt

date > $logfile
getdbsetup

echo "today: $today" >> $logfile 2>&1
echo "date to remove: $olday" >> $logfile 2>&1

echo "removing old files..." >> $logfile 2>&1
rm -v $oldzip >> $logfile 2>&1
rm -v $oldlog >> $logfile 2>&1
rm -v $oldcommand >> $logfile 2>&1

echo "writing all databases to $zipfile..." >> $logfile 2>&1

# complete databases 
if ! mysqldump --host=$ho --all-databases -u dump | bzip2 -9 -c >| $zipfile 2>> $logfile
then
   printprocesslog "ERROR mysqldump failed "
   echo "ERROR mysqldump failed " >> $logfile 2>&1
fi
echo "writing create commands to $command..." >> $logfile 2>&1
# commands to create MyMagic
if ! mysqldump --host=$ho --database $dbname -u dump --no-data >| $command 2>> $logfile
then
   printprocesslog "ERROR mysqldump failed "
   echo "ERROR mysqldump failed " >> $logfile 2>&1
fi

finish >> $logfile 2>&1

