XML Database, Ready For Business

Sedna is arguably the most stable of the open-source and free XML Database solutions you can find today. Despite this, it is still important you set it up correctly when installing into a mission critical, production ready environment.

This article outlines a set of steps to set up the Sedna XML Database on a Linux system, ready for a production environment. Ready for large loads, queries, updates and most importantly backups and recovery from backups.

This article assumes you have a Linux system and that you are familiar with Linux at the command-line shell level.

1. Installing the Sedna XML Database

Create a user account for the Sedna XML Database, this gives you greater control over the database, it is not recommended to allow the database to run as the root user.

Log into the system as root, then from the command line shell, type:

[root@mypc ~]# useradd sedna

Get the latest copy of the Sedna XML Database, goto Sedna's Website, find the link for the latest copy of Sedna binaries for Linux, copy the address ready for the wget command.

[root@mypc ~]# cd /var/tmp
[root@mypc ~]# wget \
http://www.modis.ispras.ru/FTPContent./sedna-x.x.xx-bin-linux.sh
[root@mypc ~]# chomd +x sedna-*.sh

Follow the instructions so that Sedna will install into into /usr

[root@mypc ~]# ./sedna-x.x.xx-bin-linux.sh

The sedna user account must now own this directory and everything beneath it.

[root@mypc ~]# chown -R sedna /usr/sedna

2. Create the database instance(s) you will use later

Let's say you want to run 2 database instances from the one Sedna Server, "mydb" for production and "test-mydb" for pre-live testing. Change the user account to "sedna", and create the nessacary databases.

[root@mypc ~]# su - sedna
[sedna@mypc ~]# se_cdb mydb
[sedna@mypc ~]# se_cdb test-mydb

3. Install Sedna as a Linux Service

change user back to root

[root@mypc ~]# su - root

Take the following bash script and install it into /etc/init.d, save the filename as sedna

Also, edit the SEDNA_HOME, USER and DBLIST variables as nessacary.

#!/bin/bash

############################################################
# Sedna Database Service
# Copyright (c) 2008 Charles Foster, http://www.cfoster.net/
#
# Start, stop, restart or query the status
# of the Sedna XML Database.
#
# This service manages the both the Sedna Governor Server
# as well as loading/unloading child database instances.
#
# You are free to modify and redistribute this script,
# providing you leave the copyright message in tact.
############################################################

# Home of The Sedna Installation
SEDNA_HOME=/usr/sedna

# The user account which will run Sedna
USER=sedna

# Database instances to start/stop, separated by spaces
DBLIST="mydb test-mydb"

############################################################
# Don't edit below here, unless you know what you are doing.
############################################################

SPIDX="pidof se_gov"
DESC="Sedna XML Database Server"
IDESC="Sedna DB"
SEGOV="/etc/init.d/sedna"

. /lib/lsb/init-functions

do_start()
{
    if [ `$SPIDX` ]; then
      log_daemon_msg "$DESC already running."
    else
      log_daemon_msg "Starting $DESC"
      sudo -u $USER nohup $SEDNA_HOME/bin/se_gov > /dev/null;\
      if [ `$SPIDX` ]; then
        log_end_msg 0
      else
        log_end_msg 1
      fi
    fi
}

do_start_dbs()
{
  for n in $DBLIST;\
  do\
    log_daemon_msg "Starting $IDESC \"$n\""
    if [ ! -d $SEDNA_HOME/data/"$n"_files ]; then
      log_end_msg 1
    else
      if sudo -u $USER nohup $SEDNA_HOME/bin/se_sm $n > /dev/null; then
        log_end_msg 0
      else
        log_end_msg 1
      fi
    fi
  done
}

do_stop_dbs()
{
  if [ ! `$SPIDX` ]; then
    return
  fi

  for n in $DBLIST;\
  do\
    log_daemon_msg "Stopping $IDESC \"$n\""
    if [ ! -d $SEDNA_HOME/data/"$n"_files ]; then
      log_end_msg 1
    else
      if sudo -u $USER nohup $SEDNA_HOME/bin/se_smsd $n > /dev/null; then
        log_end_msg 0
      else
        log_end_msg 1
      fi
    fi
  done
}


get_status()
{
  if [ `$SPIDX` ]; then
      $SEDNA_HOME/bin/se_rc
  else
    echo "$DESC not running."
  fi
}

