Skip to content

Instantly share code, notes, and snippets.

@Skinner927
Last active April 8, 2024 04:22
Show Gist options
  • Select an option

  • Save Skinner927/f2cd4c9cd3b57502ce32297e83a69a06 to your computer and use it in GitHub Desktop.

Select an option

Save Skinner927/f2cd4c9cd3b57502ce32297e83a69a06 to your computer and use it in GitHub Desktop.
Minecraft Server Setup

Minecraft Server Setup

This is for Ubuntu 18.04 / Systemd

Install deps

sudo apt-get install -y screen wget rdiff-backup openjdk-11-jre-headless

Create a minecraft service user

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

Switch to the minecraft user and switch to their home dir

sudo su minecraft

cd

Make the minecraft server dir in minecraft's home dir and cd into it

mkdir minecraft_server && cd $_

Download the minecraft server jar from https://minecraft.net/en-us/download/server/

wget https://launcher.mojang.com/v1/objects/ThisLinkIsOutdated/server.jar

Accept the eula by running echo 'eula=true' > eula.txt

Copy all the service_* scripts into the minecraft_server dir and chmod u+x service_*.

Copy the do_backups.sh to one directory up, the minecraft user's home dir. chmod it too: chmod u+x do_backups.sh.

And then create a cron job to run backups as frequently as you'd like, I'd suggest every hour:

crontab -e

0 * * * * /home/minecraft/do_backups.sh

exit the minecraft user and back to your regular user.

Install the minecraft_screen.sh script to your home dir and chmod u+x minecraft_screen.sh.

Install the service /etc/systemd/system/minecraft.service.

Reload for the new service: sudo systemctl daemon-reload

Start it sudo systemctl start minecraft.

Ensure it's running sudo systemctl status minecraft.

Set it to start on boot sudo systemctl enable minecraft.

You're done.

#!/bin/bash
if [ "$(whoami)" != "minecraft" ]; then
echo "You must run this script as minecraft user"
exit
fi
cd /home/minecraft || exit 1
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"
[Unit]
Description=Minecraft Server
After=network-online.target
[Service]
Type=simple
Restart=always
RestartSec=1
User=minecraft
WorkingDirectory=/home/minecraft/minecraft_server
ExecStart=/home/minecraft/minecraft_server/service_start.sh
ExecStop=/home/minecraft/minecraft_server/service_stop.sh
[Install]
WantedBy=multi-user.target
#!/bin/bash
if [[ "$(whoami)" != 'minecraft' ]]; then
sudo su minecraft -c "script -q -c 'screen -r minecraft' /dev/null"
else
screen -r minecraft
fi
#!/bin/bash
# This file is used as an example basically to remember
# about the locking mechanism of Przemyslaw's script.
# SPDX-License-Identifier: MIT
## Copyright (C) 2009 Przemyslaw Pawelczyk <[email protected]>
## https://gist.github.com/przemoc/571091
## This script is licensed under the terms of the MIT license.
## https://opensource.org/licenses/MIT
#
# Lockable script boilerplate
echo 'starting'
### HEADER ###
LOCKFILE="/var/lock/mapcrafter.run.lock"
LOCKFD=99
# PRIVATE
_lock() { flock -$1 $LOCKFD; }
_no_more_locking() { _lock u; _lock xn && rm -f $LOCKFILE; }
_prepare_locking() { eval "exec $LOCKFD>\"$LOCKFILE\""; trap _no_more_locking EXIT; }
# ON START
_prepare_locking
# PUBLIC
exlock_now() { _lock xn; } # obtain an exclusive lock immediately or fail
exlock() { _lock x; } # obtain an exclusive lock
shlock() { _lock s; } # obtain a shared lock
unlock() { _lock u; } # drop a lock
### BEGIN OF SCRIPT ###
# Simplest example is avoiding running multiple instances of script.
exlock_now || exit 1
# Remember! Lock file is removed when one of the scripts exits and it is
# the only script holding the lock or lock is not acquired at all.
cd /home/minecraft
echo 'making map'
mapcrafter -c mapcrafter.conf 2>&1 | tee last_render.txt
#!/bin/bash
if [ "$(whoami)" != "minecraft" ]; then
echo "You must run this script as minecraft user"
exit
fi
screen -DmS minecraft java -Xms1024M -Xmx6144M -jar server.jar nogui
#!/bin/bash
screen -dr minecraft -X stuff "stop$(echo -ne '\r')"
screen -dr minecraft
@lucacastelnuovo
Copy link

Thanks for these scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment