update_minecraft_bedrock.sh 5.8 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 -Ls "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 [ "${firstcommandname}" == "INSTALL" ]; 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. requirerestart=1
  35. localbuild=$(grep Version "${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. fn_firstcommand_reset
  48. sleep 3
  49. exitbypass=1
  50. command_stop.sh
  51. fn_firstcommand_reset
  52. # If server started.
  53. else
  54. exitbypass=1
  55. command_stop.sh
  56. fn_firstcommand_reset
  57. fi
  58. fi
  59. if [ -z "${localbuild}" ]; then
  60. localbuild=$(grep Version "$(ls -tr "${consolelogdir}"/* 2>/dev/null)" | tail -1 | sed 's/.*Version //')
  61. fi
  62. if [ -z "${localbuild}" ]; then
  63. localbuild="0"
  64. fn_print_error "Checking local build: ${remotelocation}: waiting for local build: missing local build info"
  65. fn_script_log_error "Missing local build info"
  66. fn_script_log_error "Set localbuild to 0"
  67. else
  68. fn_print_ok "Checking local build: ${remotelocation}"
  69. fn_script_log_pass "Checking local build"
  70. fi
  71. }
  72. fn_update_minecraft_remotebuild(){
  73. # Gets remote build info.
  74. 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]")
  75. if [ "${firstcommandname}" != "INSTALL" ]; then
  76. fn_print_dots "Checking remote build: ${remotelocation}"
  77. # Checks if remotebuild variable has been set.
  78. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  79. fn_print_fail "Checking remote build: ${remotelocation}"
  80. fn_script_log_fatal "Checking remote build"
  81. core_exit.sh
  82. else
  83. fn_print_ok "Checking remote build: ${remotelocation}"
  84. fn_script_log_pass "Checking remote build"
  85. fi
  86. else
  87. # Checks if remotebuild variable has been set.
  88. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  89. fn_print_failure "Unable to get remote build"
  90. fn_script_log_fatal "Unable to get remote build"
  91. core_exit.sh
  92. fi
  93. fi
  94. }
  95. fn_update_minecraft_compare(){
  96. # Removes dots so if statement can compare version numbers.
  97. fn_print_dots "Checking for update: ${remotelocation}"
  98. localbuilddigit=$(echo -e "${localbuild}" | tr -cd '[:digit:]')
  99. remotebuilddigit=$(echo -e "${remotebuild}" | tr -cd '[:digit:]')
  100. if [ "${localbuilddigit}" -ne "${remotebuilddigit}" ]||[ "${forceupdate}" == "1" ]; then
  101. fn_print_ok_nl "Checking for update: ${remotelocation}"
  102. echo -en "\n"
  103. echo -e "Update available"
  104. echo -e "* Local build: ${red}${localbuild}${default}"
  105. echo -e "* Remote build: ${green}${remotebuild}${default}"
  106. echo -en "\n"
  107. fn_script_log_info "Update available"
  108. fn_script_log_info "Local build: ${localbuild}"
  109. fn_script_log_info "Remote build: ${remotebuild}"
  110. fn_script_log_info "${localbuild} > ${remotebuild}"
  111. unset updateonstart
  112. check_status.sh
  113. # If server stopped.
  114. if [ "${status}" == "0" ]; then
  115. exitbypass=1
  116. fn_update_minecraft_dl
  117. if [ "${requirerestart}" == "1" ]; then
  118. exitbypass=1
  119. command_start.sh
  120. fn_firstcommand_reset
  121. exitbypass=1
  122. command_stop.sh
  123. fn_firstcommand_reset
  124. fi
  125. # If server started.
  126. else
  127. fn_print_restart_warning
  128. exitbypass=1
  129. command_stop.sh
  130. fn_firstcommand_reset
  131. exitbypass=1
  132. fn_update_minecraft_dl
  133. exitbypass=1
  134. command_start.sh
  135. fn_firstcommand_reset
  136. fi
  137. unset exitbypass
  138. date +%s > "${lockdir}/lastupdate.lock"
  139. alert="update"
  140. alert.sh
  141. else
  142. fn_print_ok_nl "Checking for update: ${remotelocation}"
  143. echo -en "\n"
  144. echo -e "No update available"
  145. echo -e "* Local build: ${green}${localbuild}${default}"
  146. echo -e "* Remote build: ${green}${remotebuild}${default}"
  147. echo -en "\n"
  148. fn_script_log_info "No update available"
  149. fn_script_log_info "Local build: ${localbuild}"
  150. fn_script_log_info "Remote build: ${remotebuild}"
  151. fi
  152. }
  153. # The location where the builds are checked and downloaded.
  154. remotelocation="minecraft.net"
  155. if [ "${firstcommandname}" == "INSTALL" ]; then
  156. fn_update_minecraft_remotebuild
  157. fn_update_minecraft_dl
  158. else
  159. fn_print_dots "Checking for update"
  160. fn_print_dots "Checking for update: ${remotelocation}"
  161. fn_script_log_info "Checking for update: ${remotelocation}"
  162. fn_update_minecraft_localbuild
  163. fn_update_minecraft_remotebuild
  164. fn_update_minecraft_compare
  165. fi