update_minecraft_bedrock.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. core_exit.sh
  26. fi
  27. }
  28. fn_update_minecraft_localbuild(){
  29. # Gets local build info.
  30. fn_print_dots "Checking local build: ${remotelocation}"
  31. # Uses log file to gather info.
  32. # Log is generated and cleared on startup but filled on shutdown.
  33. localbuild=$(grep Version "${consolelogdir}"/* 2>/dev/null | tail -1 | sed 's/.*Version //')
  34. if [ -z "${localbuild}" ]; then
  35. fn_print_error "Checking local build: ${remotelocation}"
  36. fn_print_error_nl "Checking local build: ${remotelocation}: no log files containing version info"
  37. fn_print_info_nl "Checking local build: ${remotelocation}: forcing server restart"
  38. fn_script_log_error "No log files containing version info"
  39. fn_script_log_info "Forcing server restart"
  40. check_status.sh
  41. # If server stopped.
  42. if [ "${status}" == "0" ]; then
  43. exitbypass=1
  44. command_start.sh
  45. sleep 3
  46. exitbypass=1
  47. command_stop.sh
  48. # If server started.
  49. else
  50. exitbypass=1
  51. command_stop.sh
  52. fi
  53. fi
  54. if [ -z "${localbuild}" ]; then
  55. localbuild=$(grep Version "$(ls -tr "${consolelogdir}"/* 2>/dev/null)" | tail -1 | sed 's/.*Version //')
  56. fi
  57. if [ -z "${localbuild}" ]; then
  58. localbuild="0"
  59. fn_print_error "Checking local build: ${remotelocation}: waiting for local build: missing local build info"
  60. fn_script_log_error "Missing local build info"
  61. fn_script_log_error "Set localbuild to 0"
  62. else
  63. fn_print_ok "Checking local build: ${remotelocation}: checking local build"
  64. fn_script_log_pass "Checking local build"
  65. fi
  66. }
  67. fn_update_minecraft_remotebuild(){
  68. # Gets remote build info.
  69. 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]")
  70. if [ "${installer}" != "1" ]; then
  71. fn_print_dots "Checking remote build: ${remotelocation}"
  72. # Checks if remotebuild variable has been set.
  73. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  74. fn_print_fail "Checking remote build: ${remotelocation}"
  75. fn_script_log_fatal "Checking remote build"
  76. core_exit.sh
  77. else
  78. fn_print_ok "Checking remote build: ${remotelocation}"
  79. fn_script_log_pass "Checking remote build"
  80. fi
  81. else
  82. # Checks if remotebuild variable has been set.
  83. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  84. fn_print_failure "Unable to get remote build"
  85. fn_script_log_fatal "Unable to get remote build"
  86. core_exit.sh
  87. fi
  88. fi
  89. }
  90. fn_update_minecraft_compare(){
  91. # Removes dots so if statement can compare version numbers.
  92. fn_print_dots "Checking for update: ${remotelocation}"
  93. localbuilddigit=$(echo -e "${localbuild}" | tr -cd '[:digit:]')
  94. remotebuilddigit=$(echo -e "${remotebuild}" | tr -cd '[:digit:]')
  95. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ]||[ "${forceupdate}" == "1" ]; then
  96. fn_print_ok_nl "Checking for update: ${remotelocation}"
  97. echo -en "\n"
  98. echo -e "Update available"
  99. echo -e "* Local build: ${red}${localbuild}${default}"
  100. echo -e "* Remote build: ${green}${remotebuild}${default}"
  101. fn_script_log_info "Update available"
  102. fn_script_log_info "Local build: ${localbuild}"
  103. fn_script_log_info "Remote build: ${remotebuild}"
  104. fn_script_log_info "${localbuild} > ${remotebuild}"
  105. unset updateonstart
  106. check_status.sh
  107. # If server stopped.
  108. if [ "${status}" == "0" ]; then
  109. exitbypass=1
  110. fn_update_minecraft_dl
  111. exitbypass=1
  112. command_start.sh
  113. exitbypass=1
  114. command_stop.sh
  115. # If server started.
  116. else
  117. fn_stop_warning
  118. exitbypass=1
  119. command_stop.sh
  120. exitbypass=1
  121. fn_update_minecraft_dl
  122. exitbypass=1
  123. command_start.sh
  124. fi
  125. date +%s > "${lockdir}/lastupdate.lock"
  126. alert="update"
  127. alert.sh
  128. else
  129. fn_print_ok_nl "Checking for update: ${remotelocation}"
  130. echo -en "\n"
  131. echo -e "No update available"
  132. echo -e "* Local build: ${green}${localbuild}${default}"
  133. echo -e "* Remote build: ${green}${remotebuild}${default}"
  134. fn_script_log_info "No update available"
  135. fn_script_log_info "Local build: ${localbuild}"
  136. fn_script_log_info "Remote build: ${remotebuild}"
  137. fi
  138. }
  139. fn_stop_warning(){
  140. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  141. fn_script_log_warn "Updating server: SteamCMD: ${selfname} will be stopped during update"
  142. totalseconds=3
  143. for seconds in {3..1}; do
  144. fn_print_warn "Updating server: SteamCMD: ${selfname} will be stopped during update: ${totalseconds}"
  145. totalseconds=$((totalseconds - 1))
  146. sleep 1
  147. if [ "${seconds}" == "0" ]; then
  148. break
  149. fi
  150. done
  151. fn_print_warn_nl "Updating server: SteamCMD: ${selfname} will be stopped during update"
  152. }
  153. # The location where the builds are checked and downloaded.
  154. remotelocation="minecraft.net"
  155. if [ "${installer}" == "1" ]; then
  156. fn_update_minecraft_remotebuild
  157. fn_update_minecraft_dl
  158. else
  159. fn_print_dots "Checking for update: ${remotelocation}"
  160. fn_script_log_info "Checking for update: ${remotelocation}"
  161. fn_update_minecraft_localbuild
  162. fn_update_minecraft_remotebuild
  163. fn_update_minecraft_compare
  164. fi