update_minecraft.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/bin/bash
  2. # LinuxGSM update_minecraft.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://linuxgsm.com
  5. # Description: Handles updating of Minecraft servers.
  6. commandname="UPDATE"
  7. commandaction="Update"
  8. function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  9. fn_update_dl(){
  10. fn_fetch_file "https://s3.amazonaws.com/Minecraft.Download/versions/${availablebuild}/minecraft_server.${availablebuild}.jar" "${tmpdir}" "minecraft_server.${availablebuild}.jar"
  11. echo -e "copying to ${serverfiles}...\c"
  12. fn_script_log "Copying to ${serverfiles}"
  13. cp "${tmpdir}/minecraft_server.${availablebuild}.jar" "${serverfiles}/minecraft_server.jar"
  14. local exitcode=$?
  15. if [ ${exitcode} -eq 0 ]; then
  16. fn_print_ok_eol_nl
  17. else
  18. fn_print_fail_eol_nl
  19. fi
  20. }
  21. fn_update_currentbuild(){
  22. # Gets current build info
  23. # Checks if current build info is available. If it fails, then a server restart will be forced to generate logs.
  24. if [ ! -f "${consolelogdir}/${servicename}-console.log" ]; then
  25. fn_print_error "Checking for update: mojang.com"
  26. sleep 0.5
  27. fn_print_error_nl "Checking for update: mojang.com: No logs with server version found"
  28. fn_script_log_error "Checking for update: mojang.com: No logs with server version found"
  29. sleep 0.5
  30. fn_print_info_nl "Checking for update: mojang.com: Forcing server restart"
  31. fn_script_log_info "Checking for update: mojang.com: Forcing server restart"
  32. sleep 0.5
  33. exitbypass=1
  34. command_stop.sh
  35. exitbypass=1
  36. command_start.sh
  37. sleep 0.5
  38. # Check again and exit on failure.
  39. if [ ! -f "${consolelogdir}/${servicename}-console.log" ]; then
  40. fn_print_fail_nl "Checking for update: mojang.com: Still No logs with server version found"
  41. fn_script_log_fatal "Checking for update: mojang.com: Still No logs with server version found"
  42. core_exit.sh
  43. fi
  44. fi
  45. # Get current build from logs
  46. currentbuild=$(cat "${serverfiles}/logs/latest.log" 2> /dev/null | grep version | grep -Eo '((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}')
  47. if [ -z "${currentbuild}" ]; then
  48. fn_print_error_nl "Checking for update: mojang.com: Current build version not found"
  49. fn_script_log_error "Checking for update: mojang.com: Current build version not found"
  50. sleep 0.5
  51. fn_print_info_nl "Checking for update: mojang.com: Forcing server restart"
  52. fn_script_log_info "Checking for update: mojang.com: Forcing server restart"
  53. exitbypass=1
  54. command_stop.sh
  55. exitbypass=1
  56. command_start.sh
  57. currentbuild=$(cat "${serverfiles}/logs/latest.log" 2> /dev/null | grep version | grep -Eo '((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}')
  58. if [ -z "${currentbuild}" ]; then
  59. fn_print_fail_nl "Checking for update: mojang.com: Current build version still not found"
  60. fn_script_log_fatal "Checking for update: mojang.com: Current build version still not found"
  61. core_exit.sh
  62. fi
  63. fi
  64. }
  65. fn_update_availablebuild(){
  66. # Gets latest build info.
  67. availablebuild=$(${curlpath} -s "https://launchermeta.mojang.com/mc/game/version_manifest.json" | sed -e 's/^.*"release":"\([^"]*\)".*$/\1/')
  68. # Checks if availablebuild variable has been set
  69. if [ -z "${availablebuild}" ]; then
  70. fn_print_fail "Checking for update: mojang.com"
  71. sleep 0.5
  72. fn_print_fail "Checking for update: mojang.com: Not returning version info"
  73. fn_script_log_fatal "Failure! Checking for update: mojang.com: Not returning version info"
  74. core_exit.sh
  75. elif [ "${installer}" == "1" ]; then
  76. :
  77. else
  78. fn_print_ok_nl "Checking for update: mojang.com"
  79. fn_script_log_pass "Checking for update: mojang.com"
  80. sleep 0.5
  81. fi
  82. }
  83. fn_update_compare(){
  84. # Removes dots so if can compare version numbers
  85. currentbuilddigit=$(echo "${currentbuild}" | tr -cd '[:digit:]')
  86. availablebuilddigit=$(echo "${availablebuild}" | tr -cd '[:digit:]')
  87. if [ "${currentbuilddigit}" -ne "${availablebuilddigit}" ]; then
  88. echo -e "\n"
  89. echo -e "Update available:"
  90. sleep 0.5
  91. echo -e " Current build: ${red}${currentbuild}${default}"
  92. echo -e " Available build: ${green}${availablebuild}${default}"
  93. echo -e ""
  94. sleep 0.5
  95. echo ""
  96. echo -en "Applying update.\r"
  97. sleep 1
  98. echo -en "Applying update..\r"
  99. sleep 1
  100. echo -en "Applying update...\r"
  101. sleep 1
  102. echo -en "\n"
  103. fn_script_log "Update available"
  104. fn_script_log "Current build: ${currentbuild}"
  105. fn_script_log "Available build: ${availablebuild}"
  106. fn_script_log "${currentbuild} > ${availablebuild}"
  107. unset updateonstart
  108. check_status.sh
  109. if [ "${status}" == "0" ]; then
  110. fn_update_dl
  111. exitbypass=1
  112. command_start.sh
  113. exitbypass=1
  114. command_stop.sh
  115. else
  116. exitbypass=1
  117. command_stop.sh
  118. fn_update_dl
  119. exitbypass=1
  120. command_start.sh
  121. fi
  122. alert="update"
  123. alert.sh
  124. else
  125. echo -e "\n"
  126. echo -e "No update available:"
  127. echo -e " Current version: ${green}${currentbuild}${default}"
  128. echo -e " Available version: ${green}${availablebuild}${default}"
  129. echo -e ""
  130. fn_print_ok_nl "No update available"
  131. fn_script_log_info "Current build: ${currentbuild}"
  132. fn_script_log_info "Available build: ${availablebuild}"
  133. fi
  134. }
  135. if [ "${installer}" == "1" ]; then
  136. fn_update_availablebuild
  137. fn_update_dl
  138. else
  139. # Checks for server update from mojang.com
  140. fn_print_dots "Checking for update: mojang.com"
  141. fn_script_log_info "Checking for update: mojang.com"
  142. sleep 0.5
  143. fn_update_currentbuild
  144. fn_update_availablebuild
  145. fn_update_compare
  146. fi