Firebird backup script

This is my first released backup script for Firebird RDBMS. It automagically search for all Firebird databases from a central directory, uses gfix to validate them, then backup + gzip them to a central backup directory keeping only 5 last successful backups.

It may be used as a cron script to automate Firebird databases backup.

#!/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 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 $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
 
#  Avoid */*.fdb to be expanded if there are no files in the 
#+ databases repository
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

Hope this will be useful. You can also download the script

Be Sociable, Share!

Tags: , , ,

  1. Donald Klopper’s avatar

    I suggest that as part of your script you also attempt to do a “restore” (to a temporary location of course) which will “close the loop” on this exercise. Unfortunately, I’ve experience many unrestorable FB production database backup files… and changed my own “backup” utilities to always attempt a restore on a backed up database and that has served me well (in the very least it gives you more peace of mind, at worst it wastes time doing the “complete” backup script, as best you will be warned when you still have a working database that it can’t be backed up.

  2. Lou’s avatar

    Cool script. I adopted a shell script years ago that I modified to read a file of databases to backup. I have several large DB’s about 7GB+ and recently have had an issue using tar to compress the files.

    I will give this a try and may modify it to read a list of databases to backup. If I do, I will post a copy back here or email to you.

  3. Bryanne’s avatar

    Interesting to know.

  4. Anderson’s avatar

    A good mod IMHO would be to backup all databases listed on aliases.conf instead of all db on some dir.

    Anyway, great script. Thanks for sharing.

  5. Trackback from sonson on 3 janvier 2009 at 16:39

Reply

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *