2015-09-20 14:23:34 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
2016-02-29 12:35:38 -08:00
|
|
|
# The actual program name
|
|
|
|
declare -r myname="minecraftd"
|
|
|
|
declare -r game="minecraft"
|
2015-09-20 14:23:34 -07:00
|
|
|
|
2016-01-26 11:30:17 -08:00
|
|
|
# General rule for the variable-naming-schema:
|
|
|
|
# Variables in capital letters may be passed through the command line others not.
|
2016-03-05 03:18:45 -08:00
|
|
|
# Avoid altering any of those later in the code since they may be readonly (IDLE_SERVER is an exception!)
|
2016-02-29 12:35:38 -08:00
|
|
|
|
|
|
|
# You may use this script for any game server of your choice, just alter the config file
|
|
|
|
[[ ! -z "${SERVER_ROOT}" ]] && declare -r SERVER_ROOT=${SERVER_ROOT} || SERVER_ROOT="/srv/${game}"
|
|
|
|
[[ ! -z "${BACKUP_DEST}" ]] && declare -r BACKUP_DEST=${BACKUP_DEST} || BACKUP_DEST="/srv/${game}/backup"
|
|
|
|
[[ ! -z "${BACKUP_PATHS}" ]] && declare -r BACKUP_PATHS=${BACKUP_PATHS} || BACKUP_PATHS="world"
|
|
|
|
[[ ! -z "${KEEP_BACKUPS}" ]] && declare -r KEEP_BACKUPS=${KEEP_BACKUPS} || KEEP_BACKUPS="10"
|
|
|
|
[[ ! -z "${GAME_USER}" ]] && declare -r GAME_USER=${GAME_USER} || GAME_USER="minecraft"
|
|
|
|
[[ ! -z "${MAIN_EXECUTABLE}" ]] && declare -r MAIN_EXECUTABLE=${MAIN_EXECUTABLE} || MAIN_EXECUTABLE="minecraft_server.jar"
|
|
|
|
[[ ! -z "${SESSION_NAME}" ]] && declare -r SESSION_NAME=${SESSION_NAME} || SESSION_NAME="${game}"
|
|
|
|
|
2016-06-24 08:14:20 -07:00
|
|
|
# Command and parameter declaration with which to start the server
|
|
|
|
[[ ! -z "${SERVER_START_CMD}" ]] && declare -r SERVER_START_CMD=${SERVER_START_CMD} || SERVER_START_CMD="java -Xms512M -Xmx1024M -XX:ParallelGCThreads=1 -jar './${MAIN_EXECUTABLE}' nogui"
|
2016-02-29 12:35:38 -08:00
|
|
|
|
|
|
|
# System parameters for the control script
|
2016-03-05 03:18:45 -08:00
|
|
|
[[ ! -z "${IDLE_SERVER}" ]] && tmp_IDLE_SERVER=${IDLE_SERVER} || IDLE_SERVER="false"
|
2016-08-04 03:51:07 -07:00
|
|
|
[[ ! -z "${IDLE_SESSION_NAME}" ]] && declare -r IDLE_SESSION_NAME=${IDLE_SESSION_NAME} || IDLE_SESSION_NAME="${SESSION_NAME}_idle_server"
|
2016-02-29 12:35:38 -08:00
|
|
|
[[ ! -z "${GAME_PORT}" ]] && declare -r GAME_PORT=${GAME_PORT} || GAME_PORT="25565"
|
|
|
|
[[ ! -z "${CHECK_PLAYER_TIME}" ]] && declare -r CHECK_PLAYER_TIME=${CHECK_PLAYER_TIME} || CHECK_PLAYER_TIME="30"
|
|
|
|
[[ ! -z "${IDLE_IF_TIME}" ]] && declare -r IDLE_IF_TIME=${IDLE_IF_TIME} || IDLE_IF_TIME="1200"
|
|
|
|
|
2016-08-04 03:51:07 -07:00
|
|
|
# Additional configuration options which only few may need to alter
|
|
|
|
[[ ! -z "${GAME_COMMAND_DUMP}" ]] && declare -r GAME_COMMAND_DUMP=${GAME_COMMAND_DUMP} || GAME_COMMAND_DUMP="/tmp/${myname}_${SESSION_NAME}_command_dump.txt"
|
|
|
|
|
2016-02-29 12:35:38 -08:00
|
|
|
# Variables passed over the command line will always override the one from a config file
|
2016-08-04 03:51:07 -07:00
|
|
|
source /etc/conf.d/${game} 2>/dev/null || >&2 echo "Could not source /etc/conf.d/${game}"
|
2016-01-25 15:18:20 -08:00
|
|
|
|
2016-03-05 03:18:45 -08:00
|
|
|
# Preserve the content of IDLE_SERVER without making it readonly
|
|
|
|
[[ ! -z ${tmp_IDLE_SERVER} ]] && IDLE_SERVER=${tmp_IDLE_SERVER}
|
|
|
|
|
2016-03-29 08:44:57 -07:00
|
|
|
|
2016-01-26 11:30:17 -08:00
|
|
|
# Check whether sudo is needed at all
|
2016-02-29 12:35:38 -08:00
|
|
|
if [[ $(whoami) == ${GAME_USER} ]]; then
|
2016-01-26 11:30:17 -08:00
|
|
|
SUDO_CMD=""
|
|
|
|
else
|
2016-02-29 12:35:38 -08:00
|
|
|
SUDO_CMD="sudo -u ${GAME_USER}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Choose which flavor of netcat is to be used
|
|
|
|
if which netcat &> /dev/null; then
|
|
|
|
NETCAT_CMD="netcat"
|
|
|
|
elif which ncat &> /dev/null; then
|
|
|
|
NETCAT_CMD="ncat"
|
|
|
|
else
|
|
|
|
NETCAT_CMD=""
|
2016-01-26 11:30:17 -08:00
|
|
|
fi
|
|
|
|
|
2015-09-20 14:23:34 -07:00
|
|
|
# Check for sudo rigths
|
2016-02-29 12:35:38 -08:00
|
|
|
if [[ $(${SUDO_CMD} whoami) != ${GAME_USER} ]]; then
|
|
|
|
>&2 echo -e "You have \e[39;1mno permission\e[0m to run commands as $GAME_USER user."
|
|
|
|
exit 21
|
2015-09-20 14:23:34 -07:00
|
|
|
fi
|
|
|
|
|
2016-06-24 08:14:20 -07:00
|
|
|
# Pipe any given argument to the game server console,
|
|
|
|
# sleep for $sleep_time and return its output if $return_stdout is set
|
2016-02-29 12:35:38 -08:00
|
|
|
game_command() {
|
2016-06-24 08:14:20 -07:00
|
|
|
if [[ -z "${return_stdout}" ]]; then
|
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -X stuff "`printf \"$*\r\"`"
|
|
|
|
else
|
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -X log on
|
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -X stuff "`printf \"$*\r\"`"
|
|
|
|
sleep ${sleep_time:-0.3}
|
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -X log off
|
2016-08-04 03:51:07 -07:00
|
|
|
${SUDO_CMD} cat "${GAME_COMMAND_DUMP}"
|
|
|
|
${SUDO_CMD} rm "${GAME_COMMAND_DUMP}"
|
2016-06-24 08:14:20 -07:00
|
|
|
fi
|
2015-09-20 14:23:34 -07:00
|
|
|
}
|
|
|
|
|
2016-05-28 04:11:17 -07:00
|
|
|
# Check whether there are player on the server through list
|
|
|
|
is_player_online() {
|
2016-06-24 08:14:20 -07:00
|
|
|
response="$(sleep_time=0.6 return_stdout=true game_command list)"
|
2016-05-28 04:11:17 -07:00
|
|
|
# The list command prints a line containing the usernames after the last occurrence of ": "
|
2016-06-24 08:14:20 -07:00
|
|
|
# and since playernames may not contain this string the clean player-list can easily be retrieved.
|
2016-05-28 04:11:17 -07:00
|
|
|
# Otherwiese check the first digit after the last occurrence of "There are". If it is 0 then there
|
|
|
|
# are no players on the server. Should this test fail as well. Assume that a player is online.
|
2016-06-24 08:14:20 -07:00
|
|
|
if [[ $(echo "${response}" | grep ":" | sed -r -e '$!d' -e 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' -e 's/.*\: //' | tr -d '\n' | wc -c) -le 1 ]]; then
|
2016-05-28 04:11:17 -07:00
|
|
|
# No player is online
|
2016-06-24 08:14:20 -07:00
|
|
|
return 0
|
|
|
|
elif [[ $(echo "${response}" | grep "There are" | sed -r -e '$!d' -e 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' -e 's/.*\: //' -e 's/^([^.]+).*$/\1/; s/^[^0-9]*([0-9]+).*$/\1/' | tr -d '\n') -eq 0 ]]; then
|
2016-05-28 04:11:17 -07:00
|
|
|
# No player is online
|
2016-06-24 08:14:20 -07:00
|
|
|
return 0
|
2016-05-28 04:11:17 -07:00
|
|
|
else
|
|
|
|
# A player is online (or it could not be determined)
|
2016-06-24 08:14:20 -07:00
|
|
|
return 1
|
2016-05-28 04:11:17 -07:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-29 12:35:38 -08:00
|
|
|
# Check whether the server is visited by a player otherwise shut it down
|
|
|
|
idle_server_daemon() {
|
|
|
|
# This function is run within a screen session of the GAME_USER therefore SUDO_CMD can be omitted
|
|
|
|
if [[ $(whoami) != ${GAME_USER} ]]; then
|
|
|
|
>&2 echo "Somehow this hidden function was not executed by the ${GAME_USER} user."
|
|
|
|
>&2 echo "This should not have happend. Are you messing around with this script? :P"
|
|
|
|
exit 22
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Time in seconds for which no player was on the server
|
|
|
|
no_player=0
|
|
|
|
|
|
|
|
while true; do
|
2016-05-21 15:31:25 -07:00
|
|
|
echo -e "no_player: ${no_player}s\tcheck_player_time: ${CHECK_PLAYER_TIME}s\tidle_if_time: ${IDLE_IF_TIME}s"
|
2016-03-05 03:18:45 -08:00
|
|
|
# Retry in ${CHECK_PLAYER_TIME} seconds
|
|
|
|
sleep ${CHECK_PLAYER_TIME}
|
|
|
|
|
2016-02-29 12:35:38 -08:00
|
|
|
screen -S "${SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
|
|
|
# Game server is up and running
|
2016-12-29 05:04:41 -08:00
|
|
|
if [[ "$(screen -S "${SESSION_NAME}" -ls | sed -n "s/.*${SESSION_NAME}\s\+//gp")" == "(Attached)" ]]; then
|
2016-05-20 02:53:30 -07:00
|
|
|
# An administrator is connected to the console, pause player checking
|
|
|
|
echo "An admin is connected to the console. Pause player checking."
|
2016-05-28 04:11:17 -07:00
|
|
|
# Check for active player
|
|
|
|
elif SUDO_CMD="" is_player_online; then
|
2016-02-29 12:35:38 -08:00
|
|
|
# No player was seen on the server through list
|
2016-05-10 15:18:26 -07:00
|
|
|
no_player=$(( no_player + CHECK_PLAYER_TIME ))
|
2016-02-29 12:35:38 -08:00
|
|
|
# Stop the game server if no player was active for at least ${IDLE_IF_TIME}
|
2016-03-05 03:18:45 -08:00
|
|
|
if [[ "${no_player}" -ge "${IDLE_IF_TIME}" ]]; then
|
|
|
|
IDLE_SERVER="false" ${myname} stop
|
2016-03-06 04:50:10 -08:00
|
|
|
# Wait for game server to go down
|
2016-03-13 14:07:58 -07:00
|
|
|
for i in {1..100}; do
|
2016-03-06 04:50:10 -08:00
|
|
|
screen -S "${SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
[[ $? -eq 1 ]] && break
|
2016-03-29 08:44:57 -07:00
|
|
|
[[ $i -eq 100 ]] && echo -e "An \e[39;1merror\e[0m occurred while trying to reset the idle_server!"
|
2016-03-06 04:50:10 -08:00
|
|
|
sleep 0.1
|
|
|
|
done
|
2016-05-10 15:18:26 -07:00
|
|
|
# Reset timer and give the player 300 seconds to connect after pinging
|
|
|
|
no_player=$(( IDLE_IF_TIME - 300 ))
|
2016-04-03 09:00:41 -07:00
|
|
|
# Game server is down, listen on port ${GAME_PORT} for incoming connections
|
2016-05-20 02:53:30 -07:00
|
|
|
echo -n "Netcat: "
|
2016-03-05 03:18:45 -08:00
|
|
|
${NETCAT_CMD} -v -l -p ${GAME_PORT}
|
2016-03-29 08:44:57 -07:00
|
|
|
[[ $? -eq 0 ]] && echo "Netcat caught an connection. The server is coming up again..."
|
2016-03-05 03:18:45 -08:00
|
|
|
IDLE_SERVER="false" ${myname} start
|
|
|
|
fi
|
2016-05-21 15:31:25 -07:00
|
|
|
else
|
2016-05-10 15:18:26 -07:00
|
|
|
# Reset timer since there is an active player on the server
|
2016-02-29 12:35:38 -08:00
|
|
|
no_player=0
|
|
|
|
fi
|
|
|
|
else
|
2016-05-10 15:18:26 -07:00
|
|
|
# Reset timer and give the player 300 seconds to connect after pinging
|
|
|
|
no_player=$(( IDLE_IF_TIME - 300 ))
|
2016-02-29 12:35:38 -08:00
|
|
|
# Game server is down, listen on port ${GAME_PORT} for incoming connections
|
2016-05-20 02:53:30 -07:00
|
|
|
echo -n "Netcat: "
|
2016-03-05 01:08:55 -08:00
|
|
|
${NETCAT_CMD} -v -l -p ${GAME_PORT}
|
2016-04-03 09:00:41 -07:00
|
|
|
[[ $? -eq 0 ]] && echo "Netcat caught an connection. The server is coming up again..."
|
2016-02-29 12:35:38 -08:00
|
|
|
IDLE_SERVER="false" ${myname} start
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2015-09-20 14:23:34 -07:00
|
|
|
# Start the server if it is not already running
|
|
|
|
server_start() {
|
2016-02-29 12:35:38 -08:00
|
|
|
# Start the game server
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
2015-12-13 06:32:38 -08:00
|
|
|
echo "A screen ${SESSION_NAME} session is already running. Please close it first."
|
|
|
|
else
|
2016-03-29 08:44:57 -07:00
|
|
|
echo -en "Starting server..."
|
2016-06-24 08:14:20 -07:00
|
|
|
${SUDO_CMD} screen -dmS "${SESSION_NAME}" /bin/bash -c "cd '${SERVER_ROOT}'; ${SERVER_START_CMD}"
|
2016-08-04 03:51:07 -07:00
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -X logfile "${GAME_COMMAND_DUMP}"
|
2015-12-13 06:32:38 -08:00
|
|
|
echo -e "\e[39;1m done\e[0m"
|
|
|
|
fi
|
2016-02-29 12:35:38 -08:00
|
|
|
|
2016-08-09 02:56:49 -07:00
|
|
|
if [[ "${IDLE_SERVER,,}" == "true" ]]; then
|
2016-02-29 12:35:38 -08:00
|
|
|
# Check for the availability of the netcat (nc) binaries
|
|
|
|
if [[ -z "${NETCAT_CMD}" ]]; then
|
|
|
|
>&2 echo "The netcat binaries are needed for suspending an idle server."
|
|
|
|
exit 12
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Start the idle server daemon
|
|
|
|
${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
2016-03-05 03:18:45 -08:00
|
|
|
${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -X quit
|
2016-03-06 04:50:10 -08:00
|
|
|
# Restart as soon as the idle_server_daemon has shut down completely
|
2016-03-13 14:07:58 -07:00
|
|
|
for i in {1..100}; do
|
2016-03-06 04:50:10 -08:00
|
|
|
${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
[[ $? -eq 1 ]] && ${SUDO_CMD} screen -dmS "${IDLE_SESSION_NAME}" /bin/bash -c "${myname} idle_server_daemon" && break
|
2016-03-29 08:44:57 -07:00
|
|
|
[[ $i -eq 100 ]] && echo -e "An \e[39;1merror\e[0m occurred while trying to reset the idle_server!"
|
2016-03-06 04:50:10 -08:00
|
|
|
sleep 0.1
|
|
|
|
done
|
2016-02-29 12:35:38 -08:00
|
|
|
else
|
2016-03-29 08:44:57 -07:00
|
|
|
echo -en "Starting idle server daeomon..."
|
2016-02-29 12:35:38 -08:00
|
|
|
${SUDO_CMD} screen -dmS "${IDLE_SESSION_NAME}" /bin/bash -c "${myname} idle_server_daemon"
|
|
|
|
echo -e "\e[39;1m done\e[0m"
|
|
|
|
fi
|
|
|
|
fi
|
2015-09-20 14:23:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
# Stop the server gracefully by saving everything prior and warning the users
|
|
|
|
server_stop() {
|
2016-02-29 12:35:38 -08:00
|
|
|
# Quit the idle daemon
|
2016-08-09 02:56:49 -07:00
|
|
|
if [[ "${IDLE_SERVER,,}" == "true" ]]; then
|
2016-02-29 12:35:38 -08:00
|
|
|
# Check for the availability of the netcat (nc) binaries
|
|
|
|
if [[ -z "${NETCAT_CMD}" ]]; then
|
|
|
|
>&2 echo "The netcat binaries are needed for suspending an idle server."
|
|
|
|
exit 12
|
|
|
|
fi
|
|
|
|
|
|
|
|
${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
2016-03-29 08:44:57 -07:00
|
|
|
echo -en "Stopping idle server daemon..."
|
2016-02-29 12:35:38 -08:00
|
|
|
${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -X quit
|
|
|
|
echo -e "\e[39;1m done\e[0m"
|
|
|
|
else
|
|
|
|
echo "The corresponding screen session for ${IDLE_SESSION_NAME} was already dead."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Gracefully exit the game server
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
2016-04-03 04:43:10 -07:00
|
|
|
# Game server is up and running, gracefully stop the server when there are still active players
|
2016-05-20 02:53:30 -07:00
|
|
|
|
2016-05-28 04:11:17 -07:00
|
|
|
# Check for active player
|
|
|
|
if is_player_online; then
|
2016-03-29 08:44:57 -07:00
|
|
|
# No player was seen on the server through list
|
|
|
|
echo -en "Server is going down..."
|
|
|
|
game_command stop
|
|
|
|
else
|
|
|
|
# Player(s) were seen on the server through list (or an error occurred)
|
|
|
|
# Warning the users through the server console
|
|
|
|
game_command say "Server is going down in 10 seconds! HURRY UP WITH WHATEVER YOU ARE DOING!"
|
2016-04-03 04:43:10 -07:00
|
|
|
game_command save-all
|
2016-03-29 08:44:57 -07:00
|
|
|
echo -en "Server is going down in..."
|
|
|
|
for i in {1..10}; do
|
|
|
|
game_command say "down in... $(expr 10 - $i)"
|
|
|
|
echo -n " $(expr 10 - $i)"
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
game_command stop
|
|
|
|
fi
|
2016-03-06 04:50:10 -08:00
|
|
|
|
|
|
|
# Finish as soon as the server has shut down completely
|
2016-03-13 14:07:58 -07:00
|
|
|
for i in {1..100}; do
|
2016-03-06 04:50:10 -08:00
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
[[ $? -eq 1 ]] && echo -e "\e[39;1m done\e[0m" && break
|
2016-03-13 14:07:58 -07:00
|
|
|
[[ $i -eq 100 ]] && echo -e "\e[39;1m timed out\e[0m"
|
2016-03-06 04:50:10 -08:00
|
|
|
sleep 0.1
|
|
|
|
done
|
2015-12-13 06:32:38 -08:00
|
|
|
else
|
|
|
|
echo "The corresponding screen session for ${SESSION_NAME} was already dead."
|
|
|
|
fi
|
2015-09-20 14:23:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
# Print whether the server is running and if so give some information about memory usage and threads
|
|
|
|
server_status() {
|
2016-02-29 12:35:38 -08:00
|
|
|
# Print status information about the idle daemon
|
2016-08-09 02:56:49 -07:00
|
|
|
if [[ "${IDLE_SERVER,,}" == "true" ]]; then
|
2016-02-29 12:35:38 -08:00
|
|
|
# Check for the availability of the netcat (nc) binaries
|
|
|
|
if [[ -z "${NETCAT_CMD}" ]]; then
|
|
|
|
>&2 echo "The netcat binaries are needed for suspending an idle server."
|
|
|
|
exit 12
|
|
|
|
fi
|
|
|
|
|
|
|
|
${SUDO_CMD} screen -S "${IDLE_SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
|
|
|
echo -e "Idle server daemon status:\e[39;1m running\e[0m"
|
|
|
|
else
|
|
|
|
echo -e "Idle server daemon status:\e[39;1m stopped\e[0m"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Print status information for the game server
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
2015-12-13 06:32:38 -08:00
|
|
|
echo -e "Status:\e[39;1m running\e[0m"
|
|
|
|
|
|
|
|
# Calculating memory usage
|
2016-02-29 12:35:38 -08:00
|
|
|
for p in $(${SUDO_CMD} pgrep -f "${MAIN_EXECUTABLE}"); do
|
2016-04-03 07:33:16 -07:00
|
|
|
ps -p${p} -O rss | tail -n 1;
|
2016-06-24 08:14:20 -07:00
|
|
|
done | gawk '{ count ++; sum += $2 }; END {count --; print "Number of processes =", count, "(screen, bash,", count-2, "x server)"; print "Total memory usage =", sum/1024, "MB" ;};'
|
2015-12-13 06:32:38 -08:00
|
|
|
else
|
|
|
|
echo -e "Status:\e[39;1m stopped\e[0m"
|
|
|
|
fi
|
2015-09-20 14:23:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
# Restart the complete server by shutting it down and starting it again
|
|
|
|
server_restart() {
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
2015-12-13 06:32:38 -08:00
|
|
|
server_stop
|
|
|
|
server_start
|
|
|
|
else
|
|
|
|
server_start
|
|
|
|
fi
|
2015-09-20 14:23:34 -07:00
|
|
|
}
|
|
|
|
|
2016-02-29 12:35:38 -08:00
|
|
|
# Backup the directories specified in BACKUP_PATHS
|
2015-09-20 14:23:34 -07:00
|
|
|
backup_files() {
|
2016-01-23 03:45:46 -08:00
|
|
|
# Check for the availability of the tar binaries
|
2015-12-13 06:32:38 -08:00
|
|
|
which tar &> /dev/null
|
2016-01-26 11:30:17 -08:00
|
|
|
if [[ $? -ne 0 ]]; then
|
2016-02-29 12:35:38 -08:00
|
|
|
>&2 echo "The tar binaries are needed for a backup."
|
|
|
|
exit 11
|
2015-12-13 06:32:38 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Starting backup..."
|
2016-01-23 03:45:46 -08:00
|
|
|
FILE="$(date +%Y_%m_%d_%H.%M.%S).tar.gz"
|
2016-02-29 12:35:38 -08:00
|
|
|
${SUDO_CMD} mkdir -p "${BACKUP_DEST}"
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
2016-02-29 12:35:38 -08:00
|
|
|
game_command save-off
|
|
|
|
game_command save-all
|
2015-12-13 06:32:38 -08:00
|
|
|
sync && wait
|
2016-02-29 12:35:38 -08:00
|
|
|
${SUDO_CMD} tar -C "${SERVER_ROOT}" -czf "${BACKUP_DEST}/${FILE}" --totals ${BACKUP_PATHS} 2>&1 | grep -v "tar: Removing leading "
|
|
|
|
game_command save-on
|
2015-12-13 06:32:38 -08:00
|
|
|
else
|
2016-02-29 12:35:38 -08:00
|
|
|
${SUDO_CMD} tar -C "${SERVER_ROOT}" -czf "${BACKUP_DEST}/${FILE}" --totals ${BACKUP_PATHS} 2>&1 | grep -v "tar: Removing leading "
|
2015-12-13 06:32:38 -08:00
|
|
|
fi
|
|
|
|
echo -e "\e[39;1mbackup completed\e[0m\n"
|
|
|
|
|
|
|
|
echo -n "Only keeping the last ${KEEP_BACKUPS} backups and removing the other ones..."
|
2016-02-29 12:35:38 -08:00
|
|
|
BACKUP_COUNT=$(for f in "${BACKUP_DEST}"/[0-9_.]*; do echo ${f}; done | wc -l)
|
2016-01-26 11:30:17 -08:00
|
|
|
if [[ $(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS}) -gt 0 ]]; then
|
2016-02-29 12:35:38 -08:00
|
|
|
${SUDO_CMD} rm $(for f in "${BACKUP_DEST}"/[0-9_.]*; do echo ${f}; done | head -n$(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS}))
|
2015-12-13 06:32:38 -08:00
|
|
|
echo -e "\e[39;1m done\e[0m ($(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS}) backup(s) pruned)"
|
|
|
|
else
|
|
|
|
echo -e "\e[39;1m done\e[0m (no backups pruned)"
|
|
|
|
fi
|
2015-09-20 14:23:34 -07:00
|
|
|
}
|
|
|
|
|
2016-01-23 03:45:46 -08:00
|
|
|
# Restore backup
|
|
|
|
backup_restore() {
|
|
|
|
# Check for the availability of the tar binaries
|
|
|
|
which tar &> /dev/null
|
2016-01-26 11:30:17 -08:00
|
|
|
if [[ $? -ne 0 ]]; then
|
2016-02-29 12:35:38 -08:00
|
|
|
>&2 echo "The tar binaries are needed for a backup."
|
|
|
|
exit 11
|
2016-01-23 03:45:46 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Only allow the user to restore a backup if the server is down
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
2016-02-29 12:35:38 -08:00
|
|
|
>&2 echo -e "The \e[39;1mserver should be down\e[0m in order to restore the world data."
|
2016-01-23 03:45:46 -08:00
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Either let the user choose a backup or expect one as an argument
|
2016-01-26 11:30:17 -08:00
|
|
|
if [[ $# -lt 1 ]]; then
|
2016-01-23 03:45:46 -08:00
|
|
|
echo "Please enter the corresponding number for the backup to be restored: "
|
|
|
|
i=1
|
2016-02-29 12:35:38 -08:00
|
|
|
for f in "${BACKUP_DEST}"/[0-9_.]*; do
|
2016-01-23 03:45:46 -08:00
|
|
|
echo -e " \e[39;1m$i)\e[0m\t$f"
|
2016-05-10 15:18:26 -07:00
|
|
|
i=$(( i + 1 ))
|
2016-01-23 03:45:46 -08:00
|
|
|
done
|
|
|
|
echo -en "Restore backup number: "
|
|
|
|
|
|
|
|
# Read in user input
|
2016-01-26 11:30:17 -08:00
|
|
|
read user_choice
|
2016-01-23 03:45:46 -08:00
|
|
|
|
|
|
|
# Interpeting the input
|
2016-01-26 11:30:17 -08:00
|
|
|
if [[ $user_choice =~ ^-?[0-9]+$ ]]; then
|
2016-01-23 03:45:46 -08:00
|
|
|
n=1
|
2016-02-29 12:35:38 -08:00
|
|
|
for f in "${BACKUP_DEST}"/[0-9_.]*; do
|
2016-01-26 11:30:17 -08:00
|
|
|
[[ ${n} -eq $user_choice ]] && FILE="$f"
|
2016-05-10 15:18:26 -07:00
|
|
|
n=$(( n + 1 ))
|
2016-01-23 03:45:46 -08:00
|
|
|
done
|
|
|
|
if [[ -z $FILE ]]; then
|
2016-02-29 12:35:38 -08:00
|
|
|
>&2 echo -e "\e[39;1mFailed\e[0m to interpret your input. Please enter the digit of the presented options."
|
2016-01-23 03:45:46 -08:00
|
|
|
exit 5
|
|
|
|
fi
|
|
|
|
else
|
2016-02-29 12:35:38 -08:00
|
|
|
>&2 echo -e "\e[39;1mFailed\e[0m to interpret your input. Please enter a valid digit for one of the presented options."
|
2016-01-23 03:45:46 -08:00
|
|
|
exit 6
|
|
|
|
fi
|
|
|
|
elif [[ $# -eq 1 ]]; then
|
|
|
|
# Check for the existance of the specified file
|
2016-01-26 11:30:17 -08:00
|
|
|
if [[ -f "$1" ]]; then
|
2016-01-23 03:45:46 -08:00
|
|
|
FILE="$1"
|
|
|
|
else
|
2016-02-29 12:35:38 -08:00
|
|
|
if [[ -f "${BACKUP_DEST}"/"$1" ]]; then
|
|
|
|
FILE="${BACKUP_DEST}"/"$1"
|
2016-01-23 03:45:46 -08:00
|
|
|
else
|
2016-02-29 12:35:38 -08:00
|
|
|
>&2 echo -e "Sorry, but '$1', is \e[39;1mnot a valid file\e[0m, neither in your current directory nor in the backup folder."
|
2016-01-23 03:45:46 -08:00
|
|
|
exit 4
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
elif [[ $# -gt 1 ]]; then
|
2016-02-29 12:35:38 -08:00
|
|
|
>&2 echo -e "\e[39;1mToo many arguments.\e[0m Please pass only the filename for the world data as an argument."
|
|
|
|
>&2 echo "Or alternatively no arguments at all to chose from a list of available backups."
|
|
|
|
exit 7
|
2016-01-23 03:45:46 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Restoring backup..."
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} tar -xf "${FILE}" -C "${SERVER_ROOT}" 2>&1
|
2016-01-23 03:45:46 -08:00
|
|
|
if [[ $? -eq 0 ]]; then
|
|
|
|
echo -e "\e[39;1mRestoration completed\e[0m"
|
|
|
|
else
|
|
|
|
echo -e "\e[39;1mFailed to restore backup.\e[0m"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-02-29 12:35:38 -08:00
|
|
|
# Run the given comman at the game server console
|
2015-09-20 14:23:34 -07:00
|
|
|
server_command() {
|
2016-01-26 11:30:17 -08:00
|
|
|
if [[ $# -lt 1 ]]; then
|
2016-02-29 12:35:38 -08:00
|
|
|
>&2 echo "No server command specified. Try 'help' for a list of commands."
|
2015-12-13 06:32:38 -08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
2016-06-24 08:14:20 -07:00
|
|
|
return_stdout=true game_command "$@"
|
2015-12-13 06:32:38 -08:00
|
|
|
else
|
|
|
|
echo "There is no ${SESSION_NAME} session to connect to."
|
|
|
|
fi
|
2015-09-20 14:23:34 -07:00
|
|
|
}
|
|
|
|
|
2016-02-29 12:35:38 -08:00
|
|
|
# Enter the screen game session
|
2015-09-20 14:23:34 -07:00
|
|
|
server_console() {
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -rx
|
2015-12-13 06:32:38 -08:00
|
|
|
else
|
|
|
|
echo "There is no ${SESSION_NAME} session to connect to."
|
|
|
|
fi
|
2015-09-20 14:23:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
# Help function, no arguments required
|
|
|
|
help() {
|
2016-01-25 15:18:20 -08:00
|
|
|
cat <<-EOF
|
2016-02-29 12:35:38 -08:00
|
|
|
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.
|
2015-12-13 06:32:38 -08:00
|
|
|
|
2016-02-29 12:35:38 -08:00
|
|
|
Usage: ${myname} {start|stop|status|backup|restore|command <command>|console}
|
|
|
|
start Start the ${game} server
|
|
|
|
stop Stop the ${game} server
|
|
|
|
restart Restart the ${game} server
|
2015-12-14 02:51:31 -08:00
|
|
|
status Print some status information
|
|
|
|
backup Backup the world data
|
2016-01-23 03:45:46 -08:00
|
|
|
restore [filename] Restore the world data from a backup
|
2016-02-29 12:35:38 -08:00
|
|
|
command <command> Run the given comman at the ${game} server console
|
2015-12-14 02:51:31 -08:00
|
|
|
console Enter the server console through a screen session
|
2015-12-13 06:32:38 -08:00
|
|
|
|
|
|
|
Copyright (c) Gordian Edenhofer <gordian.edenhofer@gmail.com>
|
|
|
|
EOF
|
2015-09-20 14:23:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
start)
|
2015-12-13 06:32:38 -08:00
|
|
|
server_start
|
|
|
|
;;
|
2015-09-20 14:23:34 -07:00
|
|
|
|
|
|
|
stop)
|
2015-12-13 06:32:38 -08:00
|
|
|
server_stop
|
|
|
|
;;
|
2015-09-20 14:23:34 -07:00
|
|
|
|
2015-12-13 06:32:38 -08:00
|
|
|
status)
|
|
|
|
server_status
|
|
|
|
;;
|
2015-09-20 14:23:34 -07:00
|
|
|
|
2015-12-13 06:32:38 -08:00
|
|
|
restart)
|
|
|
|
server_restart
|
|
|
|
;;
|
2015-09-20 14:23:34 -07:00
|
|
|
|
|
|
|
console)
|
2015-12-13 06:32:38 -08:00
|
|
|
server_console
|
|
|
|
;;
|
2015-09-20 14:23:34 -07:00
|
|
|
|
2015-12-13 06:32:38 -08:00
|
|
|
command)
|
|
|
|
server_command "${@:2}"
|
|
|
|
;;
|
2015-09-20 14:23:34 -07:00
|
|
|
|
|
|
|
backup)
|
2015-12-13 06:32:38 -08:00
|
|
|
backup_files
|
|
|
|
;;
|
2015-09-20 14:23:34 -07:00
|
|
|
|
2016-01-23 03:45:46 -08:00
|
|
|
restore)
|
|
|
|
backup_restore "${@:2}"
|
|
|
|
;;
|
|
|
|
|
2016-02-29 12:35:38 -08:00
|
|
|
idle_server_daemon)
|
|
|
|
# This shell be a hidden function which should only be invoced internally
|
|
|
|
idle_server_daemon
|
|
|
|
;;
|
|
|
|
|
2015-09-20 14:23:34 -07:00
|
|
|
*|-h|--help)
|
2015-12-13 06:32:38 -08:00
|
|
|
help
|
2016-02-29 12:35:38 -08:00
|
|
|
exit 0
|
2015-09-20 14:23:34 -07:00
|
|
|
esac
|
|
|
|
|
|
|
|
exit 0
|