update_minecraft_bedrock.sh 6.0 KB

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