#!/bin/sh
#
#   Validate and Backup Firebird databases using Firebird
# standard tools
#
#   You can adjust FB_ROOT, FB_BIN, FB_DATA, FB_BACKUP and 
# BACKUP_COUNT variables to fit your own requirements. 
# For instance, if you use an official package of Firebird for your
# ditribution, you may have to change FB_ROOT to /usr/bin.
#
# The contents of this file are subject to the Initial 
# Developer's Public License Version 1.0 (the "License"); 
# you may not use this file except in compliance with the 
# License. You may obtain a copy of the License from
# the Firebird Project website, at 
# http://www.firebirdsql.org/index.php?op=doc&id=idpl.
#
# Software distributed under the License is distributed 
# on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, 
# either express or implied. See the License for the 
# specific language governing rights and limitations under 
# the License.
#
# The Original Code is licenced under IDPL
#
# The Initial Developer of the Original Code is 
#   Pierre Yager (pierre@levosgien.net).
#
# Portions created by ______________________
# are Copyright (C) ______ _________________.
#
# All Rights Reserved.
#
# Contributor(s): _________________________.
#
 
FB_ROOT=/opt/firebird
FB_BIN=${FB_ROOT}/bin
BACKUP_COUNT=5
 
# You shouldn't have to make any changes below
 
if [ ! `id -u` = 0 ]
then
  echo You are not root...
  exit 67
fi
 
# Loads the ISC_USER and ISC_PASSWORD environment variables
source ${FB_ROOT}/SYSDBA.password
 
export ISC_USER
export ISC_PASSWORD

#  Returns the filename without extension, whatever the 
#+ extension is...
function namename()
{
  local name=${1##*/}
  local name0="${name%.*}"
  echo "${name0:-$name}"
}
 
# Cleanup old backup files
function cleanup()
{
  local PREFIX=$1
 
  # list files by time in reverse order 
  local FILES=`ls -rt ${FB_BACKUP}/${PREFIX}*.fbk.gz`
 
  # count the previous files list
  local FILES_COUNT=`ls -lrt ${FB_BACKUP}/${PREFIX}*.fbk.gz | wc -l`
 
  if [ $FILES_COUNT -gt $BACKUP_COUNT ]
  then
    # Iterate the files list
    for F in $FILES
    do
      if [ $FILES_COUNT -gt $BACKUP_COUNT ]
      then
        #  And remove first files as soon as we have only 
        #+ BACKUP_COUNT files remaining
        rm $F
        FILES_COUNT=$((FILES_COUNT - 1))
      else
        break
      fi
    done
  fi
}
 
# Uses gfix to validates the database
function validate()
{
  ${FB_BIN}/gfix -validate -full ${FB_DATA}/$1
  echo $?
}
 
# Uses gbak to backup the database then compress it...
function backup()
{
  local TS=`date +"%Y%m%d_%H%M%S"`
  FB_BACKUP=/srv/website/$1/storage/backup
  ${FB_BIN}/gbak -b /srv/db/$1/$1.fdb ${FB_BACKUP}/$1_${TS}.fbk
  gzip ${FB_BACKUP}/$1_${TS}.fbk
  cleanup $1
}

backup enquirysolve

backup medw

backup northwaypc

backup lsces

backup viewyourbusiness

backup piwik

