MtnMCG

ndisk_recover

Dec 22nd, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. -- ndisk_recover_from_disk.lua
  2. -- Recovery script: pulls data from old disk/students/ and writes to NDisk
  3. -- Run this on the main server
  4.  
  5. local function log(msg)
  6. print("[RECOVERY] " .. msg)
  7. end
  8.  
  9. local function logSuccess(msg)
  10. term.setTextColor(colors.lime)
  11. log(msg)
  12. term.setTextColor(colors.white)
  13. end
  14.  
  15. local function logError(msg)
  16. term.setTextColor(colors.red)
  17. log("ERROR: " .. msg)
  18. term.setTextColor(colors.white)
  19. end
  20.  
  21. local function logWarn(msg)
  22. term.setTextColor(colors.yellow)
  23. log("WARN: " .. msg)
  24. term.setTextColor(colors.white)
  25. end
  26.  
  27. local function main()
  28. term.clear()
  29. term.setCursorPos(1, 1)
  30.  
  31. term.setTextColor(colors.cyan)
  32. print("========================================")
  33. print(" NDisk Data Recovery Tool")
  34. print("========================================")
  35. term.setTextColor(colors.white)
  36. print("")
  37.  
  38. -- Check if old data exists
  39. if not fs.exists("disk/students") then
  40. logError("disk/students not found!")
  41. print("This script requires old backup data at disk/students/")
  42. return
  43. end
  44.  
  45. -- Load NDisk
  46. log("Loading NDisk API...")
  47. local ndisk = dofile("ndisk_api.lua")
  48. ndisk.init()
  49.  
  50. local raidStatus = ndisk.getRaidStatus()
  51. print("NDisk: " .. raidStatus.nodeCount .. " nodes (" .. raidStatus.mode .. ")")
  52. print("")
  53.  
  54. -- Get list of users from old backup
  55. local oldUsers = fs.list("disk/students")
  56. log("Found " .. #oldUsers .. " users in old backup")
  57.  
  58. -- Files to recover (in priority order)
  59. local filesToRecover = {
  60. "password.txt", -- Critical for login
  61. "balance.txt", -- Financial data
  62. "emails.txt", -- User emails
  63. "doors.txt", -- Door access
  64. -- location_history.txt is likely still in NDisk (it survived)
  65. }
  66.  
  67. print("")
  68. term.setTextColor(colors.yellow)
  69. print("Will attempt to recover: password, balance, emails, doors")
  70. print("for each user from disk/students/ to NDisk")
  71. term.setTextColor(colors.white)
  72. print("")
  73.  
  74. write("Start recovery? [y/N]: ")
  75. local input = read()
  76. if input:lower() ~= "y" and input:lower() ~= "yes" then
  77. print("Aborted.")
  78. return
  79. end
  80.  
  81. print("")
  82.  
  83. local recovered = 0
  84. local skipped = 0
  85. local errors = 0
  86.  
  87. for _, username in ipairs(oldUsers) do
  88. local userPath = "disk/students/" .. username
  89.  
  90. if fs.isDir(userPath) then
  91. log("Processing: " .. username)
  92.  
  93. for _, filename in ipairs(filesToRecover) do
  94. local oldPath = userPath .. "/" .. filename
  95. local ndiskPath = "students/" .. username .. "/" .. filename
  96.  
  97. if fs.exists(oldPath) then
  98. -- Read old file
  99. local file = fs.open(oldPath, "r")
  100. local content = file.readAll()
  101. file.close()
  102.  
  103. -- Check if already exists in NDisk
  104. local existing = ndisk.read(ndiskPath)
  105. if existing then
  106. -- Skip if already has data
  107. print(" " .. filename .. ": already exists, skipping")
  108. skipped = skipped + 1
  109. else
  110. -- Write to NDisk
  111. local success, err = ndisk.write(ndiskPath, content)
  112. if success then
  113. term.setTextColor(colors.lime)
  114. print(" " .. filename .. ": RECOVERED")
  115. term.setTextColor(colors.white)
  116. recovered = recovered + 1
  117. else
  118. term.setTextColor(colors.red)
  119. print(" " .. filename .. ": FAILED - " .. (err or "unknown"))
  120. term.setTextColor(colors.white)
  121. errors = errors + 1
  122. end
  123. end
  124. else
  125. print(" " .. filename .. ": not in backup")
  126. end
  127. end
  128. print("")
  129. end
  130. end
  131.  
  132. print("========================================")
  133. print(" Recovery Complete")
  134. print("========================================")
  135. print("")
  136. logSuccess("Recovered: " .. recovered .. " files")
  137. if skipped > 0 then
  138. logWarn("Skipped (already existed): " .. skipped)
  139. end
  140. if errors > 0 then
  141. logError("Errors: " .. errors)
  142. end
  143. print("")
  144. print("Users should now be able to log in with their old passwords.")
  145. print("Balance data restored from old backup (may be outdated).")
  146. end
  147.  
  148. main()
  149.  
Advertisement
Add Comment
Please, Sign In to add comment