Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Author: Ritchie
- #Thanks to Pitriss for suggestions and advices
- #Licence: WTFPL
- #Startup script for minetestserver. Does world backup and world map if mapper is configured.
- #Script can be used with cron, then will keep minetestserver running if was stopped. Recommended location for script is /usr/local/bin/.
- #You can redirect output from this script to log file, script will inform you what happened if something went wrong.
- #Example of line in file /etc/crontab
- #*/5 * * * * minetest if [ `ps -e | grep -c minetest` = '0' ]; then /usr/local/bin/minetest_start.sh >> /opt/minetest-data/logs/minetest_start/minetest_start.log; fi >> /dev/null 2>&1
- #To know all used variables, run this script with debug parameter: $ ./minetest_start.sh debug
- #This script has many checks to ensure proper function of minetestserver. When minetestserver crashes during start for any reason (mostly broken world database file), script will make temporary file according variable named "TEMPFILE" and script in next run will do nothing, only write error message. When you fix minetestserver start, delete that temporary file.
- #Script makes symbolic links named "latest" to latest world backup, log file and map image to easily find latest files. You can make symbolic links to that "latest" files everywhere you want (for example latest map image to webserver).
- ### config section
- # All pathes to directories must have a slash "/" at end!
- #path to world
- WORLD="/opt/minetest/world/classic_technic_game/"
- #path to minetest directory
- MINETEST="/opt/minetest/"
- #path to backup directory
- BACKUPS="/opt/minetest-data/world-backups/"
- #path to maps directory
- MAP_DIR="/opt/minetest-data/maps/"
- #path to mapper directory
- MAPPER="/opt/minetest-git/minetest-mapper-cpp/"
- #path to directory with minetestserver logs
- LOGS="/opt/minetest-data/logs/"
- #path to temporary directory
- TEMP="/opt/minetest-data/temp"
- #path to temporary file which is created if minetestserver crashed
- TEMPFILE="/opt/minetest-data/minetest_crashed.txt"
- #path to startup log where to save text output from minetest_mapper, can be the same as used in crontab file as described above
- STARTLOG="/opt/minetest-data/logs/minetest_start/minetest_start.log"
- #limit of disc usage in percent, if you have full disc device, script does nothing
- DISCMAX="95"
- ### end of config section
- #variable which returns today's date and time in format DDMMYYYY_HH:MM:SS, for example 27102013_21:04:06
- NOW="$(date +%d%m%Y_%T)"
- #name of world
- NAME="$(echo "$WORLD" | awk 'BEGIN { FS = "/" } {print $(NF-1)}')"
- #path to minetest/world/
- MT_WORLD="$(echo "$WORLD" | awk 'BEGIN { FS = "/"; OFS = "/" } {$(NF-1)=""; print$0}' | rev | cut -c2- | rev)"
- #path and name of backup file
- BACKUP_TGZ="$BACKUPS$NAME$NOW.tar.gz"
- #name of backup file
- TGZ="$NAME$NOW.tar.gz"
- #path and name of output map image
- MAP_OUT="$MAP_DIR$NAME$NOW.png"
- #name of output map image
- MAP="$NAME$NOW.png"
- #path and name of logfile
- LOGFILE="$LOGS$NAME$NOW.txt"
- #base of filesystem path to temporary directory to check free space
- BASE="$(echo "$TEMP" | awk -F"/" '{print "/"$2}')"
- #debug of variables
- if [ "$1" = "debug" ] ; then
- echo WORLD $WORLD
- echo MINETEST $MINETEST
- echo BACKUPS $BACKUPS
- echo MAP_DIR $MAP_DIR
- echo MAPPER $MAPPER
- echo LOGS $LOGS
- echo NOW $NOW
- echo NAME $NAME
- echo MT_WORLD $MT_WORLD
- echo BACKUP_TGZ $BACKUP_TGZ
- echo TGZ $TGZ
- echo MAP_OUT $MAP_OUT
- echo MAP $MAP
- echo LOGFILE $LOGFILE
- echo TEMP $TEMP
- echo BASE $BASE
- echo TEMPFILE $TEMPFILE
- echo STARTLOG $STARTLOG
- echo DISCMAX $DISCMAX
- exit 0
- fi
- #date and time when job started
- echo "Script started at time" $NOW
- #check for temp file
- if [ ! -f "$TEMPFILE" ] ;
- then
- #check for available disk space
- if [ `df | grep $BASE | awk '{print $5}' | rev | cut -b2- | rev` -lt $DISCMAX ] ;
- then
- #clean temp directory
- cd $TEMP ; rm -rf * ; cd - &> /dev/null
- #copy world to temp directory
- cp -rp $WORLD $TEMP
- #check if copy succeeded or failed
- if [ $? == 0 ] ;
- then
- #run minetestserver
- $MINETEST/bin/minetestserver &> $LOGFILE &
- #wait some time to let minetestserver load all needed data
- sleep 10
- #check if minetestserver is running
- if [ `ps -e | grep -c minetestserver` -ge 1 ] ;
- then
- #backup of world to *tar.gz in backup directory
- cd $TEMP ; tar cf - $NAME | gzip -9 - > $BACKUP_TGZ ; cd - &> /dev/null
- #make symbolic link to $BACKUPS$NAME-latest.tar.gz
- cd $BACKUPS ; ln -sf $TGZ $NAME-latest.tar.gz ; cd - &> /dev/null
- #make symbolic link from actual logfile to logfile-latest.txt
- cd $LOGS ; ln -sf $LOGFILE $NAME-latest.txt ; cd - &> /dev/null
- #check if mapper is ready
- if [ -x "$MAPPER/minetest_mapper" ] ;
- then
- #do a map
- $MAPPER/minetest_mapper --draworigin --geometry -3000:-3000+6000+6000 -i $TEMP/$NAME/ -o $MAP_OUT >> $STARTLOG 2>&1
- #symbolic link to $MAP_DIR$NAME-latest.png
- cd $MAP_DIR ; ln -sf $MAP $NAME-latest.png ; cd - &> /dev/null
- else
- echo "Warning: minetest_mapper is not ready"
- fi
- else
- echo $NOW >> $TEMPFILE
- echo "ERROR: minetestserver crashed"
- fi
- else
- echo "ERROR: copying failed"
- fi
- #clean temp directory
- cd $TEMP ; rm -rf * ; cd - &> /dev/null
- else
- echo "ERROR: no space left on device"
- fi
- else
- echo "ERROR: temporary file exists - minetestserver crashed"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement