From b94e3db3db0e96c489bc4a40fc7c8c75246cb56e Mon Sep 17 00:00:00 2001 From: Gordian Edenhofer Date: Sat, 23 Jan 2016 12:45:46 +0100 Subject: [PATCH] Upgpkg: 1.8.9-2 Restore backups easily with the delivered bash script Human readable naming schema for backups Backed up directories are now relative Group has write permission (g+ws) to SERVER_ROOT Proper quotation of variables which might contain spaces --- .SRCINFO | 8 ++- PKGBUILD | 10 +++- minecraft-server.install | 17 +++--- minecraftd.conf | 2 +- minecraftd.sh | 118 ++++++++++++++++++++++++++++++++------- 5 files changed, 119 insertions(+), 36 deletions(-) diff --git a/.SRCINFO b/.SRCINFO index ab2736b..b7846b8 100644 --- a/.SRCINFO +++ b/.SRCINFO @@ -1,7 +1,9 @@ +# Generated by mksrcinfo v8 +# Sat Jan 23 11:45:46 UTC 2016 pkgbase = minecraft-server pkgdesc = Minecraft server unit files, script, and jar pkgver = 1.8.9 - pkgrel = 1 + pkgrel = 2 url = http://minecraft.net/ install = minecraft-server.install arch = any @@ -26,8 +28,8 @@ pkgbase = minecraft-server md5sums = 2cf6cdf65e0ed6aa6d452943b1e84357 md5sums = c644abdf293c2f98033a64d732a4945c md5sums = 5ed78e366146e47f8498347e93ad5423 - md5sums = d4656f27716fd78ab64344a517b07443 - md5sums = 2a54c072bb414eb6a14867efabfc163a + md5sums = 8088e2b05ef4d85049dea41585a9070d + md5sums = 36962e5da95c9f8a2beb910f312f96c6 pkgname = minecraft-server diff --git a/PKGBUILD b/PKGBUILD index 2d12091..536a69a 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -3,7 +3,7 @@ pkgname=minecraft-server pkgver=1.8.9 -pkgrel=1 +pkgrel=2 pkgdesc="Minecraft server unit files, script, and jar" arch=('any') url="http://minecraft.net/" @@ -25,8 +25,8 @@ md5sums=('3acbaef956308c805e8e2d0a03a737e9' '2cf6cdf65e0ed6aa6d452943b1e84357' 'c644abdf293c2f98033a64d732a4945c' '5ed78e366146e47f8498347e93ad5423' - 'd4656f27716fd78ab64344a517b07443' - '2a54c072bb414eb6a14867efabfc163a') + '8088e2b05ef4d85049dea41585a9070d' + '36962e5da95c9f8a2beb910f312f96c6') package() { install -Dm644 minecraftd.conf "${pkgdir}/etc/conf.d/minecraft" @@ -37,6 +37,10 @@ package() { 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" + # Link the log files mkdir -p "${pkgdir}/var/log/" ln -s "/srv/minecraft/logs" "${pkgdir}/var/log/minecraft" #&>/dev/null + + # Give the group write permissions and set user or group ID on execution + chmod g+ws "${pkgdir}/srv/minecraft" } diff --git a/minecraft-server.install b/minecraft-server.install index 5b528c8..fc4635c 100644 --- a/minecraft-server.install +++ b/minecraft-server.install @@ -1,7 +1,7 @@ USER="minecraft" SERVER_ROOT="/srv/minecraft" -_update() { +post_install() { getent group "${USER}" &>/dev/null if [ $? -ne 0 ]; then echo -e "\e[34;1m==>\e[39;1m Adding ${USER} system group... \e[0m" @@ -24,12 +24,13 @@ _update() { echo -e "\e[34;1m==>\e[39;1m which is generated after the first server start. \e[0m" } -post_install() { - _update -} - post_upgrade() { - _update + chown -R ${USER}:${USER} "${SERVER_ROOT}" + + if [[ -f /etc/conf.d/minecraft.pacnew ]]; then + echo -e "\e[34;1m==>\e[39;1m NOTE: There was a new change on how backup files are handled! Please merge the new \e[0m" + echo -e "\e[34;1m==>\e[39;1m configuration file into /etc/conf.d/minecraft by e.g. pacdiff. \e[0m" + fi } pre_remove() { @@ -50,6 +51,6 @@ post_remove() { 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" + [[ -d "${SERVER_ROOT}" ]] && chown -R 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" + [[ -f "/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/minecraftd.conf b/minecraftd.conf index 9d03a4a..8983517 100644 --- a/minecraftd.conf +++ b/minecraftd.conf @@ -4,7 +4,7 @@ SERVER_ROOT="/srv/minecraft" BACKUPPATH="/srv/minecraft/backup" LOGPATH="/srv/minecraft/logs" -WORLDPATHS="/srv/minecraft/world" # World paths separated by spaces +WORLDPATHS="world" # World paths separated by spaces relative to SERVER_ROOT KEEP_BACKUPS="10" MC_USER="minecraft" MAIN_JAR="minecraft_server.jar" diff --git a/minecraftd.sh b/minecraftd.sh index 5083472..212accd 100755 --- a/minecraftd.sh +++ b/minecraftd.sh @@ -6,7 +6,7 @@ source /etc/conf.d/minecraft || echo "Could not source /etc/conf.d/minecraft" SERVER_ROOT="${SERVER_ROOT:-/srv/minecraft}" BACKUPPATH="${BACKUPPATH:-/srv/minecraft/backup}" LOGPATH="${LOGPATH:-/srv/minecraft/logs}" -WORLDPATHS="${WORLDPATHS:-/srv/minecraft/world}" +WORLDPATHS="${WORLDPATHS:-world}" KEEP_BACKUPS="${KEEP_BACKUPS:-10}" MC_USER="${MC_USER:-minecraft}" MAIN_JAR="${MAIN_JAR:-minecraft_server.jar}" @@ -26,30 +26,29 @@ fi # Pipe any given argument to the minecraft server console mc_command() { - sudo -u ${MC_USER} screen -S ${SESSION_NAME} -X stuff "`printf \"$*\r\"`" + 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 + 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" + 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 + 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 + for i in $(seq 1 10); do mc_command say "down in... $(expr 10 - $i)" echo -n " $(expr 10 - $i)" sleep 1 @@ -63,12 +62,12 @@ server_stop() { # 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 + 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 + 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 @@ -78,7 +77,7 @@ server_status() { # 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 + sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -Q select . > /dev/null if [ $? -eq 0 ]; then server_stop sleep 0.1 @@ -90,6 +89,7 @@ server_restart() { # Backup the directories specified in $WORLDPATHS backup_files() { + # Check for the availability of the tar binaries which tar &> /dev/null if [ $? -ne 0 ]; then echo "The tar binaries are needed for a backup." @@ -97,30 +97,101 @@ backup_files() { 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 + 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 " + sudo -u "${MC_USER}" tar -C "${SERVER_ROOT}" -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 " + sudo -u "${MC_USER}" tar -C "${SERVER_ROOT}" -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) + BACKUP_COUNT=$(for f in "${BACKUPPATH}"/[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})) + sudo -u "${MC_USER}" rm $(for f in "${BACKUPPATH}"/[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 } +# Restore backup +backup_restore() { + # Check for the availability of the tar binaries + which tar &> /dev/null + if [ $? -ne 0 ]; then + 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 + sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -Q select . > /dev/null + if [ $? -eq 0 ]; then + 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 + if [ $# -lt 1 ]; then + 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 + read USER_CHOICE + + # Interpeting the input + if [[ $USER_CHOICE =~ ^-?[0-9]+$ ]]; then + n=1 + for f in "${BACKUPPATH}"/[0-9_.]*; do + [[ $n -eq $USER_CHOICE ]] && FILE="$f" + 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 + if [ -f "$1" ]; then + FILE="$1" + else + if [ -f "${BACKUPPATH}"/"$1" ]; then + 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..." + sudo -u "${MC_USER}" tar -xf "${FILE}" -C "${SERVER_ROOT}" 2>&1 + if [[ $? -eq 0 ]]; then + echo -e "\e[39;1mRestoration completed\e[0m" + else + echo -e "\e[39;1mFailed to restore backup.\e[0m" + fi +} + # Run the given comman at the minecraft server console server_command() { if [ $# -lt 1 ]; then @@ -128,12 +199,12 @@ server_command() { exit 1 fi - sudo -u ${MC_USER} screen -S ${SESSION_NAME} -Q select . > /dev/null + sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -Q select . > /dev/null if [ $? -eq 0 ]; then sleep 0.1s & SLEEP_PID=$! mc_command "$@" & - tail -f --pid=${SLEEP_PID} -n 0 ${LOGPATH}/latest.log + tail -f --pid=${SLEEP_PID} -n 0 "${LOGPATH}/latest.log" else echo "There is no ${SESSION_NAME} session to connect to." fi @@ -141,9 +212,9 @@ server_command() { # Enter the screen minecraft session server_console() { - sudo -u ${MC_USER} screen -S ${SESSION_NAME} -Q select . > /dev/null + 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 + sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -rx else echo "There is no ${SESSION_NAME} session to connect to." fi @@ -161,6 +232,7 @@ help() { restart Restart the minecraft server status Print some status information backup Backup the world data + restore [filename] Restore the world data from a backup command Run the given comman at the minecraft server console console Enter the server console through a screen session @@ -197,6 +269,10 @@ case "$1" in backup_files ;; + restore) + backup_restore "${@:2}" + ;; + *|-h|--help) help esac