milo config backups

This commit is contained in:
kepler155c@gmail.com
2019-03-12 09:58:02 -04:00
parent b43761faf1
commit 7ac10948df
4 changed files with 116 additions and 6 deletions

View File

@@ -325,7 +325,6 @@ function Milo:updateRecipe(result, recipe)
recipe.result = nil
end
self.context.userRecipes[result] = recipe
Util.backup(Craft.USER_RECIPES)
Util.writeTable(Craft.USER_RECIPES, self.context.userRecipes)
Craft.loadRecipes()
end
@@ -335,12 +334,10 @@ function Milo:saveMachineRecipe(recipe, result, machine)
-- save the recipe
self.context.userRecipes[key] = recipe
Util.backup(Craft.USER_RECIPES)
Util.writeTable(Craft.USER_RECIPES, self.context.userRecipes)
-- save the machine association
Craft.machineLookup[key] = machine
Util.backup(Craft.MACHINE_LOOKUP)
Util.writeTable(Craft.MACHINE_LOOKUP, Craft.machineLookup)
Craft.loadRecipes()
@@ -395,7 +392,6 @@ function Milo:mergeResources(t)
end
function Milo:saveResources()
Util.backup(self.RESOURCE_FILE)
Util.writeTable(self.RESOURCE_FILE, self.context.resources)
end

View File

@@ -151,7 +151,6 @@ function Storage:saveConfiguration()
v.adapter = nil
end
Util.backup('usr/config/storage')
Config.update('storage', self.nodes)
for k,v in pairs(t) do

115
milo/plugins/backupView.lua Normal file
View File

@@ -0,0 +1,115 @@
local Ansi = require('ansi')
local Event = require('event')
local Milo = require('milo')
local UI = require('ui')
local colors = _G.colors
local fs = _G.fs
local os = _G.os
local DAY = 20 * 60
local context = Milo:getContext()
local drives = { }
--[[ Configuration Screen ]]
local template =
[[%sBackup Drive%s
Backup configuration files each minecraft day.
]]
local wizardPage = UI.WizardPage {
title = 'Backup Drive',
index = 2,
backgroundColor = colors.cyan,
[1] = UI.TextArea {
x = 2, ex = -2, y = 2, ey = -2,
value = string.format(template, Ansi.yellow, Ansi.reset),
},
}
function wizardPage:isValidType(node)
return node.adapter and node.adapter.type == 'drive' and {
name = 'Backup Drive',
value = 'backup',
category = 'custom',
help = 'Backup configuration files',
}
end
function wizardPage:isValidFor(node)
return node.mtype == 'backup'
end
UI:getPage('nodeWizard').wizard:add({ backupDrive = wizardPage })
local function clearOld(dir, fname)
local files = { }
for _, file in pairs(fs.list(dir)) do
if file:match(fname) then
table.insert(files, file)
end
end
if #files > 1 then
table.sort(files, function(a, b)
return tonumber(a:match('.(%d+)')) > tonumber(b:match('.(%d+)'))
end)
while #files > 1 do
local old = table.remove(files, #files)
fs.delete(fs.combine(dir, old))
end
end
end
local function makeBackup(dir, fname)
clearOld(dir, fname)
local source = fs.combine('usr/config', fname)
local dest = string.format('%s/%s.%d', dir, fname, os.day())
fs.copy(source, dest)
end
local function backupNode(node)
local files = {
'storage',
'milo.state',
'machine_crafting.db',
'recipes.db',
'resources.db',
}
local s, m = pcall(function()
if not node.adapter.isDiskPresent() then
_G._debug('BACKUP error: No media present')
else
local dir = node.adapter.getMountPath()
for _, v in pairs(files) do
makeBackup(dir, v)
end
end
end)
if not s and m then
_G._debug('BACKUP error:' .. m)
end
end
--[[ Task ]]--
local BackupTask = {
name = 'backup',
priority = 99,
}
function BackupTask:cycle()
for node in context.storage:filterActive('backup') do
if not drives[node.name] then
drives[node.name] = Event.onInterval(DAY, function()
_G._debug('BACKUP: started')
if node.adapter and node.adapter.online then
backupNode(node)
end
end)
end
end
end
Milo:registerTask(BackupTask)

View File

@@ -50,7 +50,7 @@ function wizardPage:validate()
end
function wizardPage:isValidType(node)
return peripheral.getType(node.name) == 'speaker' and {
return node.adapter and node.adapter.type == 'speaker' and {
name = 'Speaker',
value = 'speaker',
category = 'custom',