diff options
Diffstat (limited to 'travertine.sh')
-rwxr-xr-x | travertine.sh | 141 |
1 files changed, 109 insertions, 32 deletions
diff --git a/travertine.sh b/travertine.sh index 6dda2a3105a9..44cbcf337d76 100755 --- a/travertine.sh +++ b/travertine.sh @@ -9,30 +9,88 @@ declare -r game="travertine" # Avoid altering any of those later in the code since they may be readonly (IDLE_SERVER is an exception!) # You may use this script for any game server of your choice, just alter the config file -[[ -n "${SERVER_ROOT}" ]] && declare -r SERVER_ROOT=${SERVER_ROOT} || SERVER_ROOT="/srv/${game}" -[[ -n "${BACKUP_DEST}" ]] && declare -r BACKUP_DEST=${BACKUP_DEST} || BACKUP_DEST="/srv/${game}/backup" -[[ -n "${BACKUP_PATHS}" ]] && declare -r BACKUP_PATHS=${BACKUP_PATHS} || BACKUP_PATHS="world" -[[ -n "${BACKUP_FLAGS}" ]] && declare -r BACKUP_FLAGS=${BACKUP_FLAGS} || BACKUP_FLAGS="-z" -[[ -n "${KEEP_BACKUPS}" ]] && declare -r KEEP_BACKUPS=${KEEP_BACKUPS} || KEEP_BACKUPS="10" -[[ -n "${GAME_USER}" ]] && declare -r GAME_USER=${GAME_USER} || GAME_USER="travertine" -[[ -n "${MAIN_EXECUTABLE}" ]] && declare -r MAIN_EXECUTABLE=${MAIN_EXECUTABLE} || MAIN_EXECUTABLE="travertine_server.jar" -[[ -n "${SESSION_NAME}" ]] && declare -r SESSION_NAME=${SESSION_NAME} || SESSION_NAME="${game}" +# Server settings +[[ -n "${MAIN_ROOT}" ]] && declare -r MAIN_ROOT=${MAIN_ROOT} || MAIN_ROOT="/srv/${game}" +[[ -n "${GAME_USER}" ]] && declare -r GAME_USER=${GAME_USER} || GAME_USER="${game}" +[[ -n "${PROXY_NAME}" ]] && declare -r PROXY_NAME=${PROXY_NAME} || PROXY_NAME="proxy" +source /etc/conf.d/"${game}" 2>/dev/null || >&2 echo "Could not source /etc/conf.d/${game}" + +# Handling subservers +# Check how many arguments were given +case $# in + 0) + # If zero, use the proxy server + declare -r subserver="${PROXY_NAME}" + ;; + 1) + # If one, check wheter it's a command or a subserver + if [ "$1" == "start" ] || + [ "$1" == "stop" ] || + [ "$1" == "status" ] || + [ "$1" == "restart" ] || + [ "$1" == "console" ] || + [ "$1" == "command" ] || + [ "$1" == "backup" ] || + [ "$1" == "restore" ] || + [ "$1" == "-h" ] || + [ "$1" == "--help" ] || + [ "$1" == "idle_server_daemon" ]; then + # If a command, use the proxy server + declare -r subserver="${PROXY_NAME}" + else + # If not, use the argument as subserver + declare -r subserver="$1" + # And shift the arguments forward + shift + fi + ;; + *) + # If any other (two or more), we can be sure both the subserver and command were specified + declare -r subserver="$1" + shift + ;; +esac + +echo "Using subserver: ${subserver}" +# The full command name, including the subserver +declare -r fullname="${myname} ${subserver}" + +# Default stop and say commands +if [ ${subserver} == ${PROXY_NAME} ]; then + declare -r default_stop_command="end" + declare -r default_say_command="alert" +else + declare -r default_stop_command="stop" + declare -r default_say_command="say" +fi + +# Subserver settings + +[[ -n "${SERVER_ROOT}" ]] && declare -r SERVER_ROOT=${SERVER_ROOT} || SERVER_ROOT="${MAIN_ROOT}/servers/${subserver}" +[[ -n "${BACKUP_DEST}" ]] && declare -r BACKUP_DEST=${BACKUP_DEST} || BACKUP_DEST="${MAIN_ROOT}/servers/${subserver}/backup" +[[ -n "${BACKUP_PATHS}" ]] && declare -r BACKUP_PATHS=${BACKUP_PATHS} || BACKUP_PATHS="world" +[[ -n "${BACKUP_FLAGS}" ]] && declare -r BACKUP_FLAGS=${BACKUP_FLAGS} || BACKUP_FLAGS="-z" +[[ -n "${KEEP_BACKUPS}" ]] && declare -r KEEP_BACKUPS=${KEEP_BACKUPS} || KEEP_BACKUPS="10" +[[ -n "${MAIN_EXECUTABLE}" ]] && declare -r MAIN_EXECUTABLE=${MAIN_EXECUTABLE} || MAIN_EXECUTABLE="server.jar" +[[ -n "${SESSION_NAME}" ]] && declare -r SESSION_NAME=${SESSION_NAME} || SESSION_NAME="${game}_${subserver}" +[[ -n "${SAY_COMMAND}" ]] && declare -r SAY_COMMAND=${SAY_COMMAND} || SAY_COMMAND="${default_say_command}" +[[ -n "${STOP_COMMAND}" ]] && declare -r STOP_COMMAND=${STOP_COMMAND} || STOP_COMMAND="${default_stop_command}" # Command and parameter declaration with which to start the server [[ -n "${SERVER_START_CMD}" ]] && declare -r SERVER_START_CMD=${SERVER_START_CMD} || SERVER_START_CMD="java -Xms512M -Xmx1024M -XX:ParallelGCThreads=1 -jar './${MAIN_EXECUTABLE}' nogui" # System parameters for the control script -[[ -n "${IDLE_SERVER}" ]] && tmp_IDLE_SERVER=${IDLE_SERVER} || IDLE_SERVER="false" +[[ -n "${IDLE_SERVER}" ]] && tmp_IDLE_SERVER=${IDLE_SERVER} || IDLE_SERVER="false" [[ -n "${IDLE_SESSION_NAME}" ]] && declare -r IDLE_SESSION_NAME=${IDLE_SESSION_NAME} || IDLE_SESSION_NAME="idle_server_${SESSION_NAME}" -[[ -n "${GAME_PORT}" ]] && declare -r GAME_PORT=${GAME_PORT} || GAME_PORT="25565" +[[ -n "${GAME_PORT}" ]] && declare -r GAME_PORT=${GAME_PORT} || GAME_PORT="25565" [[ -n "${CHECK_PLAYER_TIME}" ]] && declare -r CHECK_PLAYER_TIME=${CHECK_PLAYER_TIME} || CHECK_PLAYER_TIME="30" -[[ -n "${IDLE_IF_TIME}" ]] && declare -r IDLE_IF_TIME=${IDLE_IF_TIME} || IDLE_IF_TIME="1200" +[[ -n "${IDLE_IF_TIME}" ]] && declare -r IDLE_IF_TIME=${IDLE_IF_TIME} || IDLE_IF_TIME="1200" # Additional configuration options which only few may need to alter [[ -n "${GAME_COMMAND_DUMP}" ]] && declare -r GAME_COMMAND_DUMP=${GAME_COMMAND_DUMP} || GAME_COMMAND_DUMP="/tmp/${myname}_${SESSION_NAME}_command_dump.txt" # Variables passed over the command line will always override the one from a config file -source /etc/conf.d/"${game}" 2>/dev/null || >&2 echo "Could not source /etc/conf.d/${game}" +source "${MAIN_ROOT}"/servers/"${subserver}".conf 2>/dev/null || >&2 echo "Could not source ${MAIN_ROOT}/servers/${subserver}.conf" # Preserve the content of IDLE_SERVER without making it readonly [[ -n ${tmp_IDLE_SERVER} ]] && IDLE_SERVER=${tmp_IDLE_SERVER} @@ -51,9 +109,8 @@ else fi # Choose which flavor of netcat is to be used -if command -v netcat &> /dev/null; then - NETCAT_CMD="netcat" -elif command -v ncat &> /dev/null; then +# (Currently only nmap's version works) +if command -v ncat &> /dev/null; then NETCAT_CMD="ncat" else NETCAT_CMD="" @@ -129,7 +186,7 @@ idle_server_daemon() { no_player=$(( no_player + CHECK_PLAYER_TIME )) # Stop the game server if no player was active for at least ${IDLE_IF_TIME} if [[ "${no_player}" -ge "${IDLE_IF_TIME}" ]]; then - IDLE_SERVER="false" ${myname} stop + IDLE_SERVER="false" ${fullname} stop # Wait for game server to go down for i in {1..100}; do screen -S "${SESSION_NAME}" -Q select . > /dev/null || break @@ -139,9 +196,16 @@ idle_server_daemon() { # Reset timer and give the player 300 seconds to connect after pinging no_player=$(( IDLE_IF_TIME - 300 )) # Game server is down, listen on port ${GAME_PORT} for incoming connections - echo -n "Netcat: " - ${NETCAT_CMD} -v -l -p ${GAME_PORT} && echo "Netcat caught an connection. The server is coming up again..." - IDLE_SERVER="false" ${myname} start + ${myname} ${PROXY_NAME} command alert "Suspending server ${subserver}" + player=$(${NETCAT_CMD} -i 1 -l -p ${GAME_PORT} | cut -d '' -f6 | tr -dc '[:alnum:]') + IDLE_SERVER="false" ${fullname} start + if [[ -n $player ]]; then + ${myname} ${PROXY_NAME} command alert "Starting server ${subserver}, as requested by $player" + sleep 20 + ${myname} ${PROXY_NAME} command send $player ${subserver} + else + ${myname} ${PROXY_NAME} command alert "Starting server ${subserver}" + fi fi else # Reset timer since there is an active player on the server @@ -151,9 +215,16 @@ idle_server_daemon() { # Reset timer and give the player 300 seconds to connect after pinging no_player=$(( IDLE_IF_TIME - 300 )) # Game server is down, listen on port ${GAME_PORT} for incoming connections - echo -n "Netcat: " - ${NETCAT_CMD} -v -l -p ${GAME_PORT} && echo "Netcat caught an connection. The server is coming up again..." - IDLE_SERVER="false" ${myname} start + ${myname} ${PROXY_NAME} command alert "Suspending server ${subserver}" + player=$(${NETCAT_CMD} -i 1 -l -p ${GAME_PORT} | cut -d '' -f6 | tr -dc '[:alnum:]') + IDLE_SERVER="false" ${fullname} start + if [[ -n $player ]]; then + ${myname} ${PROXY_NAME} command alert "Starting server ${subserver}, as requested by $player" + sleep 20 + ${myname} ${PROXY_NAME} command send $player ${subserver} + else + ${myname} ${PROXY_NAME} command alert "Starting server ${subserver}" + fi fi done } @@ -183,7 +254,7 @@ server_start() { # Restart as soon as the idle_server_daemon has shut down completely for i in {1..100}; do if ! ${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -Q select . > /dev/null; then - ${SUDO_CMD} screen -dmS "${IDLE_SESSION_NAME}" /bin/bash -c "${myname} idle_server_daemon" + ${SUDO_CMD} screen -dmS "${IDLE_SESSION_NAME}" /bin/bash -c "${fullname} idle_server_daemon" break fi [[ $i -eq 100 ]] && echo -e "An \e[39;1merror\e[0m occurred while trying to reset the idle_server!" @@ -191,7 +262,7 @@ server_start() { done else echo -en "Starting idle server daemon..." - ${SUDO_CMD} screen -dmS "${IDLE_SESSION_NAME}" /bin/bash -c "${myname} idle_server_daemon" + ${SUDO_CMD} screen -dmS "${IDLE_SESSION_NAME}" /bin/bash -c "${fullname} idle_server_daemon" echo -e "\e[39;1m done\e[0m" fi fi @@ -224,19 +295,21 @@ server_stop() { if is_player_online; then # No player was seen on the server through list echo -en "Server is going down..." - game_command end else # Player(s) were seen on the server through list (or an error occurred) # Warning the users through the server console - game_command alert "Server is going down in 10 seconds! HURRY UP WITH WHATEVER YOU ARE DOING!" - echo -en "Server is going down in..." + game_command ${SAY_COMMAND} "This server is going down in 10 seconds." + game_command save-all + echo -en "Server going down in..." for i in {1..10}; do - game_command alert "down in... $(( 10 - i ))" - echo -n " $(( 10 - i ))" + if [[ $i -gt 7 ]]; then + game_command ${SAY_COMMAND} "$(( 11 - i ))" + fi + echo -n " $(( 11 - i ))" sleep 1 done - game_command end fi + game_command ${STOP_COMMAND} # Finish as soon as the server has shut down completely for i in {1..100}; do @@ -284,6 +357,7 @@ server_status() { # Restart the complete server by shutting it down and starting it again server_restart() { + ${myname} ${PROXY_NAME} command alert "Restarting server ${subserver}" if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then server_stop server_start @@ -304,8 +378,11 @@ backup_files() { fname="$(date +%Y_%m_%d_%H.%M.%S).tar.gz" ${SUDO_CMD} mkdir -p "${BACKUP_DEST}" if ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null; then + game_command save-off + game_command save-all sync && wait ${SUDO_CMD} tar -C "${SERVER_ROOT}" -cf "${BACKUP_DEST}/${fname}" ${BACKUP_PATHS} --totals ${BACKUP_FLAGS} 2>&1 | grep -v "tar: Removing leading " + game_command save-on else ${SUDO_CMD} tar -C "${SERVER_ROOT}" -cf "${BACKUP_DEST}/${fname}" ${BACKUP_PATHS} --totals ${BACKUP_FLAGS} 2>&1 | grep -v "tar: Removing leading " fi @@ -342,7 +419,7 @@ backup_restore() { echo "Please enter the corresponding number for the backup to be restored: " i=1 for f in "${BACKUP_DEST}"/[0-9_.]*; do - echo -e " \e[39;1m$i)\e[0m\t$f" + echo -e " \e[39;1m$i)\e[0m\t$f" i=$(( i + 1 )) done echo -en "Restore backup number: " @@ -422,7 +499,7 @@ help() { This script was design to easily control any ${game} server. Quite every parameter for a given ${game} server derivative can be altered by editing the variables in the configuration file. - Usage: ${myname} {start|stop|status|backup|restore|command <command>|console} + Usage: ${fullname} {start|stop|status|backup|restore|command <command>|console} start Start the ${game} server stop Stop the ${game} server restart Restart the ${game} server |