Host a Minecraft Server on Google Cloud With Automatic Deployment and Backup

I think when we talk about hosting a Minecraft server on Google Cloud Platform (GCP), there are tens of thousands of tutorial. However, it seems not too many articles talk about how to maintain the server well. For example, when I want to upgrade my server, could I switch over with the least down time? If using my approach, it takes around 1 min to switch over the server without changing the IP address. Also, the backup files will be stored in Google Cloud Storage every 4 hours, that means once we want to time back, we at most lose 4 hours data. Of course, the time could is set by ourselves. The whole solution diagram will look like below, I will go through how to implement it step by step.

Mincecraft Hosting Solution Diagram

Before we start, please make sure you have a GCP account, I do not go through the detail of this.

Create an instance

Compute Engine

First of all, please click the left top corner logo and it will show a list of service of GCP. Just click Compute Engine and if you are the first time to use, it will take a few minutes to start. Then click “Create”.

Instance Setting
Instance Setting (2)

Secondly, we have to choose our hardware specification. In fact, this part is up to you but if you do not have any preference, just follow me. My setting is the minimum requirement to start the minecraft server. Once you have many friends want to join, you could upgrade it easily using auto deployment. The most important thing is to set the “Network tags” as “minecraft-server” because we will use this tag to do our firewall policy. Then click “Create”

Create Firewall for Minecraft Server

Create Firewall Rules

We search for VPC network and choose “Create Firewall Rules”.

There are 3 things we care. The target tags should be the same as the network tag we create before. The source IP should be “0.0.0.0/0” which means open to everyone. The port should be TCP:25565 which is used by Minecraft. For security reason, we should release the least permission. The click “Create”.

Reserve External IP addresses

External IP addresses

In External IP addresses of VPC network, please let the IP become “static”.

Install Minecraft on Ubuntu

Connect via SSH

Once the server is created, click SSH as the above figure shown.

sudo apt update -y
sudo apt upgrade -y
sudo apt install openjdk-14-jdk -y

We have to update and upgrade linux with the jdk first. You could place this script on startup script if you want, it is not a big deal since we will also do it later.

wget https://launcher.mojang.com/v1/objects/bb2b6b1aefcd70dfd1892149ac3a215f6c636b07/server.jar

Download Mincecraft Server File

After updating, we need to download the Minecraft server from official site, here the version is 1.15.2. We get the server.jar file now.

sudo screen -S MinecraftServerScreen
java -Xmx1024M -Xms1024M -jar server.jar nogui

Fail to Start jar file

Let’s try to run the jar file in another screen. Oh, it seems we have to agree to the license agreement conditions.

List Directory

If we now check the file directory, we will find that few more files appear.

nano eula.txt

For those who are not familiar with linux, please just simply use nano to edit the eula.txt file. In the text editor, we just place “eula=true”. Then we press “Ctrl + O” and “Enter” to write out, and “Ctrl + X” to exit.

java -Xmx1024M -Xms1024M -jar server.jar nogui

Running Minecraft Server

Let’s try again. This time we run successfully! And we should be able to connect this Minecraft server by typing the IP address.

Backup Script

As we mentioned above, we will backup our files on Google Cloud Storage every 4 hours. We firstly have to press “Ctrl + A + D” to exit the screen, but the server will be still running.

gcloud auth login

First of all, we have to set permission to access Google Cloud Storage (GCS). After typing the command “gcloud auth login”, we click the link that the red arrow point to, click “Allow” and copy the code, then paste it on the “verification code”.

gsutil mb -c standard -l us-central1 gs://[PROJECT_ID]-minecraftbackup

Create a new Cloud Storage Standard Storage bucket, replacing us-central1 with the Cloud Storage region closest to you and [PROJECT_ID] with your project ID. You’ll use this bucket to store your backups. For reference, my project ID is minecraft. To check your project ID, just see the name near to “Google Cloud Platform”. Once the command is typed, the bucket will be created immediately.

sudo nano /etc/init.d/backup.sh

#!/bin/bash
now=$(date +’%Y%m%d%H%M%S’)
screen -r MinecraftServerScreen -X stuff ‘/save-all\n/save-off\n’
gsutil -m cp -R ${BASH_SOURCE%/*}/world gs://minecraft-minecraftbackup/$(date “+%Y%m%d-%H%M%S”)-world
screen -r MinecraftServerScreen -X stuff ‘/save-on\n’
echo “saved file at $now”

We should create a backup script on /etc/init.d/backup.sh, and place the above script. If you want to know the $[BASH_SOURCE%/*], just type “pwd” command on the location of jar file you put. It should be /home/xxx. Then we press “Ctrl + O” and “Enter” to write out, and “Ctrl + X” to exit.

sudo chmod 755 /etc/init.d/backup.sh
sudo /etc/init.d/backup.sh

Save and exit, then run the above command to make your script executable then test it.

Actually, what we have to backup is the “world” folder only. And once the backup script is running, the bucket will appear the backup file immediately with exact date-time in the filename.

Startup Script

As I mentioned before, we have to update the linux and run the server automatically when the server starts, therefore it is better to write a startup script.

sudo nano /etc/init.d/startup.sh

sudo apt-get update
sudo screen -dm -S MinecraftServerScreen bash -c “cd ${BASH_SOURCE%/*} && sudo java -Xmx1024M -Xms1024M -d64 -jar server.jar nogui”

The second line means to locate the server.jar file and run it in the screen. Then we press “Ctrl + O” and “Enter” to write out, and “Ctrl + X” to exit.

sudo chmod 755 /etc/init.d/startup.sh

We finally make our script executable.

Schedule a Cron Job

The basic setting and script is prepared well now. What we have to do next is to arrange the time to repeatedly run the script.

sudo crontab -e

When it is the first time to use crontab, it will let you select the editor, just simply type “1” and “enter”.

@reboot /etc/init.d/startup.sh
0 */4 * * * /etc/init.d/backup.sh

The first line means to run startup script when reboot, the second line means to run backup script every 4 hours. For how to set the time, please check it on here. Then we press “Ctrl + O” and “Enter” to write out, and “Ctrl + X” to exit.

sudo reboot
sudo screen -r

Now, when you reboot the instance then reattach the screen using the second command, you will see linux is running the server.jar file automatically.

Change Server Using Google Cloud Shell

Google Cloud Shell

All the things are ready now. Actually, Google Cloud Shell could control many services in GCP. We click on the pen as figure shown above. Then it will appear something like IDE, we could write our script here.

## Name current_snapshot
export current_snapshot=”mc-server-$(date +’%Y%m%d%H%M%S’)”## Only need to change snapshot source name, disk-size and machine-type
export existing_host=”minecraft-server”
disk_size=”10″
machine_type=”n1-standard-2″## Create snapshot
gcloud compute disks snapshot ${existing_host} \
–zone us-central1-a –snapshot-names=${current_snapshot}## Unassign the original ip with old host
gcloud compute instances delete-access-config ${existing_host} \
–access-config-name “External NAT” –zone us-central1-a## Restore snapshot
gcloud compute instances create ${current_snapshot} \
–source-snapshot ${current_snapshot} \
–boot-disk-size ${disk_size} \
–boot-disk-type pd-standard \
–boot-disk-device-name ${current_snapshot} \
–zone us-central1-a \
–scopes compute-rw,storage-ro \
–machine-type ${machine_type} \
–metadata shutdown-script=”#!/bin/bash
sh /etc/init.d/backup.sh
sudo screen -r ‘Minecraft Server Screen’ -X stuff ‘/stop\n'””” \
–tags mc-server \
–address 35.225.219.31

I will not go through the details of above script, and I mark a brief comment on it please check it out if you are interested. The idea of changing server without changing IP is quite simple. We firstly create a snapshot of existing server, then will remove the IP from existing host, finally we restore the snapshot with different machine types. Thus, what we have to provide are just three things: existing_host, new disk_size and new machine_type. We just save the above script as change-ser.sh file. Also, please modify your own zone and address.

sh change-ser.sh

After running the command on Google Cloud Shell, the script will help us create a new server within 1 min. We could see there are 2 servers here.

When we SSH to the new created server, we will find that it is exactly same as previous but just the hardware upgrade.

Cleanup Unnecessary Things

gcloud compute instances delete ${existing_host} –zone us-central1-a –delete-disks all -q
gcloud compute snapshots delete ${current_snapshot} -q

Once we have checked all the things are okay, then we have to remove the old server and the snapshot. The script is pretty simple as above. We save it as clean-up.sh

sh clean-up.sh

Run the above, command, then the old server and snapshot will be removed. If you are very confident with the script, you could just put the clean-up script together with the change-ser script.

Restore Backup

nano restore-backup.sh

Hope you all do not need this function. First of all, please SSH to the current host and create a restore-backup.sh using nano.

## run in host## Only need to change backup dir
host=”$(hostname)”
backup_dir=$(gsutil ls gs://minecraft-minecraftbackup | tail -1 | sed ‘s:.*minecraft-minecraftbackup/::’)sudo mv /home/minecraft/world /home/minecraft/old-world
gsutil -m cp -r gs://minecraft-minecraftbackup/${backup_dir} /home/minecraft
sudo mv /home/minecraft/${backup_dir} /home/minecraft/world
sudo rm -rf old-world## reboot
sudo pkill screen
sh /etc/init.d/startup.sh

Then we press “Ctrl + O” and “Enter” to write out, and “Ctrl + X” to exit. By default it will recover the latest backup. If you want to time back as some specific time, please modify the backup_dir.

sh restore-backup.sh

Once we run the backup script, it will communicate with Google Cloud Storage and exchange data. All data are back now.

Finally, enjoy Minecraft!

Ref: https://cloud.google.com/solutions/gaming/minecraft-server

Leave a Reply