update_minecraft_bedrock.sh 5.7 KB

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