Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
minetest init script
# backup every hour, meaning 00:05, 01:05, 02:05, 03:05, 04:05, ... 23:05
5 * * * * /etc/init.d/minetest backup
# clean old files (once a day, at 01:10
10 1 * * * /etc/init.d/minetest clean
#!/bin/bash
# /etc/init.d/minetest
# version 0.1 2012-09-22
# Mark Holmquist
### BEGIN INIT INFO
# Provides: minetest
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Minetest server
# Description: Starts the minetest server
### END INIT INFO
#Settings
SERVICE="minetestserver"
WORLD="world1"
USERNAME="minetest"
MTPATH="/home/$USERNAME/minetest"
MAPPATH="/home/$USERNAME/minetest/maps"
CPU_COUNT=1
INVOCATION="$MTPATH/bin/$SERVICE"
OPTIONS="--worldname $WORLD"
MAPPER="$MTPATH/util/minetestmapper.py"
LOGPATH="/var/log/minetest"
LOGFILE="$LOGPATH/minetest.log"
BACKUPPATH="/home/$USERNAME/backup"
set -e
. /lib/lsb/init-functions
ME=`whoami`
as_user() {
if [ $ME == $USERNAME ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
mt_start() {
echo "Starting $SERVICE...."
mkdir -p $LOGPATH
start-stop-daemon --start --chuid "$USERNAME:$USERNAME" --background --make-pidfile --pidfile /var/run/$SERVICE.pid --exec $INVOCATION -- $OPTIONS
echo "Done."
}
mt_stop() {
echo "Stopping $SERVICE... "
while test -d /proc/$(cat /var/run/$SERVICE.pid); do
killtree $(cat /var/run/$SERVICE.pid) 15
sleep 0.5
done
rm /var/run/$SERVICE.pid
echo "Done."
}
mt_status() {
status_of_proc -p /var/run/$SERVICE.pid "" "$SERVICE" && exit 0 || exit $?
}
mt_backup() {
echo "Backing up minetest"
BAKFILE="$BACKUPPATH/minetest_$WORLD_`date +%F`.tar.gz"
if [ -f "$BAKFILE" ]
then
echo "blub, backup already existed"
else
as_user "cd $MTPATH && tar -czf $BAKFILE $WORLD"
fi
echo "Backup complete"
}
mt_map() {
echo "Beginning mapping process"
$MAPPER -i $MTPATH/worlds/$WORLD -o /tmp/minetest-map.png > /dev/null
TEST=`diff /tmp/minetest-map.png $MAPPATH/current.png`
if [ "$TEST" != "" ]; then
SAVEPATH="$MAPPATH/`date +%F`.png"
cp /tmp/minetest-map.png $SAVEPATH
rm $MAPPATH/current.png
ln -s $SAVEPATH $MAPPATH/current.png
fi
echo "Mapping complete"
}
mt_cleanold() {
echo "Cleaning Old Files"
timestamp=`date +%F -d "8 days ago"`
cd $BACKUPPATH && rm -v minetest_*_${timestamp}-*.tar.gz
cd $MAPPATH && rm -v ${timestamp}-*.png
echo "Done cleaning up files"
}
#Start-Stop here
case "$1" in
start)
mt_start
;;
stop)
mt_stop
;;
restart)
mt_stop
mt_start
;;
backup)
mt_backup
mt_map
;;
map)
mt_map
;;
clean)
mt_cleanold
;;
status)
mt_status
;;
*)
echo "Usage: /etc/init.d/minetest {start|stop|backup|clean|status|restart|map}"
exit 1
;;
esac
exit 0

sirken commented Sep 5, 2014

Is the killtree command on line 55 a bash script of some kind? I'm trying to use your script to start a server. Thanks.

Hobb3s commented Jul 15, 2015

Line 73, on my install in ubuntu I had to edit to $MTPATH to $MTPATH/worlds so that the backup would properly call.
Also the stop server uses a killltree command, which looks like a custom additional script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment