Skip to content
Snippets Groups Projects
newservice.sh.example 2.95 KiB
#!/bin/bash
# ================================================================================
#
# LOCALDUMP :: NEW SERVICE
#
# ================================================================================

if [ -z "$BACKUP_TARGETDIR" ]; then
  echo "ERROR: you cannot start $(basename $0) directly"
  rc=$rc+1
  exit 1
fi

# --------------------------------------------------------------------------------
# CONFIG
# --------------------------------------------------------------------------------

SERVICEFOUND=0

# --------------------------------------------------------------------------------
# FUNCTIONS
# --------------------------------------------------------------------------------

function checkRequirements(){

  j_requireBinary "client"             1
  j_requireBinary "dumper"             1
  j_requireProcess "service1|service2" 1

  if [ ! -d $SOURCE_DIR ]; then
    echo "INFO: directory $SOURCE_DIR doees not exist."
    rc=$rc+1
  fi

  # set flag and reset return code
  test $rc -eq 0 && SERVICEFOUND=1
  rc=0
}

# detect and backup all databases of the service
function doBackup(){

  create_targetdir

  for DATABASE_DIR in __DATABASE_LIST__
  do

    DUMPCOMMAND  [params] "$DATABASE" > "$OUTFILE" 
    fetchrc

    # $myrc is last returncode - set in fetchrc
    if [ $myrc -eq 0 ]; then
        echo -n "gzip ... "
        compress_file "$OUTFILE"
    else
        echo "ERROR occured - no gzip"
    fi
    ls -l "$OUTFILE"*
    echo
  done

}

# restore database dump file into database
# param  string  database dump file (gzipped)
# param  string  optional: database to import; default: database is parsed from file
function restoreByFile(){
  sMyfile=$1
  sMyDb=$2

  if [ -z "$sMyDb" ]; then
    h2 "analyze dump $sMyfile"
    sMyDb=$(guessDB $sMyfile)
    echo "detected db schema from file: [${sMyDb}]"
  else
    echo "db schema from param 2: [${sMyDb}]"
  fi

  echo

  echo import to "$sMyDb"...

  h2 ensure that database exists ...
  color cmd
  echo "CREATE DATABASE IF NOT EXISTS ${sMyDb};" | mysql
  color reset

  h2 import ...
  ls -l "$sMyfile"
  echo "import to database [${sMyDb}]"
  color cmd
  zcat "$sMyfile" | mysql "${sMyDb}"
  fetchrc
  color reset

}

# --------------------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------------------


# ----- set action

checkRequirements >/dev/null 2>&1
if [ $SERVICEFOUND -eq 0 ]; then
  echo "INFO: service [$SERVICENAME] is not avilable on this machine."
fi

case $1 in
  check)
    # repeat check ... but show output
    echo
    echo Details:
    checkRequirements
    echo
    ;;
  backup)    
    test $SERVICEFOUND -eq 1 && doBackup
    ;;
  restore)
    shift 1
    test $SERVICEFOUND -eq 1 && restoreByFile $*
    ;;
  *)
    echo ERROR: wrong syntax: 
    echo $0 $*
    exit 1
esac

echo "INFO: $0 $* [$SERVICENAME] final returncode rc=$rc"

# --------------------------------------------------------------------------------