Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- -- backup packet structure:
- -- {identifier, originator_id, files}
- local function getDriveOne()
- for i,v in ipairs(rs.getSides()) do
- if peripheral.isPresent(v) and peripheral.getType(v) == "drive" and disk.getMountPath(v) == "disk" then
- return v
- end
- end
- end
- local restoreProgram = [[
- local function getDriveOne()
- for i,v in ipairs(rs.getSides()) do
- if peripheral.isPresent(v) and peripheral.getType(v) == "drive" and disk.getMountPath(v) == "disk" then
- return v
- end
- end
- end
- local function restore()
- if fs.exists("disk/backup") then
- local backup = fs.open("disk/backup", "r")
- local fileTable = backup.readAll()
- backup.close()
- local fileTable = textutils.unserialize(fileTable)
- print("This disk contains a stored backup of computer "..tostring(fileTable[1])..".")
- if type(fileTable[4]) == "string" then
- print("Label: "..fileTable[4]..".")
- os.setComputerLabel(fileTable[4])
- hasLabel = true
- end
- print("Total files: "..tostring(fileTable[3]))
- print("Total file size: "..tostring(fileTable[2]))
- while true do
- print("Restore from backup? (Y/N) >")
- local yn = read()
- yn = string.upper(yn)
- if yn == "Y" then
- break
- elseif yn == "N" then
- return
- else
- print("Please provide a valid response.")
- end
- end
- os.sleep(1.5)
- for i,v in ipairs(fileTable) do
- if i ~= 1 and i~= 2 and i ~= 3 then
- term.clear()
- term.setCursorPos(1,1)
- print("Restoring file.")
- print("ID: "..v[1])
- print("Path: "..v[2])
- if v[3] then
- print("Is a directory.")
- else
- print("Is a file.")
- print("Size: "..tostring(v[4]))
- end
- if v[3] then
- fs.makeDir(v[2])
- else
- local file = fs.open(v[2], "w")
- file.write(v[5])
- file.close()
- end
- os.sleep(0.5)
- end
- end
- print("Restore complete.")
- os.sleep(1)
- disk.eject(getDriveOne())
- os.reboot()
- end
- end
- restore()
- ]]
- local function writeBackupToDisk(entries, id)
- local backup = fs.open("disk/backup", "w")
- backup.write(entries)
- backup.close()
- disk.setLabel(getDriveOne(), "Backup of computer "..id)
- local programHandle = fs.open("disk/startup", "w")
- programHandle.write(restoreProgram)
- programHandle.close()
- disk.eject(getDriveOne())
- end
- local function handleIncomingPackets()
- for i,v in ipairs(rs.getSides()) do
- rednet.open(v)
- end
- while true do
- id, message = rednet.receive()
- local packet = textutils.unserialize(message)
- if type(packet) == "table" then -- valid packet
- if packet[1] == "backup" then
- if not fs.exists("backups") then
- fs.makeDir("backups")
- end
- print("Received backup from computer "..tostring(id)..".")
- local backupHandle = fs.open("backups/"..tostring(id), "w")
- backupHandle.write(packet[2])
- backupHandle.close()
- end
- end
- end
- end
- local function handleInput()
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print("Please enter a backup ID:")
- local backup = read()
- if type(tonumber(backup)) == "number" then
- if fs.exists("backups/"..backup) then
- local handle = fs.open("backups/"..backup, "r")
- local files = handle.readAll()
- handle.close()
- writeBackupToDisk(files, backup)
- else
- print("That backup does not exist.")
- os.sleep(2)
- end
- else
- print("Please type in a valid ID.")
- os.sleep(2)
- end
- end
- end
- parallel.waitForAny(handleIncomingPackets, handleInput)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement