..

Minecraft Server

Hosting a Minecraft Server

I am a fan of Debian, so that is the base of the server.

Fabric loader

I was following the official fabric documentation:

  • Install curl
  • Install java 17 apt install openjdk-17-jre-headless
  • Choose version you want…
  • Download curl -OJ https://meta.fabricmc.net/v2/versions/loader/1.20.4/0.15.7/1.0.0/server/jar
  • run the jar once, so that all necessary files and folders get generated
  • change eula.txt, server.properties
  • copy fabric API jar and mods in place

Make it a service

With screen you can start and interact with the server. For more information see here.

Service File

[Unit]
Description=Minecraft Server: %i
After=network.target

[Service]
WorkingDirectory=/opt/%i

User=minecraft
Group=minecraft

Restart=always

ExecStart=/usr/bin/screen -DmS mc-%i /usr/bin/java -Xmx6G -jar fabric-server-mc.1.20.4-loader.0.15.7-launcher.1.0.0.jar nogui

ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN IN 20 SECONDS."\015'
ExecStop=/bin/sleep 1
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "save-all"\015'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say  SAVED EVERYTHING, STOP IN 10 SECONDS..."\015'
ExecStop=/bin/sleep 10
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "stop"\015'


[Install]
WantedBy=multi-user.target

Enable the service: systemctl enable minecraft@hermitcraft. Start the service: systemctl stop minecraft@hermitcraft.

See defined WorkingDirectory in service unit. in this working directory i have servers in folder like hermitcraft. Like that i have to write only one service file.

Make sure everything gets saved

One could propably achieve this via systemd as well, but I found it easier like that…

Place a shutdown script in /etc/init.d/ which takes care of saving the world before a shutdown or a reboot. It only needs to stop the service and give it some time to do its job.

#! /bin/sh

systemctl stop minecraft@hermitcraft

# Wait for the service to be stopped
while systemctl is-active --quiet "$service_name"; do
     sleep 5
     done

exit 0

Create soft links to the proper runlevel (for shutdown and reboot, 0 and 6); /etc/rc0.d/, /etc/rc6.d/

Backup

A minecraft server by default saves all maps to disc every 5 minutes. See answer from some AI:

In Minecraft, the save-all command is used to manually trigger the server to save the current state of the world to disk. However, by default, Minecraft servers also have an auto-save feature enabled, which automatically saves the world at regular intervals. The frequency of these auto-saves can be configured in the server settings. By default, Minecraft servers save the world every 6000 ticks, which is approximately every 5 minutes. This auto-save feature helps prevent data loss in case of unexpected server crashes or shutdowns. However, server administrators may still use the save-all command manually, especially before making significant changes to the world or performing maintenance tasks.

This means that i can take zfs snapshots of the entire lxc and handle it with the hypervisor.