Created
November 16, 2013 17:30
-
-
Save struts2spring/7502878 to your computer and use it in GitHub Desktop.
H2 database as linux service or daemon Unpack it to any directory you like, but this guide assumes that database files are extracted to /opt/h2
unzip h2-2013-10-19.zip
Create /etc/init.d/h2 file with following content: sudo chmod 755 h2
sudo chkconfig --add h2
sudo service h2 init
sudo service h2 start access h2 webconsole at http://localhost:8082
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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.3.168 # 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 SB | |
java -cp h2-$H2_VERSION.jar $JVM_OPTS org.h2.tools.Shell -url "jdbc:h2:tcp://localhost/SB" -user SB -password SB -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/SB" -user SB -password SB -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/SB;create=true" -user SB -password SB -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