Guest User

Minetest startup script minetest_start.sh

a guest
May 11th, 2014
14
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. #Author: Ritchie
  4. #Thanks to Pitriss for suggestions and advices
  5. #Licence: WTFPL
  6.  
  7. #Startup script for minetestserver. Does world backup and world map if mapper is configured.
  8. #Script can be used with cron, then will keep minetestserver running if was stopped. Recommended location for script is /usr/local/bin/.
  9. #You can redirect output from this script to log file, script will inform you what happened if something went wrong.
  10. #Example of line in file /etc/crontab
  11. #*/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
  12.  
  13. #To know all used variables, run this script with debug parameter: $ ./minetest_start.sh debug
  14.  
  15. #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.
  16.  
  17. #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).
  18.  
  19. ### config section
  20. # All pathes to directories must have a slash "/" at end!
  21.  
  22. #path to world
  23. WORLD="/opt/minetest/world/classic_technic_game/"
  24. #path to minetest directory
  25. MINETEST="/opt/minetest/"
  26. #path to backup directory
  27. BACKUPS="/opt/minetest-data/world-backups/"
  28. #path to maps directory
  29. MAP_DIR="/opt/minetest-data/maps/"
  30. #path to mapper directory
  31. MAPPER="/opt/minetest-git/minetest-mapper-cpp/"
  32. #path to directory with minetestserver logs
  33. LOGS="/opt/minetest-data/logs/"
  34. #path to temporary directory
  35. TEMP="/opt/minetest-data/temp"
  36. #path to temporary file which is created if minetestserver crashed
  37. TEMPFILE="/opt/minetest-data/minetest_crashed.txt"
  38. #path to startup log where to save text output from minetest_mapper, can be the same as used in crontab file as described above
  39. STARTLOG="/opt/minetest-data/logs/minetest_start/minetest_start.log"
  40. #limit of disc usage in percent, if you have full disc device, script does nothing
  41. DISCMAX="95"
  42.  
  43. ### end of config section
  44.  
  45. #variable which returns today's date and time in format DDMMYYYY_HH:MM:SS, for example 27102013_21:04:06
  46. NOW="$(date +%d%m%Y_%T)"
  47. #name of world
  48. NAME="$(echo "$WORLD" | awk 'BEGIN { FS = "/" } {print $(NF-1)}')"
  49. #path to minetest/world/
  50. MT_WORLD="$(echo "$WORLD" | awk 'BEGIN { FS = "/"; OFS = "/" } {$(NF-1)=""; print$0}' | rev | cut -c2- | rev)"
  51. #path and name of backup file
  52. BACKUP_TGZ="$BACKUPS$NAME$NOW.tar.gz"
  53. #name of backup file
  54. TGZ="$NAME$NOW.tar.gz"
  55. #path and name of output map image
  56. MAP_OUT="$MAP_DIR$NAME$NOW.png"
  57. #name of output map image
  58. MAP="$NAME$NOW.png"
  59. #path and name of logfile
  60. LOGFILE="$LOGS$NAME$NOW.txt"
  61. #base of filesystem path to temporary directory to check free space
  62. BASE="$(echo "$TEMP" | awk -F"/" '{print "/"$2}')"
  63.  
  64. #debug of variables
  65. if [ "$1" = "debug" ] ; then
  66. echo WORLD $WORLD
  67. echo MINETEST $MINETEST
  68. echo BACKUPS $BACKUPS
  69. echo MAP_DIR $MAP_DIR
  70. echo MAPPER $MAPPER
  71. echo LOGS $LOGS
  72. echo NOW $NOW
  73. echo NAME $NAME
  74. echo MT_WORLD $MT_WORLD
  75. echo BACKUP_TGZ $BACKUP_TGZ
  76. echo TGZ $TGZ
  77. echo MAP_OUT $MAP_OUT
  78. echo MAP $MAP
  79. echo LOGFILE $LOGFILE
  80. echo TEMP $TEMP
  81. echo BASE $BASE
  82. echo TEMPFILE $TEMPFILE
  83. echo STARTLOG $STARTLOG
  84. echo DISCMAX $DISCMAX
  85. exit 0
  86. fi
  87.  
  88. #date and time when job started
  89. echo "Script started at time" $NOW
  90.  
  91. #check for temp file
  92. if [ ! -f "$TEMPFILE" ] ;
  93. then
  94.   #check for available disk space
  95.   if [ `df | grep $BASE | awk '{print $5}' | rev | cut -b2- | rev` -lt $DISCMAX ] ;
  96.   then
  97.     #clean temp directory
  98.     cd $TEMP ; rm -rf * ; cd - &> /dev/null
  99.     #copy world to temp directory
  100.     cp -rp $WORLD $TEMP
  101.  
  102.     #check if copy succeeded or failed
  103.     if [ $? == 0 ] ;
  104.     then
  105.       #run minetestserver
  106.       $MINETEST/bin/minetestserver &> $LOGFILE &
  107.       #wait some time to let minetestserver load all needed data
  108.       sleep 10
  109.  
  110.       #check if minetestserver is running
  111.       if [ `ps -e | grep -c minetestserver` -ge 1 ] ;
  112.       then
  113.         #backup of world to *tar.gz in backup directory
  114.         cd $TEMP ; tar cf - $NAME | gzip -9 - > $BACKUP_TGZ ; cd - &> /dev/null
  115.         #make symbolic link to $BACKUPS$NAME-latest.tar.gz
  116.         cd $BACKUPS ; ln -sf $TGZ $NAME-latest.tar.gz ; cd - &> /dev/null
  117.         #make symbolic link from actual logfile to logfile-latest.txt
  118.         cd $LOGS ; ln -sf $LOGFILE $NAME-latest.txt ; cd - &> /dev/null
  119.  
  120.         #check if mapper is ready
  121.         if [ -x "$MAPPER/minetest_mapper" ] ;
  122.         then
  123.           #do a map
  124.           $MAPPER/minetest_mapper --draworigin --geometry -3000:-3000+6000+6000 -i $TEMP/$NAME/ -o $MAP_OUT >> $STARTLOG 2>&1
  125.           #symbolic link to $MAP_DIR$NAME-latest.png
  126.           cd $MAP_DIR ; ln -sf $MAP $NAME-latest.png ; cd - &> /dev/null
  127.         else
  128.           echo "Warning: minetest_mapper is not ready"
  129.         fi
  130.  
  131.       else
  132.         echo $NOW >> $TEMPFILE
  133.         echo "ERROR: minetestserver crashed"
  134.       fi
  135.  
  136.     else
  137.       echo "ERROR: copying failed"
  138.     fi
  139.  
  140.     #clean temp directory
  141.     cd $TEMP ; rm -rf * ; cd - &> /dev/null
  142.   else
  143.     echo "ERROR: no space left on device"
  144.   fi
  145.  
  146. else
  147.   echo "ERROR: temporary file exists - minetestserver crashed"
  148. fi
RAW Paste Data