do_stop()
{
    log_daemon_msg "Stopping $DESC"
    if [ ! `$SPIDX` ]; then
      log_daemon_msg "$DESC was not running in the first place."
      log_end_msg 1
    else
      sudo -u $USER nohup $SEDNA_HOME/bin/se_stop > /dev/null
      if [ ! `$SPIDX` ]; then
        log_end_msg 0
      else
        log_end_msg 1
      fi
    fi
}

case "$1" in
  start)
      do_start
      do_start_dbs
    ;;
  status)
      get_status
    ;;
  stop)
      do_stop_dbs
      do_stop
    ;;
  restart)
      do_stop_dbs
      do_stop
      do_start
      do_start_dbs
    ;;
  *)
    echo "Usage: $SEGOV {start|stop|status|restart}" >&2
    exit 1
  ;;
esac

4. Set up a cron job to backup the database

Create a directory to place backups of the XML Database

[root@mypc ~]# cd /var
[root@mypc ~]# mkdir sedna-backups

Copy and paste the following bash script, and save it to /home/sedna/backup.sh

Also, edit the SEDNA_HOME, DBLIST, BACKUP_TO_DIR, DEL_OLD_AFTER variables as nessacary.

#!/bin/bash

############################################################
# Sedna Database backup script
# Copyright (c) 2008 Charles Foster, http://www.cfoster.net/
#
# This script takes a hot backup of defined database
# instances using Sedna's Hot Backup program. Then creates
# a .tar.bz2.
#
# This script should generally be executed as a cron job.
#
# You are free to modify and redistribute this script,
# providing you leave the copyright message in tact.
############################################################

# Home of The Sedna Installation
SEDNA_HOME=/usr/sedna

# Database instances to back up, separated by spaces
DBLIST="mydb test-mydb"

# Directory to store the backups.
BACKUP_TO_DIR=/var/sedna-backups

# Delete backups that are older than (x) days
DEL_OLD_AFTER=30

############################################################
# Don't edit below here, unless you know what you are doing.
############################################################

tar_and_bzip()
{
  cd $BACKUP_TO_DIR

  for n in `ls -d backup-*`;\
  do\
    tar -cf sedna-"$n.tar" $n
    bzip2 -9 sedna-"$n.tar"
    echo "Created .tar.bz2 file from backup directory"
    rm -fdr $n
    echo "Removed backup directory generated from Sedna Hot Backup"
  done

  cd $OLDPWD
}

if [ ! -d $BACKUP_TO_DIR ]; then
  mkdir $BACKUP_TO_DIR
fi

for n in $DBLIST;\
do\
  if [ ! -d $SEDNA_HOME/data/"$n"_files ]; then
    echo "Database $n does not appear to exist."
  else
    echo "Performing a Hot Backup of Sedna DB \"$n\"."
    if $SEDNA_HOME/bin/se_hbp -c -d $BACKUP_TO_DIR $n; then
      tar_and_bzip
      echo "Removing previous backups older than $DEL_OLD_AFTER days."
      find $BACKUP_TO_DIR/* -mtime +$DEL_OLD_AFTER -exec rm {} \;
    else
      echo "Failed to perform Hot Backup of Sedna DB \"$n\"."
    fi
  fi
done

Create the actual cron job which will execute this backup script, let's say once every hour.

[root@mypc ~]# su - sedna
[sedna@mypc ~]# crontab -e

Add the following line to the sedna user account's cron tab script.

0 * * * * /home/sedna/backup.sh

4. Starting, Stopping and finding the status of the XML Database Server

Everything should now be setup, the following commands can be executed as rootroot.

Start XML Database Server and instances "mydb" and "test-mydb":

[root@mypc ~]# service sedna start

Stop XML Database Server and instances "mydb" and "test-mydb":

[root@mypc ~]# service sedna stop

Query the status of the XML Database Server and instances "mydb" and "test-mydb":

[root@mypc ~]# service sedna status

To do a complete restart of the database and instances "mydb" and "test-mydb":

[root@mypc ~]# service sedna restart

Finally, when the Linux system starts, it will automatically start the XML Database server with the instances "mydb" and "test-mydb" loaded and when the system shuts down it will safely stop the database instance along with shutting the XML Database Server down.

5. How to restore from a previous back up.