From 80cc9eff57f758fefea6d30dacb8f502ff31c3ec Mon Sep 17 00:00:00 2001 From: Leander Hutton Date: Fri, 23 Sep 2016 16:56:05 -0400 Subject: [PATCH] Moved from github, lost history. Starting over. --- CHANGES | 12 ++ README | 84 ++++++++++++ minecraft-server | 333 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 429 insertions(+) create mode 100644 CHANGES create mode 100644 README create mode 100755 minecraft-server diff --git a/CHANGES b/CHANGES new file mode 100644 index 0000000..7212ca2 --- /dev/null +++ b/CHANGES @@ -0,0 +1,12 @@ +0.3 +- added whitelist functionality +- added banning functionality + +0.2 +- remove -t switch when syncing to RAM disk +- added the ability to specifiy how many days worth of backups to keep +- increased the amount of RAM for java to 2048MB +- Removed some more derpy backticks + +0.1 +- Intial release diff --git a/README b/README new file mode 100644 index 0000000..acfed68 --- /dev/null +++ b/README @@ -0,0 +1,84 @@ +Minecraft Server Script v0.3 +Leander Hutton +leander@one-button.org + +INTRO: + +USE THIS SOFTWARE AT YOUR OWN RISK. THIS SCRIPT IS NOT WARRANTIED TO WORK PROPERLY OR AT ALL. WILL NOT ANY ORGANS LARGER. MAY ACCIDENTALLY ALL THE THINGS. + +This script was written to make managing a Minecraft server a little easier. It's been changed/revised and updated over the months and it currently supports running a server from a physical disk or a RAM disk and making regular backups. + +This script is designed to work like a typical init.d script but can be run in any environment you see fit, such as Mac OS X. You only have to have the requirements below. + +REQUIREMENTS: + +bash +JRE +Mojang's official minecraft_serer.jar or CraftBukkit's latest recommended release. +GNU Screen +pgrep +rsync +GNU tar +find +An obsession with breaking blocks +An understanding significant other +Lots of free time + +USAGE: + +minecraft-server {start|restart|save-all|backup|writetodisk|stop|whitelist-add|whitelist-remove|ban|ban-ip|pardon|pardon-ip} + +start: starts the Minecraft server. +restart: shuts down and starts the server. Disconnects any connected clients. +save-all: saves all of the map data to disk. +backup: creates date-stamped gzipped tarball backup of the server into the backup directory. +writetodsik: writes data in RAM disk to disk. Only works if you're using RAM disk. +stop: stops the server. Disconnects any connected clients. +whitelist-add : adds player to whitelist +whitelist-remove : removes player to whitelist +ban : bans player +pardon : pardons player +ban-ip : bans ip address +pardon-ip : pardons ip address + +MAKE SURE YOU EDIT THESE SETTINGS AT THE TOP OF THE SCRIPT: + +MINECRAFT="minecraft_server.jar" # Name of your server jar file. This script supports vanilla and craftbukkit. +MCPATH="/path/to/mc/server/" # Directory on disk of your MC server. Make sure your MINECRAFT jar file is located here. +ARGS="-Xmx1024M -Xms1024M -jar" # Standard arguments for the minecraft server jar. +USER="steve" # User your minecraft server runs under. +BACKUPDIR="/path/to/backups/" # Directory for backup tar files. + +The defaults will not work. Make sure the programs referenced by the script are in the appropriate location. You may need to change those too. The script has been setup for use on Debian out of the box. + +RECOMMENDED CRON TASKS: + +It is recommended that you add a few cron jobs to the user running the Minecraft server. These are for saving the map and doing backups. Here are some example entries for your crontab: + +# Save map to disk every 30 minutes +*/30 * * * * /etc/init.d/minecraft-server save-all $>/dev/null +# Do a complete backup every 6 hours +15 */6 * * * /etc/init.d/minecraft-server backup $>/dev/null + +By default the script will keep 15 days worth of backups and then start removing the oldest ones. You can edit the DAYSOFBACKUPS varibale to keep fewer or more backups. + +RAM DISK: + +If you're using a Linux host you can create a tmpfs out of RAM and mount it like so: + +mkdir /var/ramdisk +mount -t tmpfs none /var/ramdisk -o size=512m + +If you want the tmpfs setup automatically on boot at the following line to /etc/fstab: + +tmpfs /var/ramdisk tmpfs defaults,size=512m 0 0 + +Here the size option is specified in megabytes. Make sure you create a disk with plenty of room for your map to grow. + +WARNING: tmpfs contents are COMPLETELY DESTROYED on shutdown or power loss. PLEASE USE A UPS AND DO SOME SORT OF SYNCING TO DISK! + +To use this script with a RAM disk set the RAMDRIVE variable to true and set RAMDISKDIR to the tmpfs' mount point. You can also use this script to sync the RAM disk to a hard drive. The directory it syncs to is set by the MCPATH variable. Add the following line to your crontab to get the disk sync going every ten minutes: + +*/10 * * * * /etc/init.d/minecraft-server writetodisk $>/dev/null + +If RAMDISK="true" is set the script will load up the files into the tmpfs on startup and back out of the tmpfs on shutdown automatically. If it's set to false it will just run from the disk (MCPATH) like normal. diff --git a/minecraft-server b/minecraft-server new file mode 100755 index 0000000..07a9f1b --- /dev/null +++ b/minecraft-server @@ -0,0 +1,333 @@ +#!/bin/bash + +# Minecraft server script +# v0.3 +# leander@one-button.org + +# User editable variables: +# Always include the trailing slashes on directories! +MINECRAFT="minecraft_server.jar" # Name of your server jar file. This script supports vanilla and craftbukkit. +MCPATH="/var/minecraft_directory/" # Directory on disk of your MC server. Make sure your MINECRAFT jar file is located here. +SERVERNAME="minecraft" +ARGS="-Xmx2G -Xms2G -jar" # Standard arguments for the minecraft server jar. +USER="steve" # User your minecraft server runs under. +BACKUPDIR="/var/backup/minecaft_backup/" # Directory for backup tar files. +DAYSOFBACKUPS="+7" # number of days of backups to keep. Default is +7. + +# Various programs this script calls +JAVA="/usr/bin/java" +SCREEN="/usr/bin/screen" +DATE=$(date +%m-%d-%Y-%H:%M) +TAR="/bin/tar" +RSYNC="/usr/bin/rsync" +FIND="/usr/bin/find" + +# RAM Disk settings. +# Don't use this if you don't know what you're doing! +# See the README for me info +RAMDISK=false # Set to true if you want to use a RAM disk, false to run from a hard disk. See README first. +RAMDISKDIR="/lol/ramdisk/" # The path where your RAM disk is mounted. + +already_running(){ + + if pgrep -u $USER -f $MINECRAFT > /dev/null + then + return 1 + else + return 0 + fi +} + +run_as() { + + WHOAMI=$(whoami) + + if [ "$WHOAMI" == "$USER" ] + then + bash -c "$1" + else + su - $USER -c "$1" + fi + +} + +start() { + + already_running + + if [ "$?" == "1" ] + then + echo "Minecraft already running ..." + else + if [ $RAMDISK == "true" ] + then + echo "Copying files to RAM Disk ..." + setup_ramdisk + echo "Starting $MINECRAFT ..." + run_as "cd $RAMDISKDIR && $SCREEN -dm -S $SERVERNAME $JAVA $ARGS $MINECRAFT" + sleep 7 + else + + echo "Starting $MINECRAFT ..." + run_as "cd $MCPATH && $SCREEN -dm -S $SERVERNAME $JAVA $ARGS $MINECRAFT" + sleep 7 + fi + + already_running + if [ "$?" == "1" ] + then + echo "$MINECRAFT started on $SERVERNAME screen session." + else + echo "Error, $MINECRAFT could not be started. Check ${MCPATH}logs/ for errors." + fi + + fi +} + + +stop() { + + already_running + if [ "$?" == "1" ] + then + if [ $RAMDISK == "true" ] + then + + echo "Syncing RAM drive to disk .." + sync_ram_to_disk + echo "Sync complete." + + fi + echo "Stopping $MINECRAFT ... " + run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"say SERVER SHUT DOWN IN 5 SECONDS. SAVING WORLD ...\"$(echo -ne '\015')" + run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-all\015'" + sleep 5 + run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff stop\015'" + sleep 2 + already_running + if [ "$?" == "1" ] + then + echo "$MINECRAFT could not be stopped." + echo "Check ${MCPATH}server.log for errors." + else + echo "$MINECRAFT stopped." + fi + + else + echo "$MINECRAFT is not running" + fi +} + +status() { + + already_running + if [ "$?" == "1" ] + then + echo "$MINECRAFT is running." + else + echo "$MINECRAFT is not running." + fi +} + +save_all() { + + already_running + if [ "$?" == "1" ] + then + echo "Saving all in memory to disk ..." + run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"say SAVING WORLD. PREPARE FOR LAG.\"$(echo -ne '\015')" + run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-all\015'" + sleep 10 + run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"say SAVE COMPLETE.\"$(echo -ne '\015')" + else + echo "$MINECRAFT not running." + + fi + +} + +whitelist() { + + already_running + if [ "$?" == "1" ] + then + if [ "$1" == "add" ] + then + run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"whitelist add $2\"$(echo -ne '\015')" + + elif [ "$1" == "remove" ] + then + run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"whitelist remove $2\"$(echo -ne '\015')" + fi + + else + echo "$MINECRAFT not running." + + fi + + +} + +ban() { + + already_running + if [ "$?" == "1" ] + then + if [ "$1" == "ban" ] + then + run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"ban $2\"$(echo -ne '\015')" + + elif [ "$1" == "pardon" ] + then + run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"pardon $2\"$(echo -ne '\015')" + + elif [ "$1" == "ban-ip" ] + then + run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"ban-ip $2\"$(echo -ne '\015')" + + elif [ "$1" == "pardon-ip" ] + then + run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"pardon-ip $2\"$(echo -ne '\015')" + fi + + else + echo "$MINECRAFT not running." + + fi + + +} + +backup() { + already_running + if [ "$?" == "1" ] + then + if [ "$RAMDISK" == "true" ] + then + echo "Syncing RAM drive to disk .." + sync_ram_to_disk + echo "Sync complete." + fi + + echo "Backing up server ..." + if [ -d $BACKUPDIR ] + then + run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"say BACKING UP. SERVER GOING READ-ONLY.\"$(echo -ne '\015')" + run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-off\015'" + run_as "$TAR czf ${BACKUPDIR}${SERVERNAME}-${DATE}.tar.gz $MCPATH" + run_as "$FIND ${BACKUPDIR} -maxdepth 1 -type f -mtime $DAYSOFBACKUPS -delete" + run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-on\015'" + run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"say BACK UP COMPLETE. SERVER GOING READ-WRITE.\"$(echo -ne '\015')" + + echo "Backup complete." + else + echo "Error, backup directory not found." + fi + else + run_as "$TAR czf ${BACKUPDIR}minecraft-server-${DATE}.tar.gz $MCPATH" + echo "Backup complete." + echo "Cleaning up backups older than $DAYSOFBACKUPS days ..." + run_as "$FIND ${BACKUPDIR} -maxdepth 1 -type f -mtime $DAYSOFBACKUPS -delete" + echo "Old backups cleaned up" + fi + +} + +setup_ramdisk() { + echo "You are setup to run in a RAM disk." + echo "PLEASE see the README about syncing the RAM drive with a hard drive!" + run_as "$RSYNC -rO --delete-after $MCPATH $RAMDISKDIR" +} + +sync_ram_to_disk() { + + if [ -f $RAMDISKDIR/${MINECRAFT} ] + then + + echo "Syncing RAM disk to drive ..." + run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-off\015'" + run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-all\015'" + sleep 2 + run_as "$RSYNC -art --delete-after $RAMDISKDIR $MCPATH" + run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-on\015'" + echo "Sync complete." + + else + echo "Error, $MINECRAFT not in RAM disk." + fi +} + +case $1 in + start) + start + exit 1 + ;; + stop) + stop + exit 1 + ;; + + save-all) + save_all + exit 1 + ;; + + backup) + save_all + backup + exit 1 + ;; + + whitelist-add) + whitelist add $2 + exit 1 + ;; + + whitelist-remove) + whitelist remove $2 + exit 1 + ;; + + ban) + ban ban $2 + exit 1 + ;; + + ban-ip) + ban ban-ip $2 + exit 1 + ;; + + pardon) + ban pardon $2 + exit 1 + ;; + + pardon-ip) + ban pardon-ip $2 + exit 1 + ;; + + status) + status + exit 1 + ;; + + writetodisk) + sync_ram_to_disk + exit 1 + ;; + + restart) + stop + start + exit 1 + ;; + + *) + echo "Usage: minecraft-server {start|restart|save-all|backup|writetodisk|stop|whitelist-add|whitelist-remove|ban|ban-ip|pardon|pardon-ip}" + exit 1 + ;; +esac + +exit 0