Skip to content

Instantly share code, notes, and snippets.

@rux
Last active August 8, 2016 14:02
Show Gist options
  • Save rux/391687a571fc81d00e6e5a99662b09ed to your computer and use it in GitHub Desktop.
Save rux/391687a571fc81d00e6e5a99662b09ed to your computer and use it in GitHub Desktop.
h2 init script for centos
#!/bin/sh
# This stuff will be ignored by systems that don't use chkconfig.
# chkconfig: 345 87 13
# description: H2 database
# pidfile: /opt/h2/bin/h2.pid
# config:
### BEGIN INIT INFO
# Provides: H2-Server
# Required-Start:
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: H2-Server
# Description: H2 database service
### END INIT INFO
# Starts and stops the h2 database
# Some general variables
H2_VERSION=1.4.192 # Important! Set your version!
H2_HOME=/opt/h2
#JVM_OPTS="-DfunctionsInSchema=true"
JVM_OPTS=""
# starts h2 server
h2_start () {
if [ -e $H2_HOME/bin/h2.pid ]; then
echo "H2 is still running"
exit 1
fi
cd $H2_HOME/bin
# this will start h2 server with allowed tcp connection
# you can find more info in h2 tutorials
java -Xms256m -Xmx768m -cp h2-$H2_VERSION.jar $JVM_OPTS org.h2.tools.Server -tcp -tcpAllowOthers -web -webAllowOthers -baseDir $H2_HOME/db $1 > $H2_HOME/h2.log 2>&1 &
echo $! > $H2_HOME/bin/h2.pid
sleep 3
echo "H2-$H2_VERSION started. Setting multithreaded"
# Just set multi threaded on my database with name kemuri
java -cp h2-$H2_VERSION.jar $JVM_OPTS org.h2.tools.Shell -url "jdbc:h2:tcp://localhost/kemuri" -user kemuri -password kemuri -sql "SET MULTI_THREADED 1"
}
# stops h2
h2_stop () {
if [ -e $H2_HOME/bin/h2.pid ]; then
PID=$(cat $H2_HOME/bin/h2.pid)
kill -TERM ${PID}
echo SIGTERM sent to process ${PID}
rm $H2_HOME/bin/h2.pid
else
echo File $H2_HOME/bin/h2.pid not found!
fi
}
# Just to remove pid file in case you killed h2 manually
# and want to start it by script, but he thinks
# that h2 is already running
h2_zap () {
rm $H2_HOME/bin/h2.pid
}
# Backups specified database to a given path
backup () {
echo Backing up database to $1
cd $H2_HOME/bin
java -cp h2-$H2_VERSION.jar $JVM_OPTS org.h2.tools.Script -url "jdbc:h2:tcp://localhost/kemuri" -user kemuri -password kemuri -script "$1" -options compression zip
}
# Restores specified database from the given path
restore () {
echo Restoring database from $1
cd $H2_HOME/bin
java -cp h2-$H2_VERSION.jar $JVM_OPTS org.h2.tools.RunScript -url "jdbc:h2:tcp://localhost/kemuri;create=true" -user kemuri -password kemuri -script "$1" -continueOnError -options compression zip
}
case "$1" in
init)
h2_start
;;
start)
h2_start -ifExists
;;
stop)
h2_stop
;;
zap)
h2_zap
;;
restart)
h2_stop
sleep 5
h2_start -ifExists
;;
backup)
backup $2
;;
restore)
restore $2
;;
*)
echo "Usage: /etc/init.d/h2 {init|start|stop|restart|backup |restore }"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment