Courtesy of/inspired by linuxize
$ sudo apt update
$ sudo apt install git build-essential openjdk-21-jre-headless$ sudo useradd --system --user-group --home-dir /opt/minecraft --shell /bin/bash minecraft
$ sudo su - minecraft
$ mkdir ~/{backups,tools,server}$ cd ~/tools && git clone https://github.com/Tiiffi/mcrcon.git
$ cd mcrcon && gcc -std=gnu11 -pedantic -Wall -Wextra -O2 -s -o mcrcon mcrcon.c$ cd ~/server && wget https://piston-data.mojang.com/v1/objects/3872a7f07a1a595e651aef8b058dfc2bb3772f46/server.jar$ cd server
$ java -Xmx1024M -Xms512M -jar server.jar noguiThis will complain about the eula, so halt the server and accept it in eula.txt
$ vi server/eula.txt
Enter the following and save the file:
eula=trueNext, edit the server.properties with vi server/server.properties:
rcon.port=25575
rcon.password=SomeStrongPasswordDontUseThis
enable-rcon=trueExit minecraft user back to sudo user
$ exitWrite the minecraft service unit file /etc/systemd/system/minecraft.service:
[Unit]
Description=Minecraft Server
After=network.target
[Service]
User=minecraft
Nice=1
KillMode=none
SuccessExitStatus=0 1
ProtectHome=true
ProtectSystem=full
PrivateDevices=true
NoNewPrivileges=true
WorkingDirectory=/opt/minecraft/server
ExecStart=/usr/bin/java -Xmx1024M -Xms512M -jar server.jar nogui
ExecStop=/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p SomeStrongPasswordDontUseThis stop
[Install]
WantedBy=multi-user.targetReload the system services and run the new minecraft service
$ sudo systemctl daemon-reload
$ sudo systemctl enable --now minecraftAdjust the firewall if necessary
$ sudo ufw allow 25565/tcpSwitch to minecraft user (as before) and add the following to ~/tools/backup.sh:
#!/bin/bash
function rcon {
/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p SomeStrongPasswordDontUseThis "$1"
}
rcon "save-off"
rcon "save-all"
tar -cvpzf /opt/minecraft/backups/server-$(date +%F-%H-%M).tar.gz /opt/minecraft/server
rcon "save-on"
## Delete older backups
find /opt/minecraft/backups/ -type f -mtime +7 -name '*.gz' -delete Mark as executable with chmod +x ~/tools/backup.sh, then call crontab -e
and add the following:
0 23 * * * /opt/minecraft/tools/backup.shCall mcrcon with the -t flag to access the terminal
$ /opt/minecraft/tools/mcrcon/mcrcon -H localhost -P 25575 -p SomeStrongPasswordDontUseThis -t