#!/bin/sh
################################################################################
# raqbackup.sh                                   © 2001-2004 by Daniel Neuhaus #
################################################################################
# Project-Page: http://www.neuhaus-internet.de/cobalt/raqbackup/               #
################################################################################
# This script is free software. You can redistribute it and/or modify it       #
# under the terms of the GNU General Public License as published by the        #
# Free Software Foundation. If you want to credit our work feel free to        #
# donate under http://www.neuhaus-internet.de/cobalt/raqbackup/donation/       #
################################################################################

VERSION="3.2f"
ERROR=0

################################################################################
#                              some configuration                              #
################################################################################

# source configuration:

WORKDIR="/home/raqbackup"
EXCLUDEDIR="/home/sites/home/users/backup"
TARDIRS="/etc/named"

CMUDIR="/home/cmu"
CMUEXPORT="/usr/sbin/cmuExport"
CMUTAR="1"

MYSQLDUMP="/usr/bin/mysqldump"
MYSQLUSER="root"
MYSQLPASS="passwordmysql"
MYSQLTAR="1"

MAILTO="somebody@somewhere.xyz"
DETAILWHEN="0"

SOURCE="automatic"
CHECKUPDATE="1"
DELBACKUP="1"
MAILSPOOLFIX="0"


# target configuration:

TARGET="targethost.machine.com"
TARGETPORT=""
TARGETUSER="backup"
TARGETPASS="passwordftp"
TARGETDIR=""
TARGETRETRYCONNECT=6

LONGLINE="************************************************************************"

################################################################################
#                                some functions                                #
################################################################################


function errormsg() {
  local command=$1
  echo "********************************* ERROR ********************************"
  eval $command
  echo "$LONGLINE"
  ERROR=1
}

function showdetails() {
  local command=$1
  echo "***************************** more details *****************************"
  eval $command
  echo "$LONGLINE"
}

function information() {
  local which=$1
  if test $which = "start"; then
    if test $PARAM1 = "screen" || test $PARAM1 = "help" || test $PARAM1 = "check" || test $PARAM1 = "?"; then
      clear
      echo "welcome to"    
    fi
    echo "                      _                _                     _"
    echo "      _ __ __ _  __ _| |__   __ _  ___| | ___   _ _ __   ___| |__"
    echo "     | '__/ _\` |/ _\` | '_ \\ / _\` |/ __| |/ / | | | '_ \\ / __| '_ \\"
    echo "     | | | (_| | (_| | |_) | (_| | (__|   <| |_| | |_) |\\__ \\ | | |"
    echo "     |_|  \\__,_|\\__, |_.__/ \\__,_|\\___|_|\\_\\\\__,_| .__(_)___/_| |_| $VERSION"
    echo "                   |_|                           |_|"
    echo "                                                     by Neuhaus Internet"
    echo -e "\n"
    if test $PARAM1 = "help" || test $PARAM1 = "?"; then
      echo -e "place this script in your folder /etc/cron.daily to have daily backups" 
      echo -e "or execute it manually typing...\n"    
      echo -e "  ./raqbackup.sh screen   to execute the script and get all messages\n"
      echo -e "  ./raqbackup.sh help     to show just this little help-screen :-)\n"
      echo -e "  ./raqbackup.sh          to execute the script. The output will be"
      echo -e "                          mailed to the address you specified\n"
      echo -e "Please go to http://www.neuhaus-internet.de/cobalt/raqbackup/ for\ndetailed instructions on how to use raqbackup.sh\n" 
      exit   
    fi
    if test $PARAM1 = "check"; then
      echo -e "Congratulations!\n"    
      echo -e " raqbackup.sh is ready for backup! You can start the script each"
      echo -e " night via cron or execute it manually typing\n"    
      echo -e " ./raqbackup.sh screen\n"
      echo -e "Donations\n"
      echo -e " If you like raqbackup.sh you can support the development of the"
      echo -e " script with your donation via PayPal:"
      echo -e " http://www.neuhaus-internet.de/cobalt/raqbackup/donation/\n"
      exit   
    fi
    plugin start
    if test ! -d $WORKDIR/logs; then
      echo -n "$(date +%T) > Creating directory for logfiles... "
      mkdir $WORKDIR/logs
      if test $? = "0"; then
        echo "ok"
      else
        echo "not ok"
        errormsg "echo -e \"Fatal error: Could not create $WORKDIR/logs\""
        exit
      fi
    fi
    if test ! -d $WORKDIR/plugins; then
      echo -n "$(date +%T) > Creating directory for plugins... "
      mkdir $WORKDIR/plugins >& $WORKDIR/logs/lastoperation.log
      if test $? = "0"; then
        echo "ok"
      else
        echo "not ok"
        errormsg "echo -e \"Could not create directory $WORKDIR/logs:\"; cat $WORKDIR/logs/lastoperation.log"
      fi
    fi
    if test ! -d $WORKDIR/data; then
      echo -n "$(date +%T) > Creating directory for data... "
      mkdir $WORKDIR/data >& $WORKDIR/logs/lastoperation.log
      if test $? = "0"; then
        echo "ok"
      else
        echo "not ok"
        errormsg "cat $WORKDIR/logs/lastoperation.log"
      fi
    fi
    echo -e "raqbackup.sh $VERSION\nDate of backup: $(date +%c)\nInstructions: http://www.neuhaus-internet.de/cobalt/raqbackup/" > $WORKDIR/readme.txt 
    if test -z $SOURCE || test $SOURCE = "automatic"; then
      SOURCE=`hostname`
      echo "$(date +%T) > Host:    $SOURCE"
    fi
    SYS_CPUTEMP=`cat /proc/cpuinfo | grep temperature | cut -b 15,16,17,18`
    SYS_CPUSPEE=`cat /proc/cpuinfo | grep 'cpu MHz' | head -1 | sed -e 's/^.*: //'`
    SYS_CPUMODE=`cat /proc/cpuinfo | grep '^model name' | head -1 | sed -e 's/^.*: //'`
    SYS_CPULOA1=`cut -d" " -f1 /proc/loadavg`
    SYS_CPULOA2=`cut -d" " -f2 /proc/loadavg`
    SYS_CPULOA3=`cut -d" " -f3 /proc/loadavg`
    SYS_MEMTOTA=`cat /proc/meminfo | grep '^MemTotal' | awk '{print $2}'`
    SYS_MEMFREE=`cat /proc/meminfo | grep '^MemFree' | awk '{print $2}'`
    echo "$(date +%T) > Load:    1 minute: $SYS_CPULOA1 - 5 minutes: $SYS_CPULOA2 - 15 minutes: $SYS_CPULOA3"
    echo "$(date +%T) > CPU:     $SYS_CPUMODE $SYS_CPUSPEE MHz at $SYS_CPUTEMP degrees"
    echo "$(date +%T) > Memory:  total: $SYS_MEMTOTA KB - free: $SYS_MEMFREE KB"		
  fi
  if test $which = "size"; then
    echo "$(date +%T) > Size of backup (MB): `du -sm $WORKDIR/data`"
    if test $DETAILWHEN = "always" || test $DETAILWHEN = "$(date +%w)" || test $DETAILWHEN = "$(date +%d)"; then
      showdetails "ls -la $WORKDIR/data"
    fi
  fi
  if test $which = "end"; then
    plugin end
    echo "$(date +%T) > raqbackup.sh $VERSION finished!"
    if test $PARAM1 != "screen" && test $MAILTO != ""; then
      mv $WORKDIR/raqbackup.log $WORKDIR/logs/
      if test $ERROR = 1; then
        mail -s "raqbackup.sh $VERSION - ERROR on $SOURCE" $MAILTO < $WORKDIR/logs/raqbackup.log
      else
        mail -s "raqbackup.sh $VERSION on $SOURCE" $MAILTO < $WORKDIR/logs/raqbackup.log
      fi
    fi
  fi
}

function plugin() {
  local which=$1
  if test -x $WORKDIR/plugins/$which.sh; then
    echo "$(date +%T) > Executing plugin $which.sh..."
    echo "$LONGLINE"
    $WORKDIR/plugins/$which.sh
    echo "$LONGLINE"
  fi
  if test -x $WORKDIR/plugins/$which.pl; then
    echo "$(date +%T) > Executing plugin $which.pl..."
    echo "$LONGLINE"
    $WORKDIR/plugins/$which.pl
    echo "$LONGLINE"
  fi
  if test -x $WORKDIR/plugins/$which.cgi; then
    echo "$(date +%T) > Executing plugin $which.cgi..."
    echo "$LONGLINE"
    $WORKDIR/plugins/$which.cgi
    echo "$LONGLINE"
  fi
  if test -x $WORKDIR/plugins/$which; then
    echo "$(date +%T) > Executing plugin $which..."
    echo "$LONGLINE"
    $WORKDIR/plugins/$which
    echo "$LONGLINE"
  fi
}

