update_minecraft_bedrock.sh 5.8 KB

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