MtnMCG

ndisk server migration

Dec 20th, 2025
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. -- migrate_to_ndisk.lua
  2. -- Script to migrate data from local disk "students/" folder to NDisk
  3.  
  4. local ndisk = require("ndisk_api")
  5. local disk = peripheral.find("drive")
  6. if not disk then
  7. print("Error: No disk drive found. Cannot read source data.")
  8. return
  9. end
  10.  
  11. local diskMount = disk.getMountPath()
  12. if not diskMount then
  13. print("Error: No disk inserted.")
  14. return
  15. end
  16.  
  17. if not fs.exists(diskMount .. "/students") then
  18. print("Error: 'students' directory not found on disk.")
  19. return
  20. end
  21.  
  22. print("Initializing NDisk...")
  23. ndisk.init()
  24. print("Starting migration...")
  25.  
  26. local successCount = 0
  27. local failCount = 0
  28.  
  29. local function migrateFile(localPath, ndiskPath)
  30. print("Migrating: " .. ndiskPath)
  31. local file = fs.open(localPath, "r")
  32. local content = file.readAll()
  33. file.close()
  34.  
  35. local success, err = ndisk.write(ndiskPath, content)
  36. if success then
  37. -- Verify
  38. local readBack, readErr = ndisk.read(ndiskPath)
  39. if readBack == content then
  40. print(" [OK]")
  41. successCount = successCount + 1
  42. else
  43. print(" [FAIL] Verification mismatch: " .. (readErr or "Content differs"))
  44. failCount = failCount + 1
  45. end
  46. else
  47. print(" [FAIL] Write error: " .. (err or "matches"))
  48. failCount = failCount + 1
  49. end
  50. end
  51.  
  52. local function traverse(path, relativePath)
  53. local list = fs.list(path)
  54. for _, item in ipairs(list) do
  55. local fullPath = fs.combine(path, item)
  56. local newRelPath = relativePath == "" and item or (relativePath .. "/" .. item)
  57.  
  58. if fs.isDir(fullPath) then
  59. traverse(fullPath, newRelPath)
  60. else
  61. -- Check if it's a student file?
  62. -- path starts with diskMount .. "/students"
  63. -- relPath starts with "students/..." if we pass that?
  64. -- Actually, we want keys in NDisk to be "students/ID/file.txt"
  65. -- So if we iterate inside "diskMount/students", relativePath is "ID/file.txt"
  66. -- We want to prepend "students/"
  67.  
  68. local ndiskKey = "students/" .. newRelPath
  69. migrateFile(fullPath, ndiskKey)
  70. end
  71. end
  72. end
  73.  
  74. traverse(diskMount .. "/students", "")
  75.  
  76. print("Migration Complete.")
  77. print("Success: " .. successCount)
  78. print("Failed: " .. failCount)
  79. print("You can verify manually via 'ndisk_api' or running the server.")
  80.  
Advertisement
Add Comment
Please, Sign In to add comment