update_mcb.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. local exitcode=$?
  17. if [ "${exitcode}" != 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. fn_script_log_pass "Extracting ${local_filename}"
  29. fn_clear_tmp
  30. fi
  31. }
  32. fn_update_localbuild() {
  33. # Gets local build info.
  34. fn_print_dots "Checking local build: ${remotelocation}"
  35. # Uses log file to get local build.
  36. localbuild=$(grep Version "${consolelogdir}"/* 2> /dev/null | tail -1 | sed 's/.*Version: //' | tr -d '\000-\011\013-\037')
  37. if [ -z "${localbuild}" ]; then
  38. fn_print_error "Checking local build: ${remotelocation}: missing local build info"
  39. fn_script_log_error "Missing local build info"
  40. fn_script_log_error "Set localbuild to 0"
  41. localbuild="0"
  42. else
  43. fn_print_ok "Checking local build: ${remotelocation}"
  44. fn_script_log_pass "Checking local build"
  45. fi
  46. }
  47. fn_update_remotebuild() {
  48. # Random number for userAgent
  49. randomint=$(tr -dc 0-9 < /dev/urandom 2> /dev/null | head -c 4 | xargs)
  50. # Get remote build info.
  51. if [ "${mcversion}" == "latest" ]; then
  52. 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]")
  53. else
  54. remotebuildversion="${mcversion}"
  55. fi
  56. remotebuildurl="https://www.minecraft.net/bedrockdedicatedserver/bin-linux/bedrock-server-${remotebuildversion}.zip"
  57. if [ "${firstcommandname}" != "INSTALL" ]; then
  58. fn_print_dots "Checking remote build: ${remotelocation}"
  59. # Checks if remotebuildversion variable has been set.
  60. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  61. fn_print_fail "Checking remote build: ${remotelocation}"
  62. fn_script_log_fail "Checking remote build"
  63. core_exit.sh
  64. else
  65. fn_print_ok "Checking remote build: ${remotelocation}"
  66. fn_script_log_pass "Checking remote build"
  67. fi
  68. else
  69. # Checks if remotebuild variable has been set.
  70. if [ -z "${remotebuildversion}" ] || [ "${remotebuildversion}" == "null" ]; then
  71. fn_print_failure "Unable to get remote build"
  72. fn_script_log_fail "Unable to get remote build"
  73. core_exit.sh
  74. fi
  75. fi
  76. }
  77. fn_update_compare() {
  78. fn_print_dots "Checking for update: ${remotelocation}"
  79. # Update has been found or force update.
  80. if [ "${localbuild}" != "${remotebuildversion}" ] || [ "${forceupdate}" == "1" ]; then
  81. # Create update lockfile.
  82. date '+%s' > "${lockdir:?}/update.lock"
  83. fn_print_ok_nl "Checking for update: ${remotelocation}"
  84. echo -en "\n"
  85. echo -e "Update available"
  86. echo -e "* Local build: ${red}${localbuild}${default}"
  87. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  88. if [ -n "${branch}" ]; then
  89. echo -e "* Branch: ${branch}"
  90. fi
  91. if [ -f "${rootdir}/.dev-debug" ]; then
  92. echo -e "Remote build info"
  93. echo -e "* apiurl: ${apiurl}"
  94. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  95. echo -e "* remotebuildurl: ${remotebuildurl}"
  96. echo -e "* remotebuildversion: ${remotebuildversion}"
  97. fi
  98. echo -en "\n"
  99. fn_script_log_info "Update available"
  100. fn_script_log_info "Local build: ${localbuild}"
  101. fn_script_log_info "Remote build: ${remotebuildversion}"
  102. if [ -n "${branch}" ]; then
  103. fn_script_log_info "Branch: ${branch}"
  104. fi
  105. fn_script_log_info "${localbuild} > ${remotebuildversion}"
  106. if [ "${commandname}" == "UPDATE" ]; then
  107. date +%s > "${lockdir}/last-updated.lock"
  108. unset updateonstart
  109. check_status.sh
  110. # If server stopped.
  111. if [ "${status}" == "0" ]; then
  112. fn_update_dl
  113. if [ "${localbuild}" == "0" ]; then
  114. exitbypass=1
  115. command_start.sh
  116. fn_firstcommand_reset
  117. exitbypass=1
  118. fn_sleep_time_5
  119. command_stop.sh
  120. fn_firstcommand_reset
  121. fi
  122. # If server started.
  123. else
  124. fn_print_restart_warning
  125. exitbypass=1
  126. command_stop.sh
  127. fn_firstcommand_reset
  128. exitbypass=1
  129. fn_update_dl
  130. exitbypass=1
  131. command_start.sh
  132. fn_firstcommand_reset
  133. fi
  134. unset exitbypass
  135. alert="update"
  136. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  137. alert="check-update"
  138. fi
  139. alert.sh
  140. else
  141. fn_print_ok_nl "Checking for update: ${remotelocation}"
  142. echo -en "\n"
  143. echo -e "No update available"
  144. echo -e "* Local build: ${green}${localbuild}${default}"
  145. echo -e "* Remote build: ${green}${remotebuildversion}${default}"
  146. if [ -n "${branch}" ]; then
  147. echo -e "* Branch: ${branch}"
  148. fi
  149. echo -en "\n"
  150. fn_script_log_info "No update available"
  151. fn_script_log_info "Local build: ${localbuild}"
  152. fn_script_log_info "Remote build: ${remotebuildversion}"
  153. if [ -n "${branch}" ]; then
  154. fn_script_log_info "Branch: ${branch}"
  155. fi
  156. if [ -f "${rootdir}/.dev-debug" ]; then
  157. echo -e "Remote build info"
  158. echo -e "* apiurl: ${apiurl}"
  159. echo -e "* remotebuildfilename: ${remotebuildfilename}"
  160. echo -e "* remotebuildurl: ${remotebuildurl}"
  161. echo -e "* remotebuildversion: ${remotebuildversion}"
  162. fi
  163. fi
  164. }
  165. # The location where the builds are checked and downloaded.
  166. remotelocation="minecraft.net"
  167. if [ "${firstcommandname}" == "INSTALL" ]; then
  168. fn_update_remotebuild
  169. fn_update_dl
  170. else
  171. fn_print_dots "Checking for update"
  172. fn_print_dots "Checking for update: ${remotelocation}"
  173. fn_script_log_info "Checking for update: ${remotelocation}"
  174. fn_update_localbuild
  175. fn_update_remotebuild
  176. fn_update_compare
  177. fi