update_minecraft_bedrock.sh 6.0 KB

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