I live in a country where an electricity blackout happens after every hour, which corrupts the Minecraft save files, so I came up with this auto backup utility which backs up the %appData%.minecraft\saves folder constantly after a specified time.
It works fine but I'm still not satisfied with it. Tell me what more I can do to improve/enhance it.
import time
import os
import sys
#class for parsing settings.ini
class parseIni(object):
def __init__(self, fileStream):
self.lines = fileStream.readlines()
self.options = []
for line in self.lines:
if "=" not in line:
continue
else:
self.options.append(tuple(line.replace("\n", "").split("=")))
def getValueOf(self, option):
for op, value in self.options:
if op==option:
return value
#Tries to open the settings file, if unsuccessful then exit program
settingsFileStream = None
try:
settingsFileStream = open("settings.ini")
except IOError:
print("ERROR: SETTINGS FILE NOT FOUND.", file=sys.stderr)
raw_input("Press <ENTER> to exit....")
sys.exit(0)
#Initialize parser
settings = parseIni(settingsFileStream)
#Precondition: settings file was opened successfully
#Assigning values to required variables
timeDelayInMins = eval(settings.getValueOf("timeDelay")) #For user information
timeDelay = timeDelayInMins * 60 #Converting to seconds, for time.sleep() method
sourcePath = settings.getValueOf("sourcePath")
destinationPath = settings.getValueOf("destinationPath")
#Runs the game
os.system("Minecraft.exe")
#Prints out Welcome message
print()
print("|--------------------------------------------------------|")
print("|WELCOME TO MINECRAFT AUTO BACKUP UTILITY! |")
print("|--------------------------------------------------------|")
print("|REFER TO THE settings.ini FILE TO EDIT SETTINGS FOR THIS|")
print("|PROGRAM. e.g. TIME DELAY FOR AUTO BACKUP, PATHS etc |")
print("|--------------------------------------------------------|")
print("|TIME DELAY FOR AUTO BACKUP IS {:0.2f} MIN(S) |".format(timeDelayInMins))
print("|--------------------------------------------------------|")
print()
#Variable to keep tract of backups done per session
backupsDone = 0
#mainLoop runs forever, backups "saves" folder every after ever timeDelay mins
while True:
time.sleep(timeDelay)
os.system('robocopy "{}" "{}" /e'.format(sourcePath, destinationPath))
backupsDone += 1
print
print
print("|---------------------------------|")
print("|{} BACKUP(S) DONE THIS SESSION. |".format(backupsDone))
print("|---------------------------------|")
And the settings.ini
file with the program is:
[TIME] timeDelay=1 [PATHS] sourcePath=%appData%\.minecraft\saves destinationPath=%appData%\.minecraft\SavesBackup