update_mcb.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/bin/bash
  2. # LinuxGSM update_mcb.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Handles updating of Minecraft Bedrock servers.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. fn_update_dl() {
  9. fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "bedrock_server.${remotebuildversion}.zip" "nochmodx" "norun" "noforce" "nohash"
  10. echo -e "Extracting to ${serverfiles}...\c"
  11. if [ "${firstcommandname}" == "INSTALL" ]; then
  12. unzip -oq "${tmpdir}/bedrock_server.${remotebuildversion}.zip" -x "server.properties" -d "${serverfiles}"
  13. else
  14. unzip -oq "${tmpdir}/bedrock_server.${remotebuildversion}.zip" -x "permissions.json" "server.properties" "allowlist.json" -d "${serverfiles}"
  15. fi
  16. exitcode=$?
  17. if [ "${exitcode}" -ne 0 ]; then
  18. fn_print_fail_eol_nl
  19. fn_script_log_fail "Extracting ${local_filename}"
  20. if [ -f "${lgsmlog}" ]; then
  21. echo -e "${extractcmd}" >> "${lgsmlog}"
  22. fi
  23. echo -e "${extractcmd}"
  24. fn_clear_tmp
  25. core_exit.sh
  26. else
  27. fn_print_ok_eol_nl
  28. if [ -f "${lgsmlog}" ]; then
  29. fn_script_log_pass "Extracting ${local_filename}"
  30. fi
  31. fn_clear_tmp
  32. fi
  33. }
  34. fn_update_localbuild() {
  35. # Gets local build info.
  36. fn_print_dots "Checking local build: ${remotelocation}"
  37. # Uses log file to get local build.
  38. localbuild=$(grep Version "${consolelogdir}"/* 2> /dev/null | tail -1 | sed 's/.*Version: //' | tr -d '\000-\011\013-\037')
  39. if [ -z "${localbuild}" ]; then
  40. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  41. fn_script_log_error "Missing local build info"
  42. fn_script_log_error "Set localbuild to 0"
  43. localbuild="0"
  44. else
  45. fn_print_ok "Checking local build: ${remotelocation}"
  46. fn_script_log_pass "Checking local build"
  47. fi
  48. }
  49. fn_update_remotebuild() {
  50. # Random number for userAgent
  51. randomint=$(tr -dc 0-9 < /dev/urandom 2> /dev/null | head -c 4 | xargs)
  52. # Get remote build info.
  53. if [ "${mcversion}" == "latest" ]; then
  54. remotebuildversion=$(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.${randomint}.212 Safari/537.36" "https://www.minecraft.net/en-us/download/server/bedrock/" | grep -o 'https://www.minecraft.net/bedrockdedicatedserver/bin-linux/[^"]*' | sed 's/.*\///' | grep -Eo "[.0-9]+[0-9]")
  55. else
  56. remotebuildversion="${mcversion}"
  57. fi
  58. remotebuildurl="https://www.minecraft.net/bedrockdedicatedserver/bin-linux/bedrock-server-${remotebuildversion}.zip"
  59. if [ "${firstcommandname}" != "INSTALL" ]; then
  60. fn_print_dots "Checking remote build: ${remotelocation}"
  61. # Checks if remotebuildversion variable has been set.
  62. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  63. fn_print_fail "Checking remote build: ${remotelocation}"
  64. fn_script_log_fail "Checking remote build"
  65. core_exit.sh
  66. else
  67. fn_print_ok "Checking remote build: ${remotelocation}"
  68. fn_script_log_pass "Checking remote build"
  69. fi
  70. else
  71. # Checks if remotebuild variable has been set.
  72. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  73. fn_print_failure "Unable to get remote build"
  74. fn_script_log_fail "Unable to get remote build"
  75. core_exit.sh
  76. fi
  77. fi
  78. }
  79. fn_update_compare() {
  80. fn_print_dots "Checking for update: ${remotelocation}"
  81. # Update has been found or force update.
  82. if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
  83. # Create update lockfile.
  84. date '+%s' > "${lockdir:?}/update.lock"
  85. fn_print_ok_nl "Checking for update: ${remotelocation}"
  86. echo -en "\n"
  87. echo -e "Update available"
  88. echo -e "* Local build: ${red}${localbuild}${default}"
  89. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  90. if [ -n "${branch}" ]; then
  91. echo -e "* Branch: ${branch}"
  92. fi
  93. if [ -f "${rootdir}/.dev-debug" ]; then
  94. echo -e "Remote build info"
  95. echo -e "* apiurl: ${apiurl}"
  96. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  97. echo -e "* remotebuildurl: ${remotebuildurl}"
  98. echo -e "* remotebuildversion: ${remotebuildversion}"
  99. fi
  100. echo -en "\n"
  101. fn_script_log_info "Update available"
  102. fn_script_log_info "Local build: ${localbuild}"
  103. fn_script_log_info "Remote build: ${remotebuildversion}"
  104. if [ -n "${branch}" ]; then
  105. fn_script_log_info "Branch: ${branch}"
  106. fi
  107. fn_script_log_info "${localbuild} > ${remotebuildversion}"
  108. if [ "${commandname}" == "UPDATE" ]; then
  109. date +%s > "${lockdir}/last-updated.lock"
  110. unset updateonstart
  111. check_status.sh
  112. # If server stopped.
  113. if [ "${status}" == "0" ]; then
  114. fn_update_dl
  115. if [ "${localbuild}" == "0" ]; then
  116. exitbypass=1
  117. command_start.sh
  118. fn_firstcommand_reset
  119. exitbypass=1
  120. fn_sleep_time_5
  121. command_stop.sh
  122. fn_firstcommand_reset
  123. fi
  124. # If server started.
  125. else
  126. fn_print_restart_warning
  127. exitbypass=1
  128. command_stop.sh
  129. fn_firstcommand_reset
  130. exitbypass=1
  131. fn_update_dl
  132. exitbypass=1
  133. command_start.sh
  134. fn_firstcommand_reset
  135. fi
  136. unset exitbypass
  137. alert="update"
  138. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  139. alert="check-update"
  140. fi
  141. alert.sh
  142. else
  143. fn_print_ok_nl "Checking for update: ${remotelocation}"
  144. echo -en "\n"
  145. echo -e "No update available"
  146. echo -e "* Local build: ${green}${localbuild}${default}"
  147. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  148. if [ -n "${branch}" ]; then
  149. echo -e "* Branch: ${branch}"
  150. fi
  151. echo -en "\n"
  152. fn_script_log_info "No update available"
  153. fn_script_log_info "Local build: ${localbuild}"
  154. fn_script_log_info "Remote build: ${remotebuildversion}"
  155. if [ -n "${branch}" ]; then
  156. fn_script_log_info "Branch: ${branch}"
  157. fi
  158. if [ -f "${rootdir}/.dev-debug" ]; then
  159. echo -e "Remote build info"
  160. echo -e "* apiurl: ${apiurl}"
  161. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  162. echo -e "* remotebuildurl: ${remotebuildurl}"
  163. echo -e "* remotebuildversion: ${remotebuildversion}"
  164. fi
  165. fi
  166. }
  167. # The location where the builds are checked and downloaded.
  168. remotelocation="minecraft.net"
  169. if [ "$(command -v jq 2> /dev/null)" ]; then
  170. fn_print_fail_nl "jq is not installed"
  171. fn_script_log_fatal "jq is not installed"
  172. core_exit.sh
  173. fi
  174. if [ "${firstcommandname}" == "INSTALL" ]; then
  175. fn_update_remotebuild
  176. fn_update_dl
  177. else
  178. fn_print_dots "Checking for update"
  179. fn_print_dots "Checking for update: ${remotelocation}"
  180. fn_script_log_info "Checking for update: ${remotelocation}"
  181. fn_update_localbuild
  182. fn_update_remotebuild
  183. fn_update_compare
  184. fi