Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- args = {...}
- local function getAllFilesInDir(dir, includeDirs)
- dir = dir or ""
- local files = {}
- for i,v in ipairs(fs.list(dir)) do
- if fs.isDir(dir.."/"..v) then
- if string.sub(v, 1,4) ~= "disk" and v ~= "rom" then
- if includeDirs then
- table.insert(files, v)
- end
- for i2, v2 in ipairs(getAllFilesInDir(dir.."/"..v, includeDirs)) do
- table.insert(files, v.."/"..v2)
- end
- end
- else
- table.insert(files, v)
- end
- end
- return files
- end
- 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)
- local backup = fs.open("disk/backup", "w")
- backup.write(textutils.serialize(entries))
- backup.close()
- disk.setLabel(getDriveOne(), "Backup of computer "..tostring(os.computerID()))
- local programHandle = fs.open("disk/startup", "w")
- programHandle.write(restoreProgram)
- programHandle.close()
- disk.eject(getDriveOne())
- end
- local function makeBackup()
- local files = getAllFilesInDir("", true)
- local entries = {}
- local size = 0
- for i,v in ipairs(files) do
- if not fs.isDir(v) then
- size = size + fs.getSize(v)
- end
- end
- table.insert(entries, os.getComputerID())
- table.insert(entries, size)
- table.insert(entries, #files)
- for i,v in ipairs(files) do
- local fileEntry = {}
- table.insert(fileEntry, i) --v[1]
- table.insert(fileEntry, v) --v[2]
- table.insert(fileEntry, fs.isDir(v)) --v[3]
- if not fs.isDir(v) then
- table.insert(fileEntry, fs.getSize(v)) --v[4]
- local fileHandle = fs.open(v, "r")
- table.insert(fileEntry, fileHandle.readAll()) --v[5]
- fileHandle.close()
- end
- table.insert(entries, fileEntry)
- end
- return entries
- end
- local function int_main()
- if args[1] == "-disk" then
- while true do
- if getDriveOne() ~= nil then
- print("Starting backup to disk in drive on side: "..getDriveOne())
- entries = makeBackup()
- writeBackupToDisk(entries)
- break
- else
- print("Please insert a disk.")
- os.pullEvent("disk")
- end
- end
- end
- if args[1] == "-rednet" then
- entries = makeBackup()
- for i,v in ipairs(rs.getSides()) do
- rednet.open(v)
- end
- local packet = {}
- table.insert(packet, "backup")
- table.insert(packet, textutils.serialize(entries))
- packet = textutils.serialize(packet)
- if args[2] == nil then
- rednet.broadcast(packet)
- else
- rednet.send(tonumber(args[2]), packet)
- end
- for i,v in ipairs(rs.getSides()) do
- rednet.close(v)
- end
- end
- return 0
- end
- if #args >= 1 then
- print("Starting backup with parameters: "..args[1])
- int_main()
- else
- print("USAGE: backup [-rednet/-disk] [backup server ID, if applicable]")
- print("If no backup server ID is specified, the backup will be broadcast.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement