Monday, July 18, 2016

Minecraft Pi backup Worlds on exit

My kids love building cities on the Raspberry Pi version of Minecraft.  They're not programming them yet, but they spend hours placing blocks and designing houses, hotels, swimming pools, and other building and gardens.
All of which is great.
Except I also use the Raspberry Pi for programming and other things where due to my fumbling around I end up borking the SD card and have to start again.

That's OK. If you backup all the files in ~/.minecraft/games/com.mojang/ and then put them on the new SD card all is good.  Really simple backup.
Only problem is I keep forgetting this step and delete weeks or months or effort.

Rather than expecting somehow that I will remember in the future I thought it would be a good idea to solve the problem with a bit of code and a little modification to the menu.

The goal is to replace the command that runs minecraft-pi with a command that runs minecraft-pi and then on exit archives the worlds and sends them to an ftp server.
In this way the backup is off the Pi and even if I do reformat it I can get the backup minecraft worlds and just reinstate them.

All this is being done with Raspbian as the OS.

For the ftp server I have OSMC set up on a Raspberry Pi (1) B+ that I use mainly as a media server for a couple of Blu-Ray player and tablets in the house so it is on 24x7.

The code is quite short made up of a python script that does the archiving and ftping of the file and a bash script that runs minecraft-pi and then runs the backup script.
The code is available at https://github.com/winkleink/MinecraftPi_backup

Just copy it into pi user home folder '/home/pi'
If you copy the code somewhere else then you will need to modify the bash script and the files to take it in to account.
Once copied change the file preferences to permit execution. Since you're in the GUI the easiest way to do this is right click on the file and select [Properties].
The go to Permissions and set Execute.  I chose Anyone, but if you're only going to run as user pi then you can select 'Only owner'

Change Execute permissions so this is a program that can be run

You need to do this for the 2 files (minec.sh and ftpzipminecraft.py)

minec.sh is the bash script to run the 2 commands. With the semicolon at the end of the first command it knows to run them in sequence (one after each other) rather than potentially at the same time. No modification is needed unless you save the ftpzipminecraft.py program somewhere other than the pi home folder

minec.sh

#!/bin/bash
minecraft-pi; 
/home/pi/ftpzipminecraft.py

ftpzipminecraft.py runs after minecraft-pi closes and this does the archiving to local file in the pi home folder and then connects to the ftp server and transfers the archive to the server.
You have to change 3 items in the file for it to work.  I've highlighted them in red below.

FTP SERVER NAME/IP ADDRESS
USERNAME
PASSWORD

ftpzipminecraft.py

#!/usr/bin/python3
# Script to backup the MinecraftPi worlds and then upload them to an FTP server
# By Winkleink (July 2016)
# https://github.com/winkleink/MinecraftPi_backup
# MIT Licence
# This is a quick fix so use at your peril
import os
import zipfile
import datetime

import ftplib

now = datetime.datetime.now()
year= str(now.year)
month = str(now.month)
day = str(now.day)
hour = str(now.hour)
minute= str(now.minute)

uploadfile = "minecraft"+year+"-"+month+"-"+day+"-_-"+hour+"-"+minute+".tar.gz"

print(uploadfile)
os.system ("tar zcvf " + uploadfile + " /home/pi/.minecraft/games/com.mojang")

# Enter your FTP Server Name or IP address, Username and Password in the following line
session = ftplib.FTP("FTP SERVER NAME/IP ADDRESS","USERNAME","PASSWORD")
file = open("/home/pi/"+uploadfile, "rb")
session.storbinary("STOR " + uploadfile, file)
file.close()
session.quit()
print("Done")





Once both files are in place and are set to executable we need to modify the menu entry for Minecraft so it runs our new script minec.sh

From the main menu select [Preferences] [Main Menu Editor]


Then on the left select [Games] and pick [Minecraft] and then click [Properties] on the right.


Finally, replace the 'minecraft-pi' command with '/home/pi/minec.sh'


Its not shown here, but so I know it's the one I've changed I modified the Comment to 'Minecraft + Backup' so when I mouse over the menu entry I can see it the one with the backup.

With this little change I will hopefully never lose my kids amazing creations in Minecraft on the Raspberry Pi.