#!/bin/bash

GEMSTONE=/opt/gemstone/product
GEMSTONE_CONFIG=$GEMSTONE/seaside/etc/gemstone.conf

if [ -s $GEMSTONE_CONFIG ]; then
    . $GEMSTONE_CONFIG
else
    echo "Configuration file $GEMSTONE_CONFIG not found, aborting startup"
    exit 1
fi

if [ -s $GEMSTONE/seaside/etc/gemstone.secret ]; then
    . $GEMSTONE/seaside/etc/gemstone.secret
else
    echo 'Missing password file $GEMSTONE/seaside/etc/gemstone.secret'
    exit 1
fi

export GEMSTONE GEMSTONE_SYS_CONF GEMSTONE_EXE_CONF GEMSTONE_DATADIR GEMSTONE_LOGDIR \
       GEMSTONE_KEYFILE GEMSTONE_NAME PATH MANPATH LD_LIBRARY_PATH

echo "Performing backup for stone: $GEMSTONE_NAME"

$GEMSTONE/bin/topaz -l << EOF
display oops
output pushnew $GEMSTONE_LOGDIR/online_backup.log
set user DataCurator pass $GEMSTONE_CURATOR_PASS gems $GEMSTONE_NAME
login
run
| status |
status := 'failed'.
[
  GsFile gciLogServer: DateAndTime now printString.
  GsFile gciLogServer: 'Performing backup...'.
  (System suspendCheckpointsForMinutes: 5)
    ifTrue: [
      System performOnServer: 'cp $GEMSTONE_DATADIR/extent0.dbf $GEMSTONE_DATADIR/backups'.
      status := 'success'.
    ].
] ensure: [ System resumeCheckpoints].
GsFile gciLogServer: DateAndTime now printString.
GsFile gciLogServer: '...backup status: ', status.
^System checkpointStatus
%
EOF


