2015-09-20 14:23:34 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
source /etc/conf.d/minecraft || echo "Could not source /etc/conf.d/minecraft"
|
|
|
|
|
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.
|
|
|
|
|
2015-09-20 14:23:34 -07:00
|
|
|
# You may use this script for any minecraft server of your choice, just alter the config file
|
|
|
|
SERVER_ROOT="${SERVER_ROOT:-/srv/minecraft}"
|
|
|
|
BACKUPPATH="${BACKUPPATH:-/srv/minecraft/backup}"
|
|
|
|
LOGPATH="${LOGPATH:-/srv/minecraft/logs}"
|
2016-01-23 03:45:46 -08:00
|
|
|
WORLDPATHS="${WORLDPATHS:-world}"
|
2015-09-20 14:23:34 -07:00
|
|
|
KEEP_BACKUPS="${KEEP_BACKUPS:-10}"
|
|
|
|
MC_USER="${MC_USER:-minecraft}"
|
|
|
|
MAIN_JAR="${MAIN_JAR:-minecraft_server.jar}"
|
|
|
|
SESSION_NAME="${SESSION_NAME:-minecraft}"
|
|
|
|
|
|
|
|
# Specify system parameters for the minecraft server
|
|
|
|
MINHEAP="${MINHEAP:-512M}"
|
|
|
|
MAXHEAP="${MAXHEAP:-1024M}"
|
|
|
|
THREADS="${THREADS:-1}"
|
|
|
|
JAVA_PARMS="${JAVA_PARMS:-"-Xmx${MAXHEAP} -Xms${MINHEAP} -XX:ParallelGCThreads=${THREADS}"}"
|
|
|
|
|
2016-01-25 15:18:20 -08:00
|
|
|
# The actual program name
|
|
|
|
declare -r myname="minecraftd"
|
|
|
|
|
2016-01-26 11:30:17 -08:00
|
|
|
# Check whether sudo is needed at all
|
|
|
|
if [[ $(whoami) == ${MC_USER} ]]; then
|
|
|
|
SUDO_CMD=""
|
|
|
|
else
|
|
|
|
SUDO_CMD="sudo -u ${MC_USER}"
|
|
|
|
fi
|
|
|
|
|
2015-09-20 14:23:34 -07:00
|
|
|
# Check for sudo rigths
|
2016-01-26 11:30:17 -08:00
|
|
|
if [[ $(${SUDO_CMD} whoami) != ${MC_USER} ]]; then
|
|
|
|
echo -e "You have \e[39;1mno permission\e[0m to run commands as $MC_USER user."
|
2015-12-13 06:32:38 -08:00
|
|
|
exit 1
|
2015-09-20 14:23:34 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Pipe any given argument to the minecraft server console
|
|
|
|
mc_command() {
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -X stuff "`printf \"$*\r\"`"
|
2015-09-20 14:23:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
# Start the server if it is not already running
|
|
|
|
server_start() {
|
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
|
|
|
|
echo -en "Starting server... "
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} screen -dmS "${SESSION_NAME}" /bin/bash -c "cd '${SERVER_ROOT}'; java ${JAVA_PARMS} -jar '${SERVER_ROOT}/${MAIN_JAR}' nogui"
|
2015-12-13 06:32:38 -08:00
|
|
|
echo -e "\e[39;1m done\e[0m"
|
|
|
|
fi
|
2015-09-20 14:23:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
# Stop the server gracefully by saving everything prior and warning the users
|
|
|
|
server_stop() {
|
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
|
|
|
mc_command save-all
|
|
|
|
mc_command say "Server is going down in 10 seconds! HURRY UP WITH WATHEVER YOU ARE DOING!" # Warning the users
|
|
|
|
echo -en "Server is going down in... "
|
2016-01-23 03:45:46 -08:00
|
|
|
for i in $(seq 1 10); do
|
2015-12-13 06:32:38 -08:00
|
|
|
mc_command say "down in... $(expr 10 - $i)"
|
|
|
|
echo -n " $(expr 10 - $i)"
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
mc_command stop
|
|
|
|
echo -e "\e[39;1m done\e[0m"
|
|
|
|
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-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-01-26 11:30:17 -08:00
|
|
|
for p in $(${SUDO_CMD} pgrep -f "${MAIN_JAR}"); do
|
|
|
|
ps -p${p} -O rss | tail -n1;
|
2015-12-13 06:32:38 -08:00
|
|
|
done | gawk '{ count ++; sum += $2 }; END {count --; print "Number of processes =", count, "(screen, bash,", count-2, "x java)"; print "Total memory usage =", sum/1024, "MB" ;};'
|
|
|
|
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
|
|
|
|
sleep 0.1
|
|
|
|
server_start
|
|
|
|
else
|
|
|
|
server_start
|
|
|
|
fi
|
2015-09-20 14:23:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
# Backup the directories specified in $WORLDPATHS
|
|
|
|
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
|
2015-12-13 06:32:38 -08:00
|
|
|
echo "The tar binaries are needed for a backup."
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Starting backup..."
|
2016-01-23 03:45:46 -08:00
|
|
|
FILE="$(date +%Y_%m_%d_%H.%M.%S).tar.gz"
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} mkdir -p "${BACKUPPATH}"
|
|
|
|
${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
|
|
|
|
if [[ $? -eq 0 ]]; then
|
2015-12-13 06:32:38 -08:00
|
|
|
mc_command save-off
|
|
|
|
mc_command save-all
|
|
|
|
sync && wait
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} tar -C "${SERVER_ROOT}" -czf "${BACKUPPATH}/${FILE}" --totals "${WORLDPATHS}" 2>&1 | grep -v "tar: Removing leading "
|
2015-12-13 06:32:38 -08:00
|
|
|
mc_command save-on
|
|
|
|
else
|
2016-01-26 11:30:17 -08:00
|
|
|
${SUDO_CMD} tar -C "${SERVER_ROOT}" -czf "${BACKUPPATH}/${FILE}" --totals "${WORLDPATHS}" 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-01-26 11:30:17 -08:00
|
|
|
BACKUP_COUNT=$(for f in "${BACKUPPATH}"/[0-9_.]*; do echo ${f}; done | wc -l)
|
|
|
|
if [[ $(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS}) -gt 0 ]]; then
|
|
|
|
${SUDO_CMD} rm $(for f in "${BACKUPPATH}"/[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-01-23 03:45:46 -08:00
|
|
|
echo "The tar binaries are needed for a backup."
|
|
|
|
exit 2
|
|
|
|
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-01-23 03:45:46 -08:00
|
|
|
echo -e "The \e[39;1mserver should be down\e[0m in order to restore the world data."
|
|
|
|
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
|
|
|
|
for f in "${BACKUPPATH}"/[0-9_.]*; do
|
|
|
|
echo -e " \e[39;1m$i)\e[0m\t$f"
|
|
|
|
i=$((i+1))
|
|
|
|
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
|
|
|
|
for f in "${BACKUPPATH}"/[0-9_.]*; do
|
2016-01-26 11:30:17 -08:00
|
|
|
[[ ${n} -eq $user_choice ]] && FILE="$f"
|
2016-01-23 03:45:46 -08:00
|
|
|
n=$((n+1))
|
|
|
|
done
|
|
|
|
if [[ -z $FILE ]]; then
|
|
|
|
echo -e "\e[39;1mFailed\e[0m to interpret your input. Please enter the digit of the presented options."
|
|
|
|
exit 5
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo -e "\e[39;1mFailed\e[0m to interpret your input. Please enter a valid digit for one of the presented options."
|
|
|
|
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-01-26 11:30:17 -08:00
|
|
|
if [[ -f "${BACKUPPATH}"/"$1" ]]; then
|
2016-01-23 03:45:46 -08:00
|
|
|
FILE="${BACKUPPATH}"/"$1"
|
|
|
|
else
|
|
|
|
echo -e "Sorry, but '$1', is \e[39;1mnot a valid file\e[0m, neither in your current directory nor in the backup folder."
|
|
|
|
exit 4
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
elif [[ $# -gt 1 ]]; then
|
|
|
|
echo -e "\e[39;1mToo many arguments.\e[0m Please pass only the filename for the world data as an argument."
|
|
|
|
echo "Or alternatively no arguments at all to chose from a list of available backups."
|
|
|
|
exit 1
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-20 14:23:34 -07:00
|
|
|
# Run the given comman at the minecraft server console
|
|
|
|
server_command() {
|
2016-01-26 11:30:17 -08:00
|
|
|
if [[ $# -lt 1 ]]; then
|
2015-12-13 06:32:38 -08:00
|
|
|
echo "No server command specified. Try 'help' for a list of commands."
|
|
|
|
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
|
2015-12-14 12:32:27 -08:00
|
|
|
sleep 0.1s &
|
2016-01-26 11:30:17 -08:00
|
|
|
sleep_pid=$!
|
2015-12-14 02:51:31 -08:00
|
|
|
mc_command "$@" &
|
2016-01-26 11:30:17 -08:00
|
|
|
tail -f --pid=${sleep_pid} -n 0 "${LOGPATH}/latest.log"
|
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
|
|
|
}
|
|
|
|
|
|
|
|
# Enter the screen minecraft session
|
|
|
|
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
|
2015-12-13 06:32:38 -08:00
|
|
|
This script was design to easily control any minecraft server. Quite every parameter for a given
|
|
|
|
minecraft server derivative can be altered by editing the variables in the configuration file.
|
|
|
|
|
2016-01-25 15:18:20 -08:00
|
|
|
Usage: $myname {start|stop|status|backup|restore|command <command>|console}
|
2015-12-14 02:51:31 -08:00
|
|
|
start Start the minecraft server
|
|
|
|
stop Stop the minecraft server
|
|
|
|
restart Restart the minecraft server
|
|
|
|
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
|
2015-12-14 02:51:31 -08:00
|
|
|
command <command> Run the given comman at the minecraft server console
|
|
|
|
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}"
|
|
|
|
;;
|
|
|
|
|
2015-09-20 14:23:34 -07:00
|
|
|
*|-h|--help)
|
2015-12-13 06:32:38 -08:00
|
|
|
help
|
2015-09-20 14:23:34 -07:00
|
|
|
esac
|
|
|
|
|
|
|
|
exit 0
|