Daemonizing and World Backups

After reviewing multiple documents about setting up Minecraft, including the Google Cloud guide, and the A Cloud Guru series I previously mentioned, I tweaked my system to start Minecraft as a service, but still do it via Screen to give me access to the console to let me send commands without having to log into the game, and so the backup script would function.

This is the minecraft.service file used by systemctl, which calls Java with all of Aikar’s flags:

[Unit]
Description=Paper MC - Minecraft Java Edition
After=network.target

[Service]
User=minecraft
Group=minecraft
WorkingDirectory=/home/minecraft


ExecStart=/usr/bin/screen -DmS mcs /usr/bin/java -Xms2G -Xmx6G -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -jar paperclip.jar nogui


ExecStop=/home/minecraft/mc-backup.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

This is the Bash script that I use to back up the World and plugin folders to Google Cloud storage. I’ve modified it slightly with PaperMC running, as it doesn’t recognize the /commands in the console via Screen. Also, PaperMC stores the World files in separate folders for the Overworld, Nether, and The End, so these have to be collected into the compressed file separately.

This script assumes you are running Minecraft in a screen window, started as “mcs”, so you can send commands to the console.

#!/bin/bash
FILENAME=$(date "+%Y%m%d-%H%M%S")-world.tar.gz 

screen -r mcs -X stuff 'save-all flush\nsave-off\n'
tar -zcvf /home/minecraft/$FILENAME /home/minecraft/world /home/minecraft/world_nether /home/minecraft/world_the_end /home/minecraft/plugins
/usr/bin/gsutil cp /home/minecraft/$FILENAME gs://path-to-minecraft-backup-bucket/$FILENAME 
screen -r mcs -X stuff 'save-on\n'
rm /home/minecraft/$FILENAME

The script will:

  • Snapshot the date/time so it is consistent as the script runs, since creating the tar.gz file can take a few minutes.
  • Call /save-all flush in the Minecraft screen console, which has Minecraft save all chunks to the storage device immediately, rather than waiting for them to done over time. This may cause a freeze on the server if there are lot of chunks to be saved, however I have my server set to save every 4 hours, there shouldn’t be a performance hit during save.
  • Calls /save-off in the Minecraft screen console, so nothing should be written to the World files while the backup is running.
  • Calls the Google utility to copy the created tar.gz file to the minecraft-server backup bucket.
  • Calls /save-on to resume normal saving to the World files on the storage device.
  • Removes the tar.gz file locally, as it has been backed up.

Loading

One thought on “Daemonizing and World Backups

Leave a Reply

Your email address will not be published. Required fields are marked *