Linux Bash Script for H2 Server

3,209 views
Skip to first unread message

telegardian

unread,
Nov 7, 2010, 1:32:47 AM11/7/10
to H2 Database
Good Afternoon,

I am posting this script in hopes that others may find it useful. I
have copy/pasted it straight out of my own project, so don't expect
this to work as-is. I would welcome any feedback or improvements. I
had originally included H2 as an embedded database, but later found
that multi-user concurrency was a problem, so I run it as a TCP Server
now. To save time, I put this script together to allow me to start,
stop, access the webadmin, and take backups of my db. This script was
NOT intended to be placed into /etc/init.d since it was not written
for any specific Linux/Unix distro. If for some reason the formatting
is wrong here, the source is available on my project's code
repository.

Cheers,
Chris Tusa
www.telegard.org


#!/bin/bash
#---[ File
Info ]-------------------------------------------------------------
#
# Source File: /h2.init.sh
# Version: 1.00
# Author(s): Chris Tusa <chris...@telegard.org>
# Description: Generic Shell script to start/stop H2 database
#
#-----------------------------------------------------------------------------


TELEGARD_HOME=/opt/telegard/bbs
H2_HOME=$TELEGARD_HOME/class
echo Using Embedded Database Engine in $TELEGARD_HOME
case $1 in
start)
cd $H2_HOME
echo "Starting H2 Database Server"
java -cp $H2_HOME/h2.jar org.h2.tools.Server -tcp -baseDir
$TELEGARD_HOME/db &
;;
stop)
echo "Shutting down H2 Database Server"
java -cp $H2_HOME/h2.jar org.h2.tools.Server -tcpShutdown "tcp://
localhost"
;;
web)
echo "Starting H2 Web Server Admin Tool :: [CTRL-C] to
terminate..."
java -cp $H2_HOME/h2.jar org.h2.tools.Server -web
#echo "NOTE: The webserver administration tool must be"
#echo " shutdown from either the browser interface"
#echo " or by killing the PID of the service."
;;
webremote)
echo "Starting H2 Web Server Admin Tool :: [CTRL-C] to
terminate..."
echo " *** !!! WARNING !!!! ***"
echo ""
echo "THIS MODE PERMITS REMOTE ACCESS TO YOUR MASTER DATABASE FROM
THE INTERNET!"
echo "DO NOT LEAVE THE ADMIN WEB SERVER RUNNING!! YOU
HAVE BEEN WARNED!"
echo ""
java -cp $H2_HOME/h2.jar org.h2.tools.Server -webAllowOthers
;;
backup)
echo "Taking backup of H2 Database (opentg)"
java -cp $H2_HOME/h2.jar org.h2.tools.Backup -file "$TELEGARD_HOME/
db/backup.zip" -dir "$TELEGARD_HOME/db" -db "opentg"
;;
sqldump)
java -cp $H2_HOME/h2.jar org.h2.tools.Script -url jdbc:h2:tcp://
localhost/opentg -user '' -script $TELEGARD_HOME/sqldump.zip -options
compression zip
;;
*)
echo "Syntax is: h2 [start | stop | web | webremote | backup |
sqldump]"
;;
esac

Thomas Mueller

unread,
Nov 8, 2010, 2:23:19 PM11/8/10
to h2-da...@googlegroups.com
Hi,

Thanks a lot! I wonder if there is a way to simplify using the H2
tools from the command line. You have used:

echo "Starting H2 Web Server Admin Tool :: [CTRL-C] to terminate..."
java -cp $H2_HOME/h2.jar org.h2.tools.Server -web

Currently the second line writes the following to system out:

Web server running on http://192.168.0.106:8083

Probably it should write something more user friendly, for example:

H2 Web Console running at http://192.168.0.106:8083
Press [Ctrl]+[C] to terminate

Is it "running at" or "running on"?
Is the second line (Press Ctrl+C) really needed? I mean, most people
know about that, right?

Also, the command line of the tools is a bit long:


java -cp $H2_HOME/h2.jar org.h2.tools.Server -web

You could shorten it to to:
java -jar $H2_HOME/h2.jar -web
but this doesn't print anything to system out. I guess it should.

Regards,
Thomas

Reply all
Reply to author
Forward
0 new messages