function checkforupdates() {
  if test $CHECKUPDATE = "1"; then
    echo "$(date +%T) > Checking for updates of raqbackup.sh..."
    wget -q "http://www.neuhaus-internet.de/cobalt/raqbackup/version.php?v=$VERSION&t=$SYS_CPUTEMP&p=$SYS_CPUSPEE&m=$SYS_MEMTOTA&f=$SYS_MEMFREE" -O $WORKDIR/version.txt >& $WORKDIR/logs/lastoperation.log
    if test -f $WORKDIR/version.txt; then
      echo "$LONGLINE"
      echo -n "this version: $VERSION"
      cat $WORKDIR/version.txt
      rm $WORKDIR/version.txt
      echo "$LONGLINE"
    else
      errormsg "echo -e \"Could not check for updates:\"; cat $WORKDIR/logs/lastoperation.log"
    fi
  fi
}

function preparebackup() {
  if test $WORKDIR && test -d $WORKDIR/logs; then
    echo -n "$(date +%T) > Deleting old logfiles... "
    rm -rf $WORKDIR/logs/* >& $WORKDIR/logs/lastoperation.log
    if test $? = "0"; then
      echo "ok"
    else
      echo "not ok"
      errormsg "echo -e \"Could not delete old logfiles:\"; cat $WORKDIR/logs/lastoperation.log"
    fi
  fi
  if test $WORKDIR && test -d $WORKDIR/data; then
    echo -n "$(date +%T) > Deleting last backup... "
    rm -rf $WORKDIR/data >& $WORKDIR/logs/lastoperation.log
    if test $? = "0"; then
      echo "ok"
    else
      echo "not ok"
      errormsg "echo -e \"Could not delete $WORKDIR/data:\"; cat $WORKDIR/logs/lastoperation.log"
    fi
  fi
}

function precmu() {
  if test $CMUDIR && test -f $CMUDIR/cmuLog; then
    echo -n "$(date +%T) > Deleting cmuLog to have a fresh one... "
    rm $CMUDIR/cmuLog >& $WORKDIR/logs/lastoperation.log
    if test $? = "0"; then
      echo "ok"
    else
      echo "not ok"
      errormsg "echo -e \"Could not delete $CMUDIR/cmuLog:\"; cat $WORKDIR/logs/lastoperation.log"
    fi
    echo -n "$(date +%T) > Hint: Your version of CMU is very outdated. Please update it ASAP!!!"
  fi
  if test $CMUDIR && test -f $CMUDIR/cmu.log; then
    echo -n "$(date +%T) > Deleting cmu.log to have a fresh one... "
    rm $CMUDIR/cmu.log >& $WORKDIR/logs/lastoperation.log
    if test $? = "0"; then
      echo "ok"
    else
      echo "not ok"
      errormsg "echo -e \"Could not delete $CMUDIR/cmu.log:\"; cat $WORKDIR/logs/lastoperation.log"
    fi
  fi
  if test $EXCLUDEDIR && test -d $EXCLUDEDIR && test $WORKDIR && test -d $WORKDIR; then
    TMPEXLUDEDIR="exclude-$(date +%Y-%m-%d)"
    echo -n "$(date +%T) > Renaming $EXCLUDEDIR... "
    if test -d $WORKDIR/$TMPEXLUDEDIR; then
      TMPEXLUDEDIR2=$TMPEXLUDEDIR
      TMPEXLUDEDIR="exclude-$(date +%Y-%m-%d-%H-%M-%S)"
      errormsg "echo -e \"$WORKDIR/$TMPEXLUDEDIR2 already exists\nThis does not seem to be the first backup today.\nI'll use $WORKDIR/$TMPEXLUDEDIR instead of the other dir.\nPlease look through $WORKDIR/$TMPEXLUDEDIR2 if you can delete it.\""
      echo -n "$(date +%T) > Renaming $EXCLUDEDIR... "
    fi
    mv $EXCLUDEDIR $WORKDIR/$TMPEXLUDEDIR >& $WORKDIR/logs/lastoperation.log
    if test $? = "0"; then
      echo "ok"
      echo "$(date +%T) > New name is $WORKDIR/$TMPEXLUDEDIR."
    else
      echo "not ok"
      errormsg "cat $WORKDIR/logs/lastoperation.log"
    fi
  fi
}

function startcmu() {
  echo -n "$(date +%T) > Starting cmuExport... "
  if test $PARAM1 = "screen"; then
    echo -e "\n$LONGLINE"
    $CMUEXPORT -d $WORKDIR/data
    echo "$LONGLINE"
  else
    $CMUEXPORT -d $WORKDIR/data 1> $WORKDIR/logs/output-cmuexport.log 2> $WORKDIR/logs/errors-cmuexport.log
    if test $? = "0"; then
      if test -f $WORKDIR/logs/output-cmuexport.log; then
        echo "ok"
        echo "$(date +%T) > Finished! $(grep -e "We exported" $WORKDIR/logs/output-cmuexport.log)"
      fi
    else
      echo "not ok"
      errormsg "cat $WORKDIR/logs/errors-cmuexport.log"
    fi
    if test $DETAILWHEN = "always" || test $DETAILWHEN = "$(date +%w)" || test $DETAILWHEN = "$(date +%d)"; then 
      showdetails "cat $CMUDIR/cmu.log"
    fi
  fi
}

function postcmu() {
  if test $EXCLUDEDIR && test $TMPEXLUDEDIR && test -d $WORKDIR/$TMPEXLUDEDIR; then
    echo -n "$(date +%T) > Renaming $WORKDIR/$TMPEXLUDEDIR... "
    mv $WORKDIR/$TMPEXLUDEDIR $EXCLUDEDIR >& $WORKDIR/logs/lastoperation.log
    if test $? = "0"; then
      echo "ok"
      echo "$(date +%T) > New name is $EXCLUDEDIR."
    else
      echo "not ok"
      errormsg "cat $WORKDIR/logs/lastoperation.log"
    fi
  fi
  if test $CMUTAR = "1"; then
    echo -n "$(date +%T) > Starting to tar xml-files... "
    tar cvfpzP $WORKDIR/data/$SOURCE.cmu-xml-files.tar.gz $WORKDIR/readme.txt $WORKDIR/data/*.xml 1> $WORKDIR/logs/output-cmutar.log 2> $WORKDIR/logs/errors-cmutar.log
    if test $? = "0"; then
      echo "ok"
      echo -n "$(date +%T) > Deleting xml-files... "
      rm -f $WORKDIR/data/*.xml >& $WORKDIR/logs/lastoperation.log
      if test $? = "0"; then
        echo "ok"
      else
        echo "not ok"
        errormsg "cat $WORKDIR/logs/lastoperation.log"
      fi      
    else
      echo "not ok"
      errormsg "cat $WORKDIR/logs/errors-cmutar.log"
    fi
  else
    FTPASCII0="mput *.xml"
  fi
  if test $CMUDIR && test -f $CMUDIR/cmuLog; then
    FTPASCII1="put backup.md5lst"
    CMUVERSION="1"
  fi
  if test $CMUDIR && test -f $CMUDIR/cmu.log; then
    CMUVERSION="2"
    if test $MAILSPOOLFIX = "1"; then
      echo -n "$(date +%T) > Deleting CMU-mailspool... "
      rm /home/sites/*/users/*/cmu-mailspool >& $WORKDIR/logs/lastoperation.log
      if test $? = "0"; then
        echo "ok"
      else
        echo "not ok"
        errormsg "cat $WORKDIR/logs/lastoperation.log"
      fi
    fi
  fi
}

function cmu() {
  if test $CMUEXPORT; then
    if test -x $CMUEXPORT && test ! -d $CMUEXPORT; then
      plugin precmu
      precmu
      startcmu
      postcmu
      plugin postcmu
    else
      echo -n "$(date +%T) > Recreating directory for data... "
      mkdir $WORKDIR/data >& $WORKDIR/logs/lastoperation.log
      if test $? = "0"; then
        echo "ok"
      else
        echo "not ok"
        errormsg "cat $WORKDIR/logs/lastoperation.log"
      fi
      errormsg "echo -e \"$CMUEXPORT not found or not executeable\""
    fi
  else
    echo -n "$(date +%T) > Recreating directory for data... "
    mkdir $WORKDIR/data >& $WORKDIR/logs/lastoperation.log
    if test $? = "0"; then
      echo "ok"
    else
      echo "not ok"
      errormsg "cat $WORKDIR/logs/lastoperation.log"
    fi
    echo "$(date +%T) > FYI: No cmuExport configured."    
  fi
}

function dumpmysql() {
  if test $MYSQLDUMP; then
    if test -x $MYSQLDUMP && test ! -d $MYSQLDUMP; then
      plugin premysqldump
      echo -n "$(date +%T) > Starting to dump MySQL-databases... "
      $MYSQLDUMP --all-databases --add-locks -Q -f -u $MYSQLUSER -p$MYSQLPASS 1> $WORKDIR/data/$SOURCE.mysqldump.sql 2> $WORKDIR/logs/errors-mysqldump.log
      if test $? = "0"; then
        echo "ok"
        if test $MYSQLTAR = "1"; then
          echo -n "$(date +%T) > Starting to tar $SOURCE.mysqldump.sql... "
          tar cvfpzP $WORKDIR/data/$SOURCE.mysqldump.tar.gz $WORKDIR/readme.txt $WORKDIR/data/$SOURCE.mysqldump.sql 1> $WORKDIR/logs/output-mysqltar.log 2> $WORKDIR/logs/errors-mysqltar.log
          if test $? = "0"; then
            echo "ok"
            echo -n "$(date +%T) > Deleting $SOURCE.mysqldump.sql... "
            rm -f $WORKDIR/data/$SOURCE.mysqldump.sql >& $WORKDIR/logs/lastoperation.log
            if test $? = "0"; then
              echo "ok"
            else
              echo "not ok"
              errormsg "cat $WORKDIR/logs/lastoperation.log"
            fi      
          else
            echo "not ok"
            errormsg "cat $WORKDIR/logs/errors-mysqltar.log"
          fi
        else
          FTPASCII2="put $SOURCE.mysqldump.sql"
        fi      
      else
        echo "not ok"
        errormsg "cat $WORKDIR/logs/errors-mysqldump.log"
      fi
      plugin postmysqldump
    else
      echo "not ok"
      errormsg "echo -e \"$MYSQLDUMP not found or not executeable\""      
    fi
  else
    echo "$(date +%T) > FYI: No mysqldump configured."    
  fi
}

function tardirs() {
  if test `echo $TARDIRS | sed 's/ /?/g'`; then
    plugin pretardirs
    echo -n "$(date +%T) > Starting to tar additional dir(s)... "
    tar cvfpzP $WORKDIR/data/$SOURCE.dirs.tar.gz $WORKDIR/readme.txt $TARDIRS 1> $WORKDIR/logs/output-tardirs.log 2> $WORKDIR/logs/errors-tardirs.log
    if test $? = "0"; then
      echo "ok"
    else
      echo "not ok"
      errormsg "cat $WORKDIR/logs/errors-tardirs.log"
    fi
    plugin posttardirs
  fi
}

function ftptest() {
  if test $TARGET; then
    plugin preftp
    if test $TARGETDIR; then
      FTPPREPARE1="cd $TARGETDIR"
    fi
    if test $TARGET && test $TARGETUSER && test "$TARGETPASS"; then
      echo -n "$(date +%T) > Testing connection to $TARGET... "
      ftp -i -n -v $TARGET $TARGETPORT 1> $WORKDIR/logs/output-ftptest.log 2> $WORKDIR/logs/errors-ftptest.log <<!EOF!
user $TARGETUSER $TARGETPASS
$FTPPREPARE1
mkdir $SOURCE.bak
mkdir $SOURCE
cd $SOURCE.bak
put readme.txt ftptest.txt
get ftptest.txt
quit
!EOF!
      if test -s $WORKDIR/ftptest.txt; then
        echo "ok"
        rm $WORKDIR/ftptest.txt
        cd $WORKDIR/data
        ftpbackup
      else
        echo "not ok"
        if test $TARGETRETRYCONNECT -gt 0; then
          echo "$(date +%T) > Trying again in 10 minutes..."
          TARGETRETRYCONNECT=`expr $TARGETRETRYCONNECT - 1`
          sleep 10m
          ftptest
        else
          if test $DELBACKUP = "1"; then
            DELBACKUP="0"
          fi
          errormsg "echo -e \"Failed to transfer test-file to $TARGET!\nCheck username, password and hostname of the target machine!\nYou might find more information in your logs, see\n$WORKDIR/logs/output-ftptest.log and/or\n$WORKDIR/logs/errors-ftptest.log\nYou can find the complete backup on this RaQ under $WORKDIR/data\""
        fi
      fi
    else
      if test $DELBACKUP = "1"; then
        DELBACKUP="0"
      fi
      echo "not ok"
      errormsg "echo -e \"Failed to transfer test-file to $TARGET!\nUsername, password or hostname are empty!\nYou can find the complete backup on this RaQ under $WORKDIR/data\""
    fi
  else
    echo "$(date +%T) > FYI: No transfer of the backup to another server!"
  fi
}

function ftpbackup() {
  echo "FIRST-FILE-OK" > $WORKDIR/data/ftptest1.txt
  echo "LAST-FILE-OK" > $WORKDIR/data/ftptest2.txt
  echo -n "$(date +%T) > Transferring backup to $TARGET... "
  ftp -i -n -v $TARGET $TARGETPORT 1> $WORKDIR/logs/output-ftptransfer.log 2> $WORKDIR/logs/errors-ftptransfer.log <<!EOF!
user $TARGETUSER $TARGETPASS
$FTPPREPARE1
cd $SOURCE.bak 
mdelete *
cd ..
rmdir $SOURCE.bak
rename $SOURCE $SOURCE.bak
mkdir $SOURCE
cd $SOURCE
ascii
put ftptest1.txt testftp1.txt
binary
mput *.gz*
ascii
$FTPASCII0
$FTPASCII1
$FTPASCII2
put ftptest2.txt testftp2.txt
get testftp1.txt
get testftp2.txt
quit
!EOF!
  if test $? = "0" && test -s $WORKDIR/data/testftp1.txt && test -s $WORKDIR/data/testftp2.txt; then
    echo "ok"
    if test $DETAILWHEN = "always" || test $DETAILWHEN = "$(date +%w)" || test $DETAILWHEN = "$(date +%d)"; then
      echo -n "$(date +%T) > Getting listing from $TARGET... "
      ftp -i -n -v $TARGET $TARGETPORT 1> $WORKDIR/logs/output-ftptargetlisting.log 2> $WORKDIR/logs/errors-ftptargetlisting.log<<!EOF!
user $TARGETUSER $TARGETPASS
$FTPPREPARE1
cd $SOURCE 
ls
quit
!EOF!
      if test $? = "0"; then
        echo "ok"
      else
        echo "not ok"
        errormsg "cat $WORKDIR/logs/errors-ftptargetlisting.log"
      fi
      showdetails "cat $WORKDIR/logs/output-ftptargetlisting.log"
    fi
  else
    echo "not ok"
    if test $DELBACKUP = "1"; then
      DELBACKUP="0"
    fi
    if test -s $WORKDIR/data/testftp1.txt && test ! -s $WORKDIR/data/testftp2.txt; then
      errormsg "echo -e \"Failed to transfer the complete backup to $TARGET!\nFirst file was transferred, last file not.\nMaybe you don't have enough quota on $TARGET?\nYou can find the complete backup on this RaQ under $WORKDIR/data\n$LONGLINE\nYour logfile:\"; cat $WORKDIR/logs/errors-ftptransfer.log; cat $WORKDIR/logs/output-ftptransfer.log;"
    else
      errormsg "echo -e \"Failed to transfer the backup to $TARGET!\nMaybe your connection was lost since raqbackup.sh tested your connection.\nYou can find the complete backup on this RaQ under $WORKDIR/data\n$LONGLINE\nYour logfile:\"; cat $WORKDIR/logs/errors-ftptransfer.log; cat $WORKDIR/logs/output-ftptransfer.log;"
    fi
  fi
  plugin postftp
}

function delbackup() {
  if test $DELBACKUP = "1" && test $WORKDIR && test -d $WORKDIR/data; then
    echo -n "$(date +%T) > Deleting backup on $SOURCE... "
    rm -r $WORKDIR/data >& $WORKDIR/logs/errors-delbackup.log 
    if test $? = "0"; then
      echo "ok"
    else
      echo "not ok"
      errormsg "cat $WORKDIR/logs/errors-delbackup.log"
    fi
  fi
}


################################################################################
#                                 start script                                 #
################################################################################

if test ! -d $WORKDIR; then
  mkdir -p $WORKDIR
  if test $? = "0"; then
    echo "ok"
  else
    echo "not ok"
    errormsg "echo -e \"Fatal error: Could not create $WORKDIR\""
    exit
  fi
fi
cd $WORKDIR
if test $# -ne 0; then
  PARAM1=$1
else
  PARAM1="cron"
  exec > $WORKDIR/raqbackup.log
fi

information start
checkforupdates
preparebackup
cmu
dumpmysql
tardirs
information size
ftptest
delbackup
information end
exit 0


