From 7ac10948df27f9a041083f72b7a78fc430ab159b Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Tue, 12 Mar 2019 09:58:02 -0400 Subject: [PATCH] milo config backups --- milo/apis/init.lua | 4 -- milo/apis/storage.lua | 1 - milo/plugins/backupView.lua | 115 +++++++++++++++++++++++++++++++++++ milo/plugins/speakerView.lua | 2 +- 4 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 milo/plugins/backupView.lua diff --git a/milo/apis/init.lua b/milo/apis/init.lua index 411e5e2..5e3c023 100644 --- a/milo/apis/init.lua +++ b/milo/apis/init.lua @@ -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 diff --git a/milo/apis/storage.lua b/milo/apis/storage.lua index 7ad745b..7aea941 100644 --- a/milo/apis/storage.lua +++ b/milo/apis/storage.lua @@ -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 diff --git a/milo/plugins/backupView.lua b/milo/plugins/backupView.lua new file mode 100644 index 0000000..86a38fd --- /dev/null +++ b/milo/plugins/backupView.lua @@ -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) diff --git a/milo/plugins/speakerView.lua b/milo/plugins/speakerView.lua index fb448a0..ea5af6c 100644 --- a/milo/plugins/speakerView.lua +++ b/milo/plugins/speakerView.lua @@ -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',