Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ndisk_recover_from_disk.lua
- -- Recovery script: pulls data from old disk/students/ and writes to NDisk
- -- Run this on the main server
- local function log(msg)
- print("[RECOVERY] " .. msg)
- end
- local function logSuccess(msg)
- term.setTextColor(colors.lime)
- log(msg)
- term.setTextColor(colors.white)
- end
- local function logError(msg)
- term.setTextColor(colors.red)
- log("ERROR: " .. msg)
- term.setTextColor(colors.white)
- end
- local function logWarn(msg)
- term.setTextColor(colors.yellow)
- log("WARN: " .. msg)
- term.setTextColor(colors.white)
- end
- local function main()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.cyan)
- print("========================================")
- print(" NDisk Data Recovery Tool")
- print("========================================")
- term.setTextColor(colors.white)
- print("")
- -- Check if old data exists
- if not fs.exists("disk/students") then
- logError("disk/students not found!")
- print("This script requires old backup data at disk/students/")
- return
- end
- -- Load NDisk
- log("Loading NDisk API...")
- local ndisk = dofile("ndisk_api.lua")
- ndisk.init()
- local raidStatus = ndisk.getRaidStatus()
- print("NDisk: " .. raidStatus.nodeCount .. " nodes (" .. raidStatus.mode .. ")")
- print("")
- -- Get list of users from old backup
- local oldUsers = fs.list("disk/students")
- log("Found " .. #oldUsers .. " users in old backup")
- -- Files to recover (in priority order)
- local filesToRecover = {
- "password.txt", -- Critical for login
- "balance.txt", -- Financial data
- "emails.txt", -- User emails
- "doors.txt", -- Door access
- -- location_history.txt is likely still in NDisk (it survived)
- }
- print("")
- term.setTextColor(colors.yellow)
- print("Will attempt to recover: password, balance, emails, doors")
- print("for each user from disk/students/ to NDisk")
- term.setTextColor(colors.white)
- print("")
- write("Start recovery? [y/N]: ")
- local input = read()
- if input:lower() ~= "y" and input:lower() ~= "yes" then
- print("Aborted.")
- return
- end
- print("")
- local recovered = 0
- local skipped = 0
- local errors = 0
- for _, username in ipairs(oldUsers) do
- local userPath = "disk/students/" .. username
- if fs.isDir(userPath) then
- log("Processing: " .. username)
- for _, filename in ipairs(filesToRecover) do
- local oldPath = userPath .. "/" .. filename
- local ndiskPath = "students/" .. username .. "/" .. filename
- if fs.exists(oldPath) then
- -- Read old file
- local file = fs.open(oldPath, "r")
- local content = file.readAll()
- file.close()
- -- Check if already exists in NDisk
- local existing = ndisk.read(ndiskPath)
- if existing then
- -- Skip if already has data
- print(" " .. filename .. ": already exists, skipping")
- skipped = skipped + 1
- else
- -- Write to NDisk
- local success, err = ndisk.write(ndiskPath, content)
- if success then
- term.setTextColor(colors.lime)
- print(" " .. filename .. ": RECOVERED")
- term.setTextColor(colors.white)
- recovered = recovered + 1
- else
- term.setTextColor(colors.red)
- print(" " .. filename .. ": FAILED - " .. (err or "unknown"))
- term.setTextColor(colors.white)
- errors = errors + 1
- end
- end
- else
- print(" " .. filename .. ": not in backup")
- end
- end
- print("")
- end
- end
- print("========================================")
- print(" Recovery Complete")
- print("========================================")
- print("")
- logSuccess("Recovered: " .. recovered .. " files")
- if skipped > 0 then
- logWarn("Skipped (already existed): " .. skipped)
- end
- if errors > 0 then
- logError("Errors: " .. errors)
- end
- print("")
- print("Users should now be able to log in with their old passwords.")
- print("Balance data restored from old backup (may be outdated).")
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment