Advertisement
narthollis

Minecraft Server Luancher (with update check)

Sep 20th, 2011
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.55 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. #############################################################################
  4. # MINECRAFT SERVER LAUNCHER
  5. ###########################
  6. #
  7. # This is a rather basic server launcher that gets the HTTP Headers for the
  8. # minecraft server jar file and compares the Last-Modified header with a
  9. # local copy of the header. If these values are different the script
  10. # downloads a new version of the server jar. It then updates the local
  11. # copy of the header value.
  12. #
  13. #############################################################################
  14. # HELP:
  15. ###########################
  16. #
  17. # ./launcher.bash
  18. #    Runs the version check only
  19. #
  20. # ./laucnher.bash start
  21. #    Runs the version check and then starts the server with the defaults
  22. #
  23. # ./launcher.bash start="parms"
  24. #    Runs the version check and then starts the server with the 'params'
  25. #
  26. # ./launcher.bash path="path"
  27. #    Starts by changing the directory to 'path' and runs the version check
  28. #
  29. # ./launcher.bash archive
  30. #    If the checksums are difference, archive the world before existing
  31. #
  32. #############################################################################
  33. # LICENCE
  34. ###########################
  35. #
  36. # Copyright 2011 Nicholas Steicke. All rights reserved.
  37. #
  38. # Redistribution and use in source and binary forms, with or without modification, are
  39. # permitted provided that the following conditions are met:
  40. #
  41. #    1. Redistributions of source code must retain the above copyright notice, this list of
  42. #       conditions and the following disclaimer.
  43. #
  44. #    2. Redistributions in binary form must reproduce the above copyright notice, this list
  45. #       of conditions and the following disclaimer in the documentation and/or other materials
  46. #       provided with the distribution.
  47. #
  48. # THIS SOFTWARE IS PROVIDED BY NICHOLAS STEICKE ''AS IS'' AND ANY EXPRESS OR IMPLIED
  49. # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  50. # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NICHOLAS STEICKE OR
  51. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  52. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  53. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  54. # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  55. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  56. # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  57. #
  58. # The views and conclusions contained in the software and documentation are those of the
  59. # authors and should not be interpreted as representing official policies, either expressed
  60. # or implied, of Nicholas Steicke.
  61.  
  62. which="/usr/bin/which"
  63.  
  64. rm=`$which rm`
  65. ln=`$which ln`
  66. touch=`$which touch`
  67. cat=`$which cat`
  68. curl=`$which curl`
  69. grep=`$which grep`
  70. sed=`$which sed`
  71. java=`$which java`
  72. sha1sum=`$which sha1sum`
  73. tar=`$which tar`
  74. mkdir=`which mkdir`
  75.  
  76. python=`$which python`
  77.  
  78. SERVER_URL="https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar"
  79.  
  80. WDIR="."
  81. START=false
  82. START_PARAMS="-Xmx1024M -Xms1024M"
  83. ARCHIVE=false
  84.  
  85. while [ $# -ne 0 ]; do
  86.   if [[ ${1:0:5} == "path=" ]]; then
  87.     WDIR=${1:5}
  88.   elif [[ ${1:0:5} == "start" ]]; then
  89.     START=true
  90.     if [[ ${1:5} != "" ]]; then
  91.       START_PARAMS=${1:5}
  92.     fi
  93.   elif [[ $1 == "archive" ]]; then
  94.     ARCHIVE=true
  95.   fi
  96.   shift
  97. done
  98.  
  99. cd $WDIR
  100. echo -n "Working in "
  101. pwd
  102.  
  103. $touch .mclancher_lastmodified
  104.  
  105. CURVERSION=`$cat .mclancher_lastmodified`
  106.  
  107. echo "Checking HTTP Headers to see if their version is different..."
  108.  
  109. $curl -sIL $SERVER_URL > /tmp/mclauncher
  110.  
  111. VERSION=`$grep 'Last-Modified: ' /tmp/mclauncher | $sed 's/^Last-Modified: \([a-zA-Z0-9,: ]*\).*$/\1/'`
  112.  
  113. if [[ $python == "" ]]; then
  114.   COMPAT_VERSION=`echo -n $VERSION | $sed 's/ /_/g' | $sed 's/:/./g'`
  115.   COMPAT_CURVERSION=`echo -n $CURVERSION | $sed 's/ /_/g' | $sed 's/:/./g'`
  116. else
  117.   COMPAT_VERSION=`$python -c "from datetime import datetime; print datetime.strptime('$VERSION', '%a, %d %b %Y %H:%M:%S %Z').strftime('%Y-%m-%dT%H.%I.%S')"`
  118.   COMPAT_CURVERSION=`$python -c "from datetime import datetime; print datetime.strptime('$CURVERSION', '%a, %d %b %Y %H:%M:%S %Z').strftime('%Y-%m-%dT%H.%I.%S')"`
  119. fi
  120.  
  121. $rm /tmp/mclauncher
  122.  
  123. if [[ $VERSION != $CURVERSION ]] || [ ! -f minecraft_server.jar ]; then
  124.   if [ -f minecraft_Server.jar ]; then
  125.     echo "The server has been modified! Lets get it!"
  126.   else
  127.     echo "There is no minecraft_server.jar, so lets get one!"
  128.   fi
  129.  
  130.   echo -n $VERSION > .mclancher_lastmodified
  131.  
  132.   $curl $SERVER_URL -so minecraft_server_$COMPAT_VERSION.jar
  133.  
  134.   $sha1sum minecraft_server_$COMPAT_VERSION.jar > minecraft_server_$COMPAT_VERSION.jar.sha1sum
  135.  
  136.   if [ -f minecraft_server.jar ]; then
  137.     $rm minecraft_server.jar
  138.   fi
  139.  
  140.   $ln -s minecraft_server_$COMPAT_VERSION.jar minecraft_server.jar
  141. else
  142.   echo "Already upto date."
  143. fi
  144.  
  145. SUM=`$cat "minecraft_server_$COMPAT_VERSION.jar.sha1sum"`
  146. CURSUM=`$cat "minecraft_server_$COMPAT_CURVERSION.jar.sha1sum"`
  147.  
  148. if [[ ${SUM:0:40} == ${CURSUM:0:40} ]]; then
  149.   echo "The file is different, this was probably an update."
  150.  
  151.   if $ARCHIVE && [ -f server.properties ]; then
  152.     WORLD=`$grep 'level-name' server.properties`
  153.     WORLD=${WORLD:11}
  154.  
  155.     $mkdir -p backups
  156.  
  157.     $tar -cjf "backups/$WORLD.pre-$COMPAT_VERSION.tar.bz2" $WORLD
  158.  
  159.     echo 'Created world archive:'
  160.     echo "  backups/$WORLD.pre-$COMPAT_VERSION.tar.bz2"
  161.   fi
  162. fi
  163.  
  164. if $START; then
  165.   echo "Starting server..."
  166.   exec $java $START_PARAMS -jar minecraft_server.jar nogui
  167. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement