Skip to content

Instantly share code, notes, and snippets.

@Skinner927
Last active December 29, 2015 19:07
Show Gist options
  • Save Skinner927/4a044bb1f08ceeb24950 to your computer and use it in GitHub Desktop.
Save Skinner927/4a044bb1f08ceeb24950 to your computer and use it in GitHub Desktop.
Minecraft service and helper scripts for running a vanilla Minecraft server. Greatly inspired/copied by: http://wellsie.net/p/349/

#Minecraft Server Service

Minecraft service and helper scripts for running a vanilla Minecraft server. Greatly inspired/copied by: http://wellsie.net/p/349/


Create a minecraft service user

sudo adduser --system --group --shell /bin/bash minecraft

Switch to the new minecraft user

sudo su minecraft

Drop run_bakup.sh into /home/minecraft/ and chmod u+x /home/minecraft/run_backup.sh

Make the backup dir

mkdir backups

Install the backup cron crontab -e

@hourly /home/minecraft/run_backup.sh > /home/minecraft/last_backup_log.txt

Make the minecraft server dir and cd into it

mkdir minecraft_server && cd $_

Download the minecraft server jar. All scripts in this gist expect the jar to be labled minecraft_server.1.8.9.jar. Should prob symlink this later, but whatever, pull the jar down. wget https://s3.amazonaws.com/Minecraft.Download/versions/1.8.9/minecraft_server.1.8.9.jar

If you want your current user to be able to play with server files, run the next 3 commands

  1. Make everything easier and let the group play with everything

cd .. && chown -R minecraft:minecraft . && chmod -R g=u .

  1. logout of the minecraft user

exit

  1. If you want to make life easier, add yourself to the minecraft group

sudo usermod -a -G minecraft $(whoami)

else

logout of the minecraft user

exit

put minecraft_shell.sh into your home dir. Make it executable chmod u+x ~/minecraft_shell.sh

When you run ./minecraft_shell.sh you must do it as sudo. Once youre in the screen, press Ctrl+a d to detatch/exit.

put minecraft.conf in /etc/init/ and set it to executable chmod u+x /etc/init/minecraft.conf

Start the server

sudo service minecraft start

TODO: could probably make this an auto-install script.

#upstart job for minecraft server
start on (local-filesystems and net-device-up IFACE!=lo)
stop on runlevel [016]
chdir /home/minecraft/minecraft_server
# we did have -Xincgc but I think that's super aggresive gc and we don't need that
script
echo $$ > /var/run/minecraft.pid
exec su minecraft -c "screen -DmS minecraft java -Xms1024M -Xmx3072M -jar minecraft_server.1.8.9.jar nogui"
end script
pre-start script
echo >> server.log
cat server.log >> server.archive.log
rm server.log
end script
pre-stop script
# this carriage return prob wont copy correctly. Immediately after 'stop press ctrl+v then [enter]. This will insert a return char.
su minecraft -c "screen -d -r minecraft -X stuff 'stop
'"
sleep 10
end script
post-stop script
rm /var/run/minecraft.pid
end script
#!/bin/bash
screen -r minecraft/minecraft
#!/bin/bash
if [ "$(whoami)" != "minecraft" ]; then
echo "You must run this script as minecraft user"
exit
fi
cd /home/minecraft
toscreen () {
screen -d -r minecraft -X stuff "$1$(echo -ne '\r')"
}
echo "Starting backup procedure: $(date)"
toscreen "say ** Starting Backup! Brace for lag."
# remove old backups
echo "Removing old backups"
rdiff-backup --remove-older-than 7D --force backups
# turn off saving so backup is consistent
toscreen "save-off"
# do the backup
echo "Doing the backup"
rdiff-backup minecraft_server backups
# turn saving back on
toscreen "save-on"
toscreen "say ** Backup done!"
echo "Backup complete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment