There already exist instructions on how to install minecraft on a raspberry pi already. I mostly followed this guide: https://linuxize.com/post/how-to-install-minecraft-server-on-raspberry-pi/
However, I would like to document exactly what I did, so I can properly replicate it.
I have had bad luck with raspberries performing a unscheduled disassemblies – it looks like this is due to running out of space on upgrades, leaving the system without sshd and other nice to have things. Fixing these situations is pretty tricky since you never know if you got the system back into the proper state. So being able to create a new image from scratch without much fuss is the goal.
Hence I’ll document here
ssh-copy-id
, then at last edit
/etc/ssh/sshd_config
and change: #PasswordAuthentication yes
to PasswordAuthentication no
Again, a from-scratch install is described at https://linuxize.com/post/how-to-install-minecraft-server-on-raspberry-pi/. Since I still have a pi image with a bad OS (upgrade went bad), I will just copy from previous incarnation for now.
create minecraft user:
sudo useradd -r -m -U -d /opt/minecraft -s /bin/bash minecraft
copy /opt/minecraft from dead server
create a service file so minecraft starts automatically after reboot:
pi@raspberrypi:~ $ sudo vi /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 -Xmx768M -Xms512M -jar server.jar nogui
ExecStop=/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 \
-P 25575 -p SECRETPASSWORD stop
[Install]
WantedBy=multi-user.target
Then start service (And make sure it’s running):
pi@raspberrypi:~ $ sudo systemctl enable minecraft
pi@raspberrypi:~ $ sudo systemctl start minecraft
pi@raspberrypi:~ $ sudo systemctl status minecraft
Backup instructions: https://gist.github.com/kmtu/6326127, https://www.google.com/search?q=minecraft+server+backup+%22mcrcon%22
This script makes a local backup, but it also can write to a remote server. Since I am worrying about the SD card with the backups going belly-up, I also make a copy of the created backup file on a secondary server (a pihole RPi that’s moinitored by prometheus).
First, create a minecraft user and the /opt/minecraft directory as for the minecraft serverver.
Make sure ssh from minecraft to backup server works without asking for passwords, since the copying happens from cron without user inoput. On my system, which has password login disabled, I need to propagate ssh keys and make sure the key has no passphrase set.
Also make sure both minecraft and backup server have enough space for backups. E.g. one week backup = 50MB –> in one year we need 2.5GB of storage. I didn’t and ran out of space. It’s grungy work to clean up.
change the configuration items at the beginning of pishrink.sh
#!/bin/sh
WORLD='world'
SERVER='localhost'
PASSWD='123456' # Choose your own, better pwd!
MCUSER='minecraft'
MCPATH='/opt/minecraft/server'
BACKUPPATH='/opt/minecraft/backup/worlds'
REMOTE_BACKUPSERVER='192.168.1.222'
REMOTE_USER='minecraft'
REMOTE_BACKUPPATH='/opt/minecraft/backup/worlds'
MCRCON='/opt/minecraft/tools/mcrcon/mcrcon'
Add a crontab entry on the minecraft server like so:
sudo crontab -u minecraft -e
10 20 * * 1 /opt/minecraft/tools/backup.bash
Do a single full backup of /opt/minecraft/{server, tools} on backup server and save it on backup server, since the regular backups are just for the world and not configuration files.
Finally: Make a backup of the img file or you might have to do all this again soon.
It’s a) hard to get cost effective smaller micro-sd cards nowadays (cards smaller than 32GBs one are often not much cheaper then their big brethren), and it’s good to have extra storage. E.g. the Pi might horribly crash and damage the file system on uattended upgrades when running out of space.
However, having 32GB and larger backup img files is unwieldy (takes many minutes copying the file), so I recommend shrinking the image before archiving.
E.g., on my MacBook pro, I took pishrink from https://github.com/lisanet/PiShrink-macOS
, and then ran:
% brew install e2fsprogs
% sudo dd if=/dev/rdisk2 of=/Users/matik/Downloads/picraft.large.img bs=1m
% sudo pishrink /Users/matik/Downloads/picraft.large.img
% gzip /Users/matik/Downloads/picraft.large.img
I can now create a replacement image in a short time. The instructions above could help you too. I would however infinitely prefer an automated solution, like the ability to make snapshots on a running system. Amazon lightsail allows me to do this (OS independently, I tested linux and freebsd). Even a solution to create a copy of the image that requires a reboot, mounting root read-only and streaming it to a NAS would be great. Maybe in the future. If you have feedback, please peruse the comments.
Written on July 2, 2020