Goal
Instructional discussion showing how I manage my Minecraft Server. By the end of this discussion you will have the tools necessary to schedule restarts, create and store backups while also keeping it up and running. This will also help you strengthen your understanding of creating bash scripts and utilizing various tools including Crontab and Rclone in a Linux environment.
Server status script to verify Minecraft server is healthy and running!
To start this script we will use pgrep. This tool allows us to look up a process to see if it is running based on its name or a number of other attributes found in the manual. The option -x will search for exact match and with -f selected we will search for an exact match of the command line. To get this information I had the server running and then ran ps aux. With knowing the command we can narrow our search using grep.
After running pgrep we will then set the variable res to equal $?. This can be better explained here under “special parameters” in the bash manual. Essentially it will look at the last executed command which was pgrep and look at its exit status. If pgrep can find this match then res will be set to 0, if it can not be found it will be set to 1.
After we determine the value of $res we can then use an if statement to run a command if the server is not running ( $?== to 1). If the statement is executed then the tmux new-session -d -s mc_session ‘./run.sh’ will be used. I do not plan to dive into tmux today but you should definitely give it a look! I use it a lot. For this specific command it is nice because we can run Minecraft in a session in the background and whenever we need to view the session we can just attach the tmux session. If the $res is set to 0 and session is already running then there is no need to execute the if statement. I run this script every 5 minutes using crontab.
Automated scheduled Restarts and Backups for the server every night
This bash script performs the scheduled restarts and backups every night at 0000 hours. To start this script we will change the terminal sessions directory to where the server is stored (I tend to use full directory paths but sometimes I do not for no specific reason at all). The tmux send-keys “stop” command is used to stop the server. This will type stop in the tmux terminal session as if a user was doing it. This is because not only do we want to restart the server every so often, we also do not want to create backups with server running.
Next, we will set the time variable to a formatted date. This is then used in the compression of server filename.
tar -zcf ./backups/MC.backup-${time}.tgz ./*
For those not familar with tar it is a tool often used on linux to compress files. Similar to .zip which most are accustomed to on Windows or even .rar. We then set the -zcf options. -z filters through gzip, -c creates a new archive, -f to specify file. Followed by source and destination. For the source you can see me adding the $time variable to end of file name. This will allow for a unique file name every time a backup is created. Mainly for version control. Also allowing for me to revert back to another backup if one happens to get corrupted.
The next command is using the rclone tool. Super useful tool that allows for integration with many applications and well documented here. Before using this command you will have to setup the rclone config for the specific server you plan to upload to. This instruction can be found here. The rclone config utility within rclone is very straight forward and easy to set up within minutes. This tool also allows for encryption of login credentials. It then uses the copy command similar to most other copy commands and moves it from source to destination which I have named NextCloud:. Lastly is the –no-check-certificate, this option is used for those who have self signed certificates within their homelab. Without this option rclone will refuse to connect.
After backing up the srever to our self-hosted cloud we will then move the file to an uploaded directory using mv. Mainly because the rclone utility with current setup only allows for one argument. Using * wildcard is specifically only for that one file that gets created shortly before .tgz.
This script then ends with running the a run.sh script. This script is universal to all installs of Minecraft servers.
UPDATE (Fix for nightly backups):
For whatever reason I was not thinking straight and my server has been crashing at night. When I would come on in the morning I would see that somehow a backup was taking place every minute and filling my entire drive until it was full. This would then prevent server from running. Make sure if you want it to run only once at 0100 then you specify 0 as the minute and not *. If you specify * 1 * * *, it will then run this particular script every minute at the 0100 hour until 0200.
Conclusion
This instructional write-up is not step by step but instead an overview of what I use to perform these actions. I am still toying around with the various formats of my discussions but today I wanted to highlight the entire script and break down each line along with some references.
I have had some bugs along the way. This final version of the script should be good for now. Although I do have some ideas on how to improve it. For example one bug I had experienced earlier ended up in a loop where server would not start and for a .json file acting funky. Because of this and how I had the original script it would run the backup command every 5 minutes. After coming back a few hours later I noticed my HDD on MC server was full along with my NextCloud instance.
In the future one idea I have to improve this script is to manage how many backups I keep. For instance maybe only keep the last 5 days and delete anything later. I am not entirely sure how I could automate some checking of backups, but I would like to verify maybe once a week or every other week that my back ups are able to run without issue.
Well that is it for today. I will be making a video about these scripts over the next weekend for those who prefer a video format. Until next time, Never Stop Learning!