So the boys found Minecraft, and had all sorts of fun. They played in single player mode and did great exploits, and then they started looking around for ways to play collaboratively. They had some trouble getting going with the network server thingy, but eventually figured it out and got things under way.
Of course, they ran into trouble almost immediately. Well, not trouble, but inconvenience. Mat’s computer was the most powerful, so it was being the server. All well and good, until he was out when someone else wanted to play in their world. So, I figured I would step in and, being a Dad with some IT skills, set up a Linux VM for Minecraft on my computer. My PC generally doesn’t stop, so that would save them having to share passwords.
With the VM set up, I then transferred their world to the new server. All good, and always accessible. I punched a hole in our firewall, forwarding the port to that VM, and so now their friends are able to play as well. Everyone was happy.
Until. Until that fateful day, when someone gave the server address to someone else, who came in and griefed their world. We had no backup. Oops. We blacklisted the kid, and switched to whitelisting, but the damage was already done. The players started the repair work, and life went on.
Actually, it didn’t. What happened was that I had let Nathan use my PC for the repair work, because it’s really quick. Unfortunately, he accidentally hit the “Sleep” key on my keyboard, and (quite reasonably) the PC went to sleep. My PC doesn’t wake up. If it goes into sleep mode, it’s functionally the same as pressing the restart button, but without the same sense of finality. It provides you with that momentary sense of relief: wow, that was close, I could have hit the reset button instead! But then it won’t wake up. Grr. I had to switch the machine off and then on again, and, sure enough, all the VMs had to boot, and the Minecraft world had some problems. Major problems. The world was pretty much corrupted beyond my ability to repair.
We had a round table conference, and after much discussion, including teleconferencing in one of the remote players, we decided to drop the world and start from scratch. I committed to getting backups working, and we went our happy way.
Lightning struck a second time. Actually, we’re not sure if it was actually lightning, but the power went out. Actually, we are sure. It was toast being pushed down into the toaster that flipped out the breaker on that end of the house on the morning after the griefing, and before I’d set up the backups. I have three UPSs around the place, and it turns out that the one supporting my computer just wasn’t up to the task. Yes, I know I should have tested with that UPS, but that’s the cookie crumbling. Fortunately, no-one was playing at the time, and the minor corruption was able to be repaired. This galvanised me into action. Backups, here we come.
I figured that the server would have to stop for backups, and then be restarted. So the way to do that would be to have the server running as a SysV daemon, stop it at backup time, make the backup, and then restart it.
I scoured around the net for SysV startup scripts for minecraft, and found this one. That link will take you to a newsgroup article with a link to download the script. I downloaded it, started to install it, and then started running into problems. I figured that I would just get the server running as a daemon started by root. Uh uh. This one won’t do it. That made sense when I thought it through, because I don’t know how secure the server program is, and I don’t want to have to su to root when I want to fiddle with it.
OK, so I had a “minecraft” user already set up, and I figured that I would just do it with that user. Nope. The README file for this script says that that particular username is too long. Use “miner”, it says. So I set up the “miner” user, and put the rc.minecraft script into /etc/init.d. Once I’d done that, I needed to get it so that it would start the server at boot. I did this with the following command:
# chkconfig -s rc.minecraft on
I also needed to make some configuration stuff available to the server. According to the doco, the script looks in /etc and /etc/init for a minecraft.conf file. Fine, so I did the following:
# cd /etc/init.d # ./rc.minecraft dumpconfig > /home/miner/.minecraft.conf # chown miner.miner /home/miner/.minecraft.conf # cd ../init # ln -s /home/miner/.minecraft.conf minecraft.conf
OK, that’s good. Now the server will start when the computer starts, stop when the computer stops, and I can edit ~/.minecraft.conf as miner to change any of the script parameters.
Now what about the backup? As the miner user, I created a script file with the following lines:
#!/bin/bash /etc/init.d/rc.minecraft stop tar cfz "/home/miner/backup.`date +%Y%m%d`.tgz" /home/miner/mc-server/ /etc/init.d/rc.minecraft start
Then I edited the miner crontab file ($crontab -e) and added the following line:
0 3 * * * /bin/bash /home/miner/backup.sh
This means that at 3am every morning, the server will be stopped, a backup will be made and put into a tarball named with today’s date, and the server will start back up again. Perfect!
Oh, and I switched off the sleep key on my keyboard. Sigh.
Many thanks are due to Dagmar d’Surreal for his init script.
Leave a Reply