update_ts3.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/bin/bash
  2. # LinuxGSM command_ts3.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://linuxgsm.com
  5. # Description: Handles updating of teamspeak 3 servers.
  6. commandname="UPDATE"
  7. commandaction="Update"
  8. function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  9. fn_update_ts3_dl(){
  10. fn_fetch_file "http://dl.4players.de/ts/releases/${ts3_version_number}/teamspeak3-server_linux_${ts3arch}-${ts3_version_number}.tar.bz2" "${tmpdir}" "teamspeak3-server_linux_${ts3arch}-${ts3_version_number}.tar.bz2"
  11. fn_dl_extract "${tmpdir}" "teamspeak3-server_linux_${ts3arch}-${ts3_version_number}.tar.bz2" "${tmpdir}"
  12. echo -e "copying to ${serverfiles}...\c"
  13. fn_script_log "Copying to ${serverfiles}"
  14. cp -R "${tmpdir}/teamspeak3-server_linux_${ts3arch}/"* "${serverfiles}"
  15. local exitcode=$?
  16. if [ "${exitcode}" == "0" ]; then
  17. fn_print_ok_eol_nl
  18. else
  19. fn_print_fail_eol_nl
  20. fi
  21. }
  22. fn_update_ts3_currentbuild(){
  23. # Gets current build info
  24. # Checks if current build info is available. If it fails, then a server restart will be forced to generate logs.
  25. if [ -z "$(find ./* -name 'ts3server*_0.log')" ]; then
  26. fn_print_error "Checking for update: teamspeak.com"
  27. sleep 0.5
  28. fn_print_error_nl "Checking for update: teamspeak.com: No logs with server version found"
  29. fn_script_log_error "Checking for update: teamspeak.com: No logs with server version found"
  30. sleep 0.5
  31. fn_print_info_nl "Checking for update: teamspeak.com: Forcing server restart"
  32. fn_script_log_info "Checking for update: teamspeak.com: Forcing server restart"
  33. sleep 0.5
  34. exitbypass=1
  35. command_stop.sh
  36. exitbypass=1
  37. command_start.sh
  38. sleep 0.5
  39. # Check again and exit on failure.
  40. if [ -z "$(find ./* -name 'ts3server*_0.log')" ]; then
  41. fn_print_fail_nl "Checking for update: teamspeak.com: Still No logs with server version found"
  42. fn_script_log_fatal "Checking for update: teamspeak.com: Still No logs with server version found"
  43. core_exit.sh
  44. fi
  45. fi
  46. # Get current build from logs
  47. currentbuild=$(cat $(find ./* -name 'ts3server*_0.log' 2> /dev/null | sort | grep -Ev '${rootdir}/.ts3version' | tail -1) | grep -Eo 'TeamSpeak 3 Server ((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}' | grep -Eo '((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}' | sort -V | tail -1)
  48. if [ -z "${currentbuild}" ]; then
  49. fn_print_error_nl "Checking for update: teamspeak.com: Current build version not found"
  50. fn_script_log_error "Checking for update: teamspeak.com: Current build version not found"
  51. sleep 0.5
  52. fn_print_info_nl "Checking for update: teamspeak.com: Forcing server restart"
  53. fn_script_log_info "Checking for update: teamspeak.com: Forcing server restart"
  54. exitbypass=1
  55. command_stop.sh
  56. exitbypass=1
  57. command_start.sh
  58. currentbuild=$(cat $(find ./* -name 'ts3server*_0.log' 2> /dev/null | sort | grep -Ev '${rootdir}/.ts3version' | tail -1) | grep -Eo 'TeamSpeak 3 Server ((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}' | grep -Eo '((\.)?[0-9]{1,3}){1,3}\.[0-9]{1,3}')
  59. if [ -z "${currentbuild}" ]; then
  60. fn_print_fail_nl "Checking for update: teamspeak.com: Current build version still not found"
  61. fn_script_log_fatal "Checking for update: teamspeak.com: Current build version still not found"
  62. core_exit.sh
  63. fi
  64. fi
  65. }
  66. fn_update_ts3_arch(){
  67. # Gets the teamspeak server architecture.
  68. info_distro.sh
  69. if [ "${arch}" == "x86_64" ]; then
  70. ts3arch="amd64"
  71. elif [ "${arch}" == "i386" ]||[ "${arch}" == "i686" ]; then
  72. ts3arch="x86"
  73. else
  74. echo ""
  75. fn_print_failure "Unknown or unsupported architecture: ${arch}"
  76. fn_script_log_fatal "Unknown or unsupported architecture: ${arch}"
  77. core_exit.sh
  78. fi
  79. }
  80. fn_update_ts3_availablebuild(){
  81. # Gets latest build info.
  82. if [ "${arch}" == "x86_64" ]; then
  83. availablebuild="$(${curlpath} -s 'https://www.teamspeak.com/versions/server.json' | jq -r '.linux.x86_64.version')"
  84. elif [ "${arch}" == "x86" ]; then
  85. availablebuild="$(${curlpath} -s 'https://www.teamspeak.com/versions/server.json' | jq -r '.linux.x86.version')"
  86. fi
  87. ts3_version_number="${availablebuild}"
  88. # Checks if availablebuild variable has been set
  89. if [ -z "${availablebuild}" ]||[ "${availablebuild}" == "null" ]; then
  90. fn_print_fail "Checking for update: teamspeak.com"
  91. sleep 0.5
  92. fn_print_fail "Checking for update: teamspeak.com: Not returning version info"
  93. fn_script_log_fatal "Failure! Checking for update: teamspeak.com: Not returning version info"
  94. core_exit.sh
  95. elif [ "${installer}" == "1" ]; then
  96. :
  97. else
  98. fn_print_ok_nl "Checking for update: teamspeak.com"
  99. fn_script_log_pass "Checking for update: teamspeak.com"
  100. sleep 0.5
  101. fi
  102. }
  103. fn_update_ts3_availablebuild_legacy(){
  104. # Gets latest build info.
  105. # Grabs all version numbers but not in correct order.
  106. wget "http://dl.4players.de/ts/releases/?C=M;O=D" -q -O - | grep -i dir | grep -Eo '<a href=\".*\/\">.*\/<\/a>' | grep -Eo '[0-9\.?]+' | uniq > "${tmpdir}/.ts3_version_numbers_unsorted.tmp"
  107. # Sort version numbers
  108. cat "${tmpdir}/.ts3_version_numbers_unsorted.tmp" | sort -r --version-sort -o "${tmpdir}/.ts3_version_numbers_sorted.tmp"
  109. # Finds directory with most recent server version.
  110. while read -r ts3_version_number; do
  111. wget --spider -q "http://dl.4players.de/ts/releases/${ts3_version_number}/teamspeak3-server_linux_${ts3arch}-${ts3_version_number}.tar.bz2"
  112. if [ $? -eq 0 ]; then
  113. availablebuild="${ts3_version_number}"
  114. # Break while-loop, if the latest release could be found.
  115. break
  116. fi
  117. done < "${tmpdir}/.ts3_version_numbers_sorted.tmp"
  118. # Tidy up
  119. rm -f "${tmpdir}/.ts3_version_numbers_unsorted.tmp"
  120. rm -f "${tmpdir}/.ts3_version_numbers_sorted.tmp"
  121. # Checks availablebuild info is available
  122. if [ -z "${availablebuild}" ]; then
  123. fn_print_fail "Checking for update: teamspeak.com"
  124. sleep 0.5
  125. fn_print_fail "Checking for update: teamspeak.com: Not returning version info"
  126. fn_script_log_fatal "Failure! Checking for update: teamspeak.com: Not returning version info"
  127. core_exit.sh
  128. elif [ "${installer}" == "1" ]; then
  129. :
  130. else
  131. fn_print_ok "Checking for update: teamspeak.com"
  132. fn_script_log_pass "Checking for update: teamspeak.com"
  133. sleep 0.5
  134. fi
  135. }
  136. fn_update_ts3_compare(){
  137. # Removes dots so if can compare version numbers
  138. currentbuilddigit=$(echo "${currentbuild}" | tr -cd '[:digit:]')
  139. availablebuilddigit=$(echo "${availablebuild}" | tr -cd '[:digit:]')
  140. if [ "${currentbuilddigit}" -ne "${availablebuilddigit}" ]; then
  141. echo -e "\n"
  142. echo -e "Update available:"
  143. sleep 0.5
  144. echo -e " Current build: ${red}${currentbuild} ${ts3arch}${default}"
  145. echo -e " Available build: ${green}${availablebuild} ${ts3arch}${default}"
  146. echo -e ""
  147. sleep 0.5
  148. echo ""
  149. echo -en "Applying update.\r"
  150. sleep 1
  151. echo -en "Applying update..\r"
  152. sleep 1
  153. echo -en "Applying update...\r"
  154. sleep 1
  155. echo -en "\n"
  156. fn_script_log "Update available"
  157. fn_script_log "Current build: ${currentbuild}"
  158. fn_script_log "Available build: ${availablebuild}"
  159. fn_script_log "${currentbuild} > ${availablebuild}"
  160. unset updateonstart
  161. check_status.sh
  162. if [ "${status}" == "0" ]; then
  163. fn_update_ts3_dl
  164. exitbypass=1
  165. command_start.sh
  166. exitbypass=1
  167. command_stop.sh
  168. else
  169. exitbypass=1
  170. command_stop.sh
  171. fn_update_ts3_dl
  172. exitbypass=1
  173. command_start.sh
  174. fi
  175. alert="update"
  176. alert.sh
  177. else
  178. echo -e "\n"
  179. echo -e "No update available:"
  180. echo -e " Current version: ${green}${currentbuild}${default}"
  181. echo -e " Available version: ${green}${availablebuild}${default}"
  182. echo -e ""
  183. fn_print_ok_nl "No update available"
  184. fn_script_log_info "Current build: ${currentbuild}"
  185. fn_script_log_info "Available build: ${availablebuild}"
  186. fi
  187. }
  188. fn_update_ts3_arch
  189. if [ "${installer}" == "1" ]; then
  190. # if jq available uses json update checker
  191. if [ "$(command -v jq >/dev/null 2>&1)" ]; then
  192. fn_update_ts3_availablebuild
  193. else
  194. fn_update_ts3_availablebuild_legacy
  195. fi
  196. fn_update_ts3_dl
  197. else
  198. # Checks for server update from teamspeak.com using a mirror dl.4players.de.
  199. fn_print_dots "Checking for update: teamspeak.com"
  200. fn_script_log_info "Checking for update: teamspeak.com"
  201. sleep 0.5
  202. fn_update_ts3_currentbuild
  203. if [ "$(command -v jq >/dev/null 2>&1)" ]; then
  204. fn_update_ts3_availablebuild
  205. else
  206. fn_update_ts3_availablebuild_legacy
  207. fi
  208. fn_update_ts3_compare
  209. fi