diff --git a/.SRCINFO b/.SRCINFO index 030cb45..b3f9bd9 100644 --- a/.SRCINFO +++ b/.SRCINFO @@ -1,33 +1,33 @@ pkgbase = minecraft-server pkgdesc = Minecraft server unit files, script, and jar pkgver = 1.8.8 - pkgrel = 1 + pkgrel = 2 url = http://minecraft.net/ install = minecraft-server.install arch = any license = custom depends = java-runtime-headless - depends = systemd depends = screen - depends = expect + depends = sudo + depends = bash + optdepends = tar: needed in order to create world backups conflicts = minecraft-server-systemd conflicts = minecraft-canary noextract = minecraft_server.1.8.8.jar options = !strip - options = emptydirs backup = etc/conf.d/minecraft source = https://s3.amazonaws.com/Minecraft.Download/versions/1.8.8/minecraft_server.1.8.8.jar - source = minecraftd - source = minecraftd-diag + source = minecraftd-backup.service + source = minecraftd-backup.timer source = minecraftd.service - source = minecraftctl - source = conf.minecraft - sha256sums = 39aef720dc5309476f56f2e96a516f3dd3041bbbf442cbfd47d63acbd06af31e - sha256sums = 043101c29d4b4f9092cb93ef52168cfe3d16d49ff573136f4fbb45071a38a5d1 - sha256sums = de03317d02668bb71ec160fff497c7fbfe8b03ffff2950c6a0bcfb39f69cd214 - sha256sums = 6eb4c085b8377712671445b891a3a2cf2a17cf7a7bea66be456a980c2b620e23 - sha256sums = 472e06e1bd63838e88ace0bd2f971f72f2efd9e541cc0956599a44c324fd49ac - sha256sums = 738e10185df160d190a5f4f0226de110d67ba023877e4999a6d3ef41c9918ed6 + source = minecraftd.conf + source = minecraftd.sh + md5sums = a0671390aa0691e70a950155aab06ffb + md5sums = 2cf6cdf65e0ed6aa6d452943b1e84357 + md5sums = c644abdf293c2f98033a64d732a4945c + md5sums = 5ed78e366146e47f8498347e93ad5423 + md5sums = d4656f27716fd78ab64344a517b07443 + md5sums = 1991eb0aedeba1dab2371f22a6736b47 pkgname = minecraft-server diff --git a/PKGBUILD b/PKGBUILD index 227e236..9049dc4 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,40 +1,42 @@ -# Maintainer: Philip Abernethy +# Maintainer: Gordian Edenhofer +# Contributer: Philip Abernethy pkgname=minecraft-server pkgver=1.8.8 -pkgrel=1 +pkgrel=2 pkgdesc="Minecraft server unit files, script, and jar" -arch=(any) +arch=('any') url="http://minecraft.net/" license=('custom') -depends=('java-runtime-headless' 'systemd' 'screen' 'expect') +depends=('java-runtime-headless' 'screen' 'sudo' 'bash') +optdepends=('tar: needed in order to create world backups') conflicts=('minecraft-server-systemd' 'minecraft-canary') -options=(!strip emptydirs) -install=minecraft-server.install +options=(!strip) +install=${pkgname}.install backup=('etc/conf.d/minecraft') -source=(https://s3.amazonaws.com/Minecraft.Download/versions/${pkgver}/minecraft_server.${pkgver}.jar - minecraftd - minecraftd-diag - minecraftd.service - minecraftctl - conf.minecraft) -noextract=("minecraft_server.${pkgver}.jar") -sha256sums=('39aef720dc5309476f56f2e96a516f3dd3041bbbf442cbfd47d63acbd06af31e' - '043101c29d4b4f9092cb93ef52168cfe3d16d49ff573136f4fbb45071a38a5d1' - 'de03317d02668bb71ec160fff497c7fbfe8b03ffff2950c6a0bcfb39f69cd214' - '6eb4c085b8377712671445b891a3a2cf2a17cf7a7bea66be456a980c2b620e23' - '472e06e1bd63838e88ace0bd2f971f72f2efd9e541cc0956599a44c324fd49ac' - '738e10185df160d190a5f4f0226de110d67ba023877e4999a6d3ef41c9918ed6') +source=("https://s3.amazonaws.com/Minecraft.Download/versions/${pkgver}/minecraft_server.${pkgver}.jar" + "minecraftd-backup.service" + "minecraftd-backup.timer" + "minecraftd.service" + "minecraftd.conf" + "minecraftd.sh") +noextract=("minecraft_server.${pkgver}.jar") +md5sums=('a0671390aa0691e70a950155aab06ffb' + '2cf6cdf65e0ed6aa6d452943b1e84357' + 'c644abdf293c2f98033a64d732a4945c' + '5ed78e366146e47f8498347e93ad5423' + 'd4656f27716fd78ab64344a517b07443' + '1991eb0aedeba1dab2371f22a6736b47') package() { - install -Dm755 "${srcdir}/minecraftd" "${pkgdir}/usr/bin/minecraftd" - install -Dm755 "${srcdir}/minecraftd-diag" "${pkgdir}/usr/bin/minecraftd-diag" - install -Dm755 "${srcdir}/minecraftctl" "${pkgdir}/usr/bin/minecraftctl" - - install -Dm644 "${srcdir}/minecraft_server.${pkgver}".jar "${pkgdir}/srv/minecraft/minecraft_server.${pkgver}".jar - ln -s "minecraft_server.${pkgver}.jar" "${pkgdir}/srv/minecraft/minecraft_server.jar" - install -Dm644 "${srcdir}/minecraftd.service" "${pkgdir}/usr/lib/systemd/system/minecraftd.service" - install -Dm644 "${srcdir}/conf.minecraft" "${pkgdir}/etc/conf.d/minecraft" + install -Dm644 minecraftd.conf "${pkgdir}/etc/conf.d/minecraft" + install -Dm755 minecraftd.sh "${pkgdir}/usr/bin/minecraftd" + install -Dm644 minecraftd.service "${pkgdir}/usr/lib/systemd/system/minecraftd.service" + install -Dm644 minecraftd-backup.service "${pkgdir}/usr/lib/systemd/system/minecraftd-backup.service" + install -Dm644 minecraftd-backup.timer "${pkgdir}/usr/lib/systemd/system/minecraftd-backup.timer" + install -Dm644 minecraft_server.${pkgver}.jar "${pkgdir}/srv/minecraft/minecraft_server.${pkgver}.jar" + ln -s "minecraft_server.${pkgver}.jar" "${pkgdir}/srv/minecraft/minecraft_server.jar" - install -d "${pkgdir}/srv/minecraft/backup" + mkdir -p "${pkgdir}/var/log/" + ln -s "/srv/minecraft/logs" "${pkgdir}/var/log/minecraft" #&>/dev/null } diff --git a/conf.minecraft b/conf.minecraft deleted file mode 100644 index a78e2a1..0000000 --- a/conf.minecraft +++ /dev/null @@ -1,37 +0,0 @@ -# MINECRAFT SERVER LAUNCH CONFIGS -# not to be confused with server.properties. - -# Server root directory -_SRVDIR=/srv/minecraft - -# World directory (relative to SRVDIR) -# Ensure that this matches what is in your server.properties, -# or else the wrong folder will be backed up! -WORLD=world - -# Maximum RAM usage (change to suit your needs) -MAXHEAP=1024M - -# Minimum RAM usage (change to suit your needs) -MINHEAP=512M - -# Number of concurrent threads (no more than the number of logical processors in your system) -THREADS=1 - -# Where to store backup files -BACKUPPATH=/srv/minecraft/backup - -# What to say to the server when backup starts -BACKUPSTARTMSG="WORLD BACKUP STARTING. Server going read-only..." - -# What to say when to the server when backup ends -BACKUPENDMSG="WORLD BACKUP ENDED. Server going read-write..." - -## Only change these if you know what you're doing - -# Server JAR file -SERVERJAR=minecraft_server.jar -SERVICE=${SERVERJAR} - -# User to run server under -MCUSER=minecraft diff --git a/minecraft-server.install b/minecraft-server.install index 3031324..97c66de 100644 --- a/minecraft-server.install +++ b/minecraft-server.install @@ -1,33 +1,55 @@ +USER="minecraft" +SERVER_ROOT="/srv/minecraft" + +_update() { + getent group "${USER}" &>/dev/null + if [ $? -ne 0 ]; then + echo -e "\e[34;1m==>\e[39;1m Adding ${USER} system group... \e[0m" + groupadd -r ${USER} 1>/dev/null + fi + + getent passwd "${USER}" &>/dev/null + if [ $? -ne 0 ]; then + echo -e "\e[34;1m==>\e[39;1m Adding ${USER} system user... \e[0m" + useradd -r -g ${USER} -d "${SERVER_ROOT}" ${USER} 1>/dev/null + fi + + chown -R ${USER}:${USER} "${SERVER_ROOT}" + + echo -e "\e[34;1m==>\e[39;1m NOTE: The world data is stored under ${SERVER_ROOT} and the server runs \e[0m" + echo -e "\e[34;1m==>\e[39;1m as ${USER} user to increase security. \e[0m" + echo -e "\e[34;1m==>\e[39;1m NOTE: Use the minecraft script under /usr/bin/minecraft to start, stop or backup the server \e[0m" + echo -e "\e[34;1m==>\e[39;1m and the configuration file under /etc/conf.d/minecraft to adjust it to your liking. \e[0m" + echo -e "\e[34;1m==>\e[39;1m NOTE: For the server to start you have to accept the EULA in ${SERVER_ROOT}/eula.txt \e[0m" + echo -e "\e[34;1m==>\e[39;1m which is generated after the first server start. \e[0m" +} + post_install() { - post_upgrade + _update } post_upgrade() { - getent group "minecraft" &>/dev/null || groupadd -r minecraft 1>/dev/null - getent passwd "minecraft" &>/dev/null && usermod -s "/usr/bin/nologin" minecraft &>/dev/null || useradd -r -g minecraft -d "/srv/minecraft" -s "/usr/bin/nologin" minecraft 1>/dev/null - touch /srv/minecraft/server.log - chown -R minecraft:minecraft "/srv/minecraft" 1>/dev/null - ln -s /srv/minecraft/server.log /var/log/minecraft.log &>/dev/null - # Removes old data from previous version - cat << EOF -==> World data is stored under /srv/minecraft -==> The server runs as "minecraft", not root -==> You can access the server's console as root with: - $ screen -r minecraft -==> The systemd service is called "minecraftd.service" -==> Calling "stop" or "restart" with systemctl will gracefully exit the server, saving world data -==> Modify /etc/conf.d/minecraft to change the invocation of the server if you wish -==> A script at /usr/bin/minecraftctl is also provided. See the wiki for details on its usage. -==> For the server to start you have to accept the EULA in /srv/minecraft/eula.txt - -EOF + _update +} + +pre_remove() { + echo -e "\e[34;1m==>\e[39;1m Stopping and disabling minecraftd... \e[0m" + systemctl stop minecrafd.service + systemctl disable minecraftd.service + + /usr/bin/minecraftd stop > /dev/null } post_remove() { - if getent passwd "minecraft" >/dev/null; then - userdel minecraft >/dev/null - fi - if getent group "minecraft" >/dev/null; then - groupdel minecraft >/dev/null - fi + echo -e "\e[34;1m==>\e[39;1m Removing ${USER} system user and group... \e[0m" + if getent passwd ${USER} >/dev/null 2>&1; then + userdel ${USER} 2>/dev/null + fi + if getent group ${USER} >/dev/null 2>&1; then + groupdel ${USER} + fi + + # Notifying the user of kept dirs + [ -d ${SERVER_ROOT} ] && chown root:root ${SERVER_ROOT} && echo -e "\e[34;1m==>\e[39;1m NOTE: Game saves in ${SERVER_ROOT} were kept on your system.\e[0m" + [ -d /etc/conf.d/minecraft ] && echo -e "\e[34;1m==>\e[39;1m NOTE: Your configuration file /etc/conf.d/minecraft was kept on your system.\e[0m" } diff --git a/minecraftctl b/minecraftctl deleted file mode 100644 index 55ed49c..0000000 --- a/minecraftctl +++ /dev/null @@ -1,124 +0,0 @@ -#!/bin/bash - - -# Adapted from generic minecraft init script -# http://www.minecraftwiki.net/wiki/Tutorials/Server_startup_script -# -# Main Differences: -# - Start stop and reload is handled by systemd, removed -# - update handled by pacman, removed -# - changed from init script to "minecraftctl" command. -# - uses /etc/conf.d/minecraftd for variables -# -# Changes: -# -# Changed running screen to work as root, broke it out into mc_docommand -# Commented echo commands for successful execution to not spam the log. - - -. /etc/conf.d/minecraft || echo "can't source /etc/conf.d/minecraft" - - -ME=`whoami` -as_user() { - if [ $ME == $MCUSER ] ; then - bash -c "$1" - else - su - $MCUSER -c "$1" - fi -} - -mc_saveoff() { - if pgrep -u $MCUSER -f $SERVICE > /dev/null - then - #echo "$SERVICE is running... suspending saves" - mc_docommand "say $BACKUPSTARTMSG" - mc_docommand save-off - mc_docommand save-all - sync - sleep 10 - else - echo "$SERVICE is not running. Not suspending saves." - fi -} - -mc_saveon() { - if pgrep -u $MCUSER -f $SERVICE > /dev/null - then - #echo "$SERVICE is running... re-enabling saves" - mc_docommand save-on - mc_docommand "say $BACKUPENDMSG" - else - echo "$SERVICE is not running. Not resuming saves." - fi -} - -mc_backup() { - mc_saveoff - - NOW=`date "+%Y-%m-%d_%Hh%M"` - BACKUP_FILE="$BACKUPPATH/${WORLD}_${NOW}.tar" - #echo "Backing up minecraft world..." - #as_user "cd $_SRVDIR && cp -r $WORLD $BACKUPPATH/${WORLD}_`date "+%Y.%m.%d_%H.%M"`" - as_user "tar -h -C \"$_SRVDIR\" -cf \"$BACKUP_FILE\" \"$WORLD\"" - - #echo "Backing up $SERVICE" - as_user "tar -h -C \"$_SRVDIR\" -rf \"$BACKUP_FILE\" \"$SERVICE\"" - #as_user "cp \"$_SRVDIR/$SERVICE\" \"$BACKUPPATH/minecraft_server_${NOW}.jar\"" - - mc_saveon - - #echo "Compressing backup..." - as_user "gzip -f \"$BACKUP_FILE\"" - #echo "Done." -} - -mc_command() { - command="$1"; - if pgrep -u $MCUSER -f $SERVICE > /dev/null - then - pre_log_len=`wc -l "$_SRVDIR/server.log" | awk '{print $1}'` - #echo "$SERVICE is running... executing command" - mc_docommand "$command" - sleep .1 # assumes that the command will run and print to the log file in less than .1 seconds - # print output - tail -n $[`wc -l "$_SRVDIR/server.log" | awk '{print $1}'`-$pre_log_len] "$_SRVDIR/server.log" - else - echo "$SERVICE is not running. Couldn't executed command." - fi -} - -mc_docommand() { - screen -p 0 -S minecraft -X stuff "`printf \"$1\r\"`" -} - - -#Start-Stop here -case "$1" in - backup) - mc_backup - ;; - status) - if pgrep -u $MCUSER -f $SERVICE > /dev/null - then - echo "$SERVICE is running." - else - echo "$SERVICE is not running." - fi - ;; - command) - if [ $# -gt 1 ]; then - shift - mc_command "$*" - else - echo "Must specify server command (try 'help'?)" - fi - ;; - - *) - echo "Usage: $0 {backup|status|command \"server command\"}" - exit 1 - ;; -esac - -exit 0 diff --git a/minecraftd b/minecraftd deleted file mode 100644 index 997f928..0000000 --- a/minecraftd +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -. /etc/conf.d/minecraft || echo "can't source /etc/conf.d/minecraft" - -cd $_SRVDIR -screen -dmS minecraft java -Xmx${MAXHEAP} -Xms${MINHEAP} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=${THREADS} -XX:+AggressiveOpts -jar ${_SRVDIR}/${SERVERJAR} nogui diff --git a/minecraftd-backup.service b/minecraftd-backup.service new file mode 100644 index 0000000..ff2085e --- /dev/null +++ b/minecraftd-backup.service @@ -0,0 +1,10 @@ +[Unit] +Description=Minecraft Server World Backup +After=local-fs.target + +[Service] +Type=forking +ExecStart=/usr/bin/minecraftd backup + +[Install] +WantedBy=multi-user.target diff --git a/minecraftd-backup.timer b/minecraftd-backup.timer new file mode 100644 index 0000000..0b4414f --- /dev/null +++ b/minecraftd-backup.timer @@ -0,0 +1,11 @@ +[Unit] +Description=Daily Minecraft Server Backup + +[Timer] +Type=forking +OnCalendar=daily +AccuracySec=5min +Persistent=true + +[Install] +WantedBy=multi-user.target diff --git a/minecraftd-diag b/minecraftd-diag deleted file mode 100644 index a0bd045..0000000 --- a/minecraftd-diag +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -. /etc/conf.d/minecraft || echo "can't source /etc/conf.d/minecraft" - -cd $_SRVDIR -echo "If the next command executes correctly, then something is wrong with screen." -su minecraft -c "PATH=/opt/java/bin/:/usr/bin/:$PATH java -Xmx${MAXHEAP} -Xms${MINHEAP} -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=${THREADS} -XX:+AggressiveOpts -jar ${_SRVDIR}/${SERVERJAR} nogui" diff --git a/minecraftd.conf b/minecraftd.conf new file mode 100644 index 0000000..9d03a4a --- /dev/null +++ b/minecraftd.conf @@ -0,0 +1,17 @@ +# THIS IS THE CONFIGURATION FILE FOR THE SCRIPT NOT FOR THE ACTUAL SERVER + +# General parameters +SERVER_ROOT="/srv/minecraft" +BACKUPPATH="/srv/minecraft/backup" +LOGPATH="/srv/minecraft/logs" +WORLDPATHS="/srv/minecraft/world" # World paths separated by spaces +KEEP_BACKUPS="10" +MC_USER="minecraft" +MAIN_JAR="minecraft_server.jar" +SESSION_NAME="minecraft" + +# System parameters for the minecraft server +MINHEAP="512M" +MAXHEAP="1024M" +THREADS="1" +JAVA_PARMS="-Xmx${MAXHEAP} -Xms${MINHEAP} -XX:ParallelGCThreads=${THREADS}" diff --git a/minecraftd.service b/minecraftd.service index d649d63..17e18af 100644 --- a/minecraftd.service +++ b/minecraftd.service @@ -1,14 +1,11 @@ [Unit] -Description=Minecraft server daemon -After=network.target +Description=Minecraft Server +After=local-fs.target network.target [Service] -User=minecraft -Group=minecraft -ExecStart=/usr/bin/minecraftd -Type=oneshot -RemainAfterExit=yes -ExecStop=/usr/bin/screen -S minecraft -X stuff 'say Server Shutting Down in 5 Seconds\n' ; /usr/bin/sleep 5 ; /usr/bin/screen -S minecraft -X stuff 'stop\n' ; /usr/bin/expect -c "exec screen -x minecraft ; wait ; exit" +Type=forking +ExecStart=/usr/bin/minecraftd start +ExecStop=/usr/bin/minecraftd stop [Install] WantedBy=multi-user.target diff --git a/minecraftd.sh b/minecraftd.sh new file mode 100644 index 0000000..6a8e6b2 --- /dev/null +++ b/minecraftd.sh @@ -0,0 +1,205 @@ +#!/bin/bash + +source /etc/conf.d/minecraft || echo "Could not source /etc/conf.d/minecraft" + +# 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}" +WORLDPATHS="${WORLDPATHS:-/srv/minecraft/world}" +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}"}" + +# Check for sudo rigths +if [ $(sudo whoami) != "root" ]; then + echo "You must have sudo access in order to use this script." + exit 1 +fi + +# Pipe any given argument to the minecraft server console +mc_command() { + sudo -u ${MC_USER} screen -S ${SESSION_NAME} -X stuff "`printf \"$*\r\"`" +} + +# Start the server if it is not already running +server_start() { + sudo -u ${MC_USER} screen -S ${SESSION_NAME} -Q select . > /dev/null + if [ $? -eq 0 ]; then + echo "A screen ${SESSION_NAME} session is already running. Please close it first." + else + echo -en "Starting server... " + sudo -u ${MC_USER} screen -dmS ${SESSION_NAME} /bin/bash -c "cd ${SERVER_ROOT}; java ${JAVA_PARMS} -jar ${SERVER_ROOT}/${MAIN_JAR} nogui" + echo -e "\e[39;1m done\e[0m" + fi +} + +# Stop the server gracefully by saving everything prior and warning the users +server_stop() { + sudo -u ${MC_USER} screen -S ${SESSION_NAME} -Q select . > /dev/null + if [ $? -eq 0 ]; then + 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... " + for i in $(seq 1 10); + do + 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 +} + +# Print whether the server is running and if so give some information about memory usage and threads +server_status() { + sudo -u ${MC_USER} screen -S ${SESSION_NAME} -Q select . > /dev/null + if [ $? -eq 0 ]; then + echo -e "Status:\e[39;1m running\e[0m" + + # Calculating memory usage + for p in $(sudo -u ${MC_USER} pgrep -f ${MAIN_JAR}); do + ps -p$p -O rss | tail -n1; + 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 +} + +# Restart the complete server by shutting it down and starting it again +server_restart() { + sudo -u ${MC_USER} screen -S ${SESSION_NAME} -Q select . > /dev/null + if [ $? -eq 0 ]; then + server_stop + sleep 0.1 + server_start + else + server_start + fi +} + +# Backup the directories specified in $WORLDPATHS +backup_files() { + which tar &> /dev/null + if [ $? -ne 0 ]; then + echo "The tar binaries are needed for a backup." + exit 2 + fi + + echo "Starting backup..." + FILE="`date +%Y%m%d%H%M%S`.tar.gz" + sudo -u ${MC_USER} mkdir -p ${BACKUPPATH} + sudo -u ${MC_USER} screen -S ${SESSION_NAME} -Q select . > /dev/null + if [ $? -eq 0 ]; then + mc_command save-off + mc_command save-all + sync && wait + sudo -u ${MC_USER} tar -czf ${BACKUPPATH}/${FILE} --totals ${WORLDPATHS} 2>&1 | grep -v "tar: Removing leading " + mc_command save-on + else + sudo -u ${MC_USER} tar -czf ${BACKUPPATH}/${FILE} --totals ${WORLDPATHS} 2>&1 | grep -v "tar: Removing leading " + fi + echo -e "\e[39;1mbackup completed\e[0m\n" + + echo -n "Only keeping the last ${KEEP_BACKUPS} backups and removing the other ones..." + BACKUP_COUNT=$(for f in ${BACKUPPATH}/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*; do echo $f; done | wc -l) + if [ $(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS}) -gt 0 ]; then + sudo -u ${MC_USER} rm $(for f in ${BACKUPPATH}/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*; do echo $f; done | head -n$(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS})) + 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 +} + +# Run the given comman at the minecraft server console +server_command() { + if [ $# -lt 1 ]; then + echo "No server command specified. Try 'help' for a list of commands." + exit 1 + fi + + sudo -u ${MC_USER} screen -S ${SESSION_NAME} -Q select . > /dev/null + if [ $? -eq 0 ]; then + mc_command "$@" + tailf -n 0 ${LOGPATH}/latest.log & + TAILFPID=$! # Process id of tailf command + sleep 0.1 + kill ${TAILFPID} + else + echo "There is no ${SESSION_NAME} session to connect to." + fi +} + +# Enter the screen minecraft session +server_console() { + sudo -u ${MC_USER} screen -S ${SESSION_NAME} -Q select . > /dev/null + if [ $? -eq 0 ]; then + sudo -u ${MC_USER} screen -S ${SESSION_NAME} -rx + else + echo "There is no ${SESSION_NAME} session to connect to." + fi +} + +# Help function, no arguments required +help() { + cat << 'EOF' +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. + +Usage: minecraftd {start|stop|status|backup|command |console} + 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 + command Run the given comman at the minecraft server console + console Enter the server console through a screen session + +Copyright (c) Gordian Edenhofer +EOF +} + +case "$1" in + start) + server_start + ;; + + stop) + server_stop + ;; + + status) + server_status + ;; + + restart) + server_restart + ;; + + console) + server_console + ;; + + command) + server_command "${@:2}" + ;; + + backup) + backup_files + ;; + + *|-h|--help) + help +esac + +exit 0