#!/usr/bin/env bash
#
#   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
FB_DATA=/var/firebird
FB_BACKUP=backup
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 can be...
function namename()
{
  local name=${1##*/}
  local name0="${name%.*}"
  echo "${name0:-$name}"
}

# Cleanup old backup files
function cleanup()
{
  local PREFIX=$1
  local FILES=`ls -rt ${FB_BACKUP}/${PREFIX}*.fbk.gz` # list files by time in reverse order
  local FILES_COUNT=`ls -lrt ${FB_BACKUP}/${PREFIX}*.fbk.gz | wc -l` # count above list
  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 $1
  echo $?
}

# Uses gbak to backup the database then compress it...
function backup()
{
  local DIR=${1%/*}
  local DB=`namename $F`
  local TS=`date +"%Y%m%d_%H%M%S"`
  local BACKUP=${FB_BACKUP}/${DIR}_${DB}_${TS}.fbk
  ${FB_BIN}/gbak -b $1 $BACKUP
  gzip $BACKUP
  cleanup ${DIR}_${DB}
}

pushd ${FB_DATA} > /dev/null

if [ ! -d ${FB_BACKUP} ]
then
  mkdir ${FB_BACKUP}
fi

shopt -s nullglob

for F in */*.fdb
do
  if [ `validate $F` = 0 ]
  then
    # Only backup valid databases
    backup $F

    #  Future enhancements : write a message to stderr so that cron can send 
    #+ a mail to the DBA when databases are not valid...
  fi
done

popd > /dev/null


