update_ts3.sh 8.7 KB

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