update_minecraft_bedrock.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/bin/bash
  2. # LinuxGSM update_minecraft_bedrock.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Minecraft Bedrock servers.
  7. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. #random number for userAgent
  9. randnum=$((1 + RANDOM % 5000))
  10. fn_update_minecraft_dl(){
  11. latestmcbuildurl=$(curl -H "Accept-Encoding: identity" -H "Accept-Language: en" -Ls -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.${randnum}.212 Safari/537.36" "https://www.minecraft.net/en-us/download/server/bedrock/" | grep -o 'https://minecraft.azureedge.net/bin-linux/[^"]*zip')
  12. fn_fetch_file "${latestmcbuildurl}" "" "" "" "${tmpdir}" "bedrock_server.${remotebuild}.zip"
  13. echo -e "Extracting to ${serverfiles}...\c"
  14. if [ "${firstcommandname}" == "INSTALL" ]; then
  15. unzip -oq "${tmpdir}/bedrock_server.${remotebuild}.zip" -x "server.properties" -d "${serverfiles}"
  16. else
  17. unzip -oq "${tmpdir}/bedrock_server.${remotebuild}.zip" -x "permissions.json" "server.properties" "whitelist.json" -d "${serverfiles}"
  18. fi
  19. local exitcode=$?
  20. if [ "${exitcode}" == "0" ]; then
  21. fn_print_ok_eol_nl
  22. fn_script_log_pass "Extracting to ${serverfiles}"
  23. chmod u+x "${serverfiles}/bedrock_server"
  24. fn_clear_tmp
  25. else
  26. fn_print_fail_eol_nl
  27. fn_script_log_fatal "Extracting to ${serverfiles}"
  28. fn_clear_tmp
  29. core_exit.sh
  30. fi
  31. }
  32. fn_update_minecraft_localbuild(){
  33. # Gets local build info.
  34. fn_print_dots "Checking local build: ${remotelocation}"
  35. # Uses log file to gather info.
  36. # Log is generated and cleared on startup but filled on shutdown.
  37. requirerestart=1
  38. localbuild=$(grep Version "${consolelogdir}"/* 2>/dev/null | tail -1 | sed 's/.*Version //')
  39. if [ -z "${localbuild}" ]; then
  40. fn_print_error "Checking local build: ${remotelocation}"
  41. fn_print_error_nl "Checking local build: ${remotelocation}: no log files containing version info"
  42. fn_print_info_nl "Checking local build: ${remotelocation}: forcing server restart"
  43. fn_script_log_error "No log files containing version info"
  44. fn_script_log_info "Forcing server restart"
  45. check_status.sh
  46. # If server stopped.
  47. if [ "${status}" == "0" ]; then
  48. exitbypass=1
  49. command_start.sh
  50. fn_firstcommand_reset
  51. sleep 3
  52. exitbypass=1
  53. command_stop.sh
  54. fn_firstcommand_reset
  55. # If server started.
  56. else
  57. exitbypass=1
  58. command_stop.sh
  59. fn_firstcommand_reset
  60. fi
  61. fi
  62. if [ -z "${localbuild}" ]; then
  63. localbuild=$(grep Version "$(ls -tr "${consolelogdir}"/* 2>/dev/null)" | tail -1 | sed 's/.*Version //')
  64. fi
  65. if [ -z "${localbuild}" ]; then
  66. localbuild="0"
  67. fn_print_error "Checking local build: ${remotelocation}: waiting for local build: missing local build info"
  68. fn_script_log_error "Missing local build info"
  69. fn_script_log_error "Set localbuild to 0"
  70. else
  71. fn_print_ok "Checking local build: ${remotelocation}"
  72. fn_script_log_pass "Checking local build"
  73. fi
  74. }
  75. fn_update_minecraft_remotebuild(){
  76. # Gets remote build info.
  77. remotebuild=$(curl -H "Accept-Encoding: identity" -H "Accept-Language: en" -Ls -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.${randnum}.212 Safari/537.36" "https://www.minecraft.net/en-us/download/server/bedrock/" | grep -o 'https://minecraft.azureedge.net/bin-linux/[^"]*' | sed 's/.*\///' | grep -Eo "[.0-9]+[0-9]")
  78. if [ "${firstcommandname}" != "INSTALL" ]; then
  79. fn_print_dots "Checking remote build: ${remotelocation}"
  80. # Checks if remotebuild variable has been set.
  81. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  82. fn_print_fail "Checking remote build: ${remotelocation}"
  83. fn_script_log_fatal "Checking remote build"
  84. core_exit.sh
  85. else
  86. fn_print_ok "Checking remote build: ${remotelocation}"
  87. fn_script_log_pass "Checking remote build"
  88. fi
  89. else
  90. # Checks if remotebuild variable has been set.
  91. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  92. fn_print_failure "Unable to get remote build"
  93. fn_script_log_fatal "Unable to get remote build"
  94. core_exit.sh
  95. fi
  96. fi
  97. }
  98. fn_update_minecraft_compare(){
  99. # Removes dots so if statement can compare version numbers.
  100. fn_print_dots "Checking for update: ${remotelocation}"
  101. localbuilddigit=$(echo -e "${localbuild}" | tr -cd '[:digit:]')
  102. remotebuilddigit=$(echo -e "${remotebuild}" | tr -cd '[:digit:]')
  103. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ]||[ "${forceupdate}" == "1" ]; then
  104. fn_print_ok_nl "Checking for update: ${remotelocation}"
  105. echo -en "\n"
  106. echo -e "Update available"
  107. echo -e "* Local build: ${red}${localbuild}${default}"
  108. echo -e "* Remote build: ${green}${remotebuild}${default}"
  109. echo -en "\n"
  110. fn_script_log_info "Update available"
  111. fn_script_log_info "Local build: ${localbuild}"
  112. fn_script_log_info "Remote build: ${remotebuild}"
  113. fn_script_log_info "${localbuild} > ${remotebuild}"
  114. unset updateonstart
  115. check_status.sh
  116. # If server stopped.
  117. if [ "${status}" == "0" ]; then
  118. exitbypass=1
  119. fn_update_minecraft_dl
  120. if [ "${requirerestart}" == "1" ]; then
  121. exitbypass=1
  122. command_start.sh
  123. fn_firstcommand_reset
  124. exitbypass=1
  125. command_stop.sh
  126. fn_firstcommand_reset
  127. fi
  128. # If server started.
  129. else
  130. fn_print_restart_warning
  131. exitbypass=1
  132. command_stop.sh
  133. fn_firstcommand_reset
  134. exitbypass=1
  135. fn_update_minecraft_dl
  136. exitbypass=1
  137. command_start.sh
  138. fn_firstcommand_reset
  139. fi
  140. unset exitbypass
  141. date +%s > "${lockdir}/lastupdate.lock"
  142. alert="update"
  143. alert.sh
  144. else
  145. fn_print_ok_nl "Checking for update: ${remotelocation}"
  146. echo -en "\n"
  147. echo -e "No update available"
  148. echo -e "* Local build: ${green}${localbuild}${default}"
  149. echo -e "* Remote build: ${green}${remotebuild}${default}"
  150. echo -en "\n"
  151. fn_script_log_info "No update available"
  152. fn_script_log_info "Local build: ${localbuild}"
  153. fn_script_log_info "Remote build: ${remotebuild}"
  154. fi
  155. }
  156. # The location where the builds are checked and downloaded.
  157. remotelocation="minecraft.net"
  158. if [ "${firstcommandname}" == "INSTALL" ]; then
  159. fn_update_minecraft_remotebuild
  160. fn_update_minecraft_dl
  161. else
  162. fn_print_dots "Checking for update"
  163. fn_print_dots "Checking for update: ${remotelocation}"
  164. fn_script_log_info "Checking for update: ${remotelocation}"
  165. fn_update_minecraft_localbuild
  166. fn_update_minecraft_remotebuild
  167. fn_update_minecraft_compare
  168. fi