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. local commandname="UPDATE"
  7. local commandaction="Update"
  8. local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  9. fn_update_minecraft_dl(){
  10. latestmcbuildurl=$(curl -s "https://www.minecraft.net/en-us/download/server/bedrock/" | grep -o 'https://minecraft.azureedge.net/bin-linux/[^"]*zip')
  11. fn_fetch_file "${latestmcbuildurl}" "${tmpdir}" "bedrock_server.${remotebuild}.zip"
  12. echo -e "Extracting to ${serverfiles}...\c"
  13. if [ "${installer}" == "1" ]; then
  14. unzip -oq "${tmpdir}/bedrock_server.${remotebuild}.zip" -x "server.properties" -d "${serverfiles}"
  15. else
  16. unzip -oq "${tmpdir}/bedrock_server.${remotebuild}.zip" -x "permissions.json" "server.properties" "whitelist.json" -d "${serverfiles}"
  17. fi
  18. local exitcode=$?
  19. if [ "${exitcode}" == "0" ]; then
  20. fn_print_ok_eol_nl
  21. fn_script_log_pass "Extracting to ${serverfiles}"
  22. chmod u+x "${serverfiles}/bedrock_server"
  23. fn_clear_tmp
  24. else
  25. fn_print_fail_eol_nl
  26. fn_script_log_fatal "Extracting to ${serverfiles}"
  27. core_exit.sh
  28. fi
  29. }
  30. fn_update_minecraft_localbuild(){
  31. # Gets local build info.
  32. fn_print_dots "Checking for update: ${remotelocation}: checking local build"
  33. # Uses log file to gather info.
  34. # Log is generated and cleared on startup but filled on shutdown.
  35. localbuild=$(grep Version $(ls -tr "${consolelogdir}"/* 2>/dev/null) | tail -1 | sed 's/.*Version //')
  36. if [ -z "${localbuild}" ]; then
  37. fn_print_error "Checking for update: ${remotelocation}: checking local build"
  38. fn_print_error_nl "Checking for update: ${remotelocation}: checking local build: no log files containing version info"
  39. fn_script_log_error "No log files containing version info"
  40. fn_print_info_nl "Checking for update: ${remotelocation}: checking local build: forcing server restart"
  41. fn_script_log_info "Forcing server restart"
  42. check_status.sh
  43. if [ "${status}" != "0" ]; then
  44. exitbypass=1
  45. command_stop.sh
  46. else
  47. exitbypass=1
  48. command_start.sh
  49. sleep 3
  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 for update: ${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 for update: ${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 for update: ${remotelocation}: checking remote build"
  72. # Checks if remotebuild variable has been set.
  73. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  74. fn_print_fail "Checking for update: ${remotelocation}: checking remote build"
  75. fn_script_log_fatal "Checking remote build"
  76. core_exit.sh
  77. else
  78. fn_print_ok "Checking for update: ${remotelocation}: checking remote build"
  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. fn_sleep_time
  106. echo -en "\n"
  107. echo -en "applying update.\r"
  108. sleep 1
  109. echo -en "applying update..\r"
  110. sleep 1
  111. echo -en "applying update...\r"
  112. sleep 1
  113. echo -en "\n"
  114. unset updateonstart
  115. check_status.sh
  116. # If server stopped.
  117. if [ "${status}" == "0" ]; then
  118. exitbypass=1
  119. fn_update_minecraft_dl
  120. exitbypass=1
  121. command_start.sh
  122. exitbypass=1
  123. command_stop.sh
  124. # If server started.
  125. else
  126. exitbypass=1
  127. command_stop.sh
  128. exitbypass=1
  129. fn_update_minecraft_dl
  130. exitbypass=1
  131. command_start.sh
  132. fi
  133. alert="update"
  134. alert.sh
  135. else
  136. fn_print_ok_nl "Checking for update: ${remotelocation}"
  137. echo -en "\n"
  138. echo -e "No update available"
  139. echo -e "* Local build: ${green}${localbuild}${default}"
  140. echo -e "* Remote build: ${green}${remotebuild}${default}"
  141. fn_script_log_info "No update available"
  142. fn_script_log_info "Local build: ${localbuild}"
  143. fn_script_log_info "Remote build: ${remotebuild}"
  144. fi
  145. }
  146. # The location where the builds are checked and downloaded.
  147. remotelocation="minecraft.net"
  148. if [ "${installer}" == "1" ]; then
  149. fn_update_minecraft_remotebuild
  150. fn_update_minecraft_dl
  151. else
  152. check_status.sh
  153. fn_print_dots "Checking for update: ${remotelocation}"
  154. fn_script_log_info "Checking for update: ${remotelocation}"
  155. fn_update_minecraft_localbuild
  156. fn_update_minecraft_remotebuild
  157. fn_update_minecraft_compare
  158. fi