Advertisement
Tatantyler

Backup Server

Sep 22nd, 2012
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.43 KB | None | 0 0
  1. local args = {...}
  2. -- backup packet structure:
  3. -- {identifier, originator_id, files}
  4.  
  5. local function getDriveOne()
  6.     for i,v in ipairs(rs.getSides()) do
  7.         if peripheral.isPresent(v) and peripheral.getType(v) == "drive" and disk.getMountPath(v) == "disk" then
  8.             return v
  9.         end
  10.     end
  11. end
  12.  
  13. local restoreProgram = [[
  14.  
  15. local function getDriveOne()
  16.     for i,v in ipairs(rs.getSides()) do
  17.         if peripheral.isPresent(v) and peripheral.getType(v) == "drive" and disk.getMountPath(v) == "disk" then
  18.             return v
  19.         end
  20.     end
  21. end
  22.  
  23. local function restore()
  24.     if fs.exists("disk/backup") then
  25.         local backup = fs.open("disk/backup", "r")
  26.         local fileTable = backup.readAll()
  27.         backup.close()
  28.         local fileTable = textutils.unserialize(fileTable)
  29.         print("This disk contains a stored backup of computer "..tostring(fileTable[1])..".")
  30.         if type(fileTable[4]) == "string" then
  31.             print("Label: "..fileTable[4]..".")
  32.             os.setComputerLabel(fileTable[4])
  33.             hasLabel = true
  34.         end
  35.         print("Total files: "..tostring(fileTable[3]))
  36.         print("Total file size: "..tostring(fileTable[2]))
  37.         while true do
  38.             print("Restore from backup? (Y/N) >")
  39.             local yn = read()
  40.             yn = string.upper(yn)
  41.             if yn == "Y" then
  42.                 break
  43.             elseif yn == "N" then
  44.                 return
  45.             else
  46.                 print("Please provide a valid response.")
  47.             end
  48.         end
  49.         os.sleep(1.5)
  50.         for i,v in ipairs(fileTable) do
  51.             if i ~= 1 and i~= 2 and i ~= 3 then
  52.                 term.clear()
  53.                 term.setCursorPos(1,1)
  54.                 print("Restoring file.")
  55.                 print("ID: "..v[1])
  56.                 print("Path: "..v[2])
  57.                 if v[3] then
  58.                     print("Is a directory.")
  59.                 else
  60.                     print("Is a file.")
  61.                     print("Size: "..tostring(v[4]))
  62.                 end
  63.                
  64.                 if v[3] then
  65.                     fs.makeDir(v[2])
  66.                 else
  67.                     local file = fs.open(v[2], "w")
  68.                     file.write(v[5])
  69.                     file.close()
  70.                 end
  71.                 os.sleep(0.5)
  72.             end
  73.         end
  74.         print("Restore complete.")
  75.         os.sleep(1)
  76.         disk.eject(getDriveOne())
  77.         os.reboot()
  78.     end
  79. end
  80.  
  81. restore()
  82. ]]
  83.  
  84. local function writeBackupToDisk(entries, id)
  85.     local backup = fs.open("disk/backup", "w")
  86.     backup.write(entries)
  87.     backup.close()
  88.     disk.setLabel(getDriveOne(), "Backup of computer "..id)
  89.     local programHandle = fs.open("disk/startup", "w")
  90.     programHandle.write(restoreProgram)
  91.     programHandle.close()
  92.     disk.eject(getDriveOne())
  93. end
  94.  
  95. local function handleIncomingPackets()
  96.     for i,v in ipairs(rs.getSides()) do
  97.         rednet.open(v)
  98.     end
  99.     while true do
  100.         id, message = rednet.receive()
  101.         local packet = textutils.unserialize(message)
  102.         if type(packet) == "table" then -- valid packet
  103.             if packet[1] == "backup" then
  104.                 if not fs.exists("backups") then
  105.                     fs.makeDir("backups")
  106.                 end
  107.                 print("Received backup from computer "..tostring(id)..".")
  108.                 local backupHandle = fs.open("backups/"..tostring(id), "w")
  109.                 backupHandle.write(packet[2])
  110.                 backupHandle.close()
  111.             end
  112.         end
  113.     end
  114. end
  115.  
  116. local function handleInput()
  117.     while true do
  118.         term.clear()
  119.         term.setCursorPos(1,1)
  120.         print("Please enter a backup ID:")
  121.         local backup = read()
  122.         if type(tonumber(backup)) == "number" then
  123.             if fs.exists("backups/"..backup) then
  124.                 local handle = fs.open("backups/"..backup, "r")
  125.                 local files = handle.readAll()
  126.                 handle.close()
  127.                 writeBackupToDisk(files, backup)
  128.             else
  129.                 print("That backup does not exist.")
  130.                 os.sleep(2)
  131.             end
  132.         else
  133.             print("Please type in a valid ID.")
  134.             os.sleep(2)
  135.         end
  136.     end
  137. end
  138.  
  139. parallel.waitForAny(handleIncomingPackets, handleInput)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement