Maintaining a Minecraft Server
I have started hosting a minecraft server on my homelab Dell r730xd. It is running inside of an ubuntu virtual machine installed on Proxmox. Several things needed to take place not only for this to just “work” but remain online in a stable deployment that requires little intervention in the future so that the game can be enjoyed matching the experience of a paid hosted platform. There were many key elements that would make this possible. The first being creating a stable hostname that can be used regardless of how frequently my ISP changes my actual ip address. The second was resolving crashes without needing to manually restart services. Also, I needed a way to ensure proper backups would take place without using too much storage in case of game corruption or rolling back mistakes that players want to take back. Finally I wanted to be able to login to the server console from outside my home network just in case something did need to be fixed and I was away from home. Below are troubleshooting and notes to help admin the server for future use.
Server is virtual machine running Ubuntu Server inside of Proxmox
SSH
To connect to the server ssh to user@192.168.1.251
Systemd
Minecraft is set to start automatically upon the start of the vm using systemd services
This service is saved as minecraft.service and can be found in
sudo nano /etc/systemd/system/minecraft.service
The contents of this file are
[Unit]
Description=Minecraft ATM9 Server in Screen
After=network.target
[Service]
User=minecraft
WorkingDirectory=/home/minecraft/gregtech
ExecStart=/usr/bin/screen -DmS minecraft /bin/bash /home/minecraft/gregtech/startserver-java9.sh
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
All services are going to be saved inside of the /etc/systemd/system folder. In order to make them start when the virtual machine starts they must be enabled by systemd by running
sudo systemctl enable filename.service
Screen
Minecraft is now set to open inside of screen using the minecraft user on the vm. Screen helps to run the program in the background so that the minecraft console does not take over the entire terminal stdout for the minecraft user. In order to connect to the screen session and use the minecraft console use
sudo -u minecraft screen -r minecraft
To exit screen use ctl + a then d
To list the screen sessions that are running use
sudo -u minecraft screen -ls
Hostname and Networking
My ip address rotates regularly and I need a stable way to connect players that are outside my home network. To achieve this I am using no-ip.com which gave me a hostname as follows
bungacraft.ddns.net
To configure this in Verizon router login to the router
192.168.1.1/
Go to Advanced>Network Settings>Dynamic DNS
This is where no-ip.com is set to connect to my router and on no-ip.com I have bungacraft.ddns.net set as a hostname that will talk to my router and constantly check and update my new ip address as assigned by the ISP.
The default port for minecraft is being used at 25565. This is configured inside the router under
Advanced>Security and Firewall>Port Forwarding
Backups
To ensure that no progress is lost in the event of game corruption creating a backup script was necessary. I am using Gregtech New Horizons as a modpack and this pack does have a feature inside it already where the OP or the server can run /backup start to save to the default backup folder found in /home/minecraft/gregtech/backup. The challenge was to make a script that can execute this backup command and then schedule that to run hourly. Then the next challenge was to make sure that the backup folder does not continue to grow indefinitely.
Making a backup script in /home/minecraft/run_backup.sh
#!/bin/bash
# Send a message to chat before backup starts
screen -S minecraft -p 0 -X stuff "/say Bungacraft is Gregging up a backup$(printf '\r')"
# Start the actual backup
screen -S minecraft -p 0 -X stuff "/backup start$(printf '\r')"
Making a script to only keep the last 10 saves in /home/minecraft/prune_backups.sh
#!/bin/bash
cd /home/minecraft/gregtech/backups
ls -1tr | head -n -10 | xargs -d '\n' rm -f --
Then to schedule these to run automatically I am using a cronjob
crontab -e
APPEND THE FOLLOWING LINES
0 * * * * /home/minecraft/run_backup.sh
20 * * * * /home/minecraft/prune_backups.sh
This runs the backup script hourly and the prune script 20 minutes past the hour.