Advertisement
Tatantyler

Backup Program

Sep 22nd, 2012
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.51 KB | None | 0 0
  1. args = {...}
  2.  
  3. local function getAllFilesInDir(dir, includeDirs)
  4.     dir = dir or ""
  5.     local files = {}
  6.     for i,v in ipairs(fs.list(dir)) do
  7.         if fs.isDir(dir.."/"..v) then
  8.             if string.sub(v, 1,4) ~= "disk" and v ~= "rom" then
  9.                 if includeDirs then
  10.                     table.insert(files, v)
  11.                 end
  12.                 for i2, v2 in ipairs(getAllFilesInDir(dir.."/"..v, includeDirs)) do
  13.                     table.insert(files, v.."/"..v2)
  14.                 end
  15.             end
  16.         else
  17.             table.insert(files, v)
  18.         end
  19.     end
  20.     return files
  21. end
  22.  
  23. local function getDriveOne()
  24.     for i,v in ipairs(rs.getSides()) do
  25.         if peripheral.isPresent(v) and peripheral.getType(v) == "drive" and disk.getMountPath(v) == "disk" then
  26.             return v
  27.         end
  28.     end
  29. end
  30.  
  31. local restoreProgram = [[
  32.  
  33. local function getDriveOne()
  34.     for i,v in ipairs(rs.getSides()) do
  35.         if peripheral.isPresent(v) and peripheral.getType(v) == "drive" and disk.getMountPath(v) == "disk" then
  36.             return v
  37.         end
  38.     end
  39. end
  40.  
  41. local function restore()
  42.     if fs.exists("disk/backup") then
  43.         local backup = fs.open("disk/backup", "r")
  44.         local fileTable = backup.readAll()
  45.         backup.close()
  46.         local fileTable = textutils.unserialize(fileTable)
  47.         print("This disk contains a stored backup of computer "..tostring(fileTable[1])..".")
  48.         if type(fileTable[4]) == "string" then
  49.             print("Label: "..fileTable[4]..".")
  50.             os.setComputerLabel(fileTable[4])
  51.             hasLabel = true
  52.         end
  53.         print("Total files: "..tostring(fileTable[3]))
  54.         print("Total file size: "..tostring(fileTable[2]))
  55.         while true do
  56.             print("Restore from backup? (Y/N) >")
  57.             local yn = read()
  58.             yn = string.upper(yn)
  59.             if yn == "Y" then
  60.                 break
  61.             elseif yn == "N" then
  62.                 return
  63.             else
  64.                 print("Please provide a valid response.")
  65.             end
  66.         end
  67.         os.sleep(1.5)
  68.         for i,v in ipairs(fileTable) do
  69.             if i ~= 1 and i~= 2 and i ~= 3 then
  70.                 term.clear()
  71.                 term.setCursorPos(1,1)
  72.                 print("Restoring file.")
  73.                 print("ID: "..v[1])
  74.                 print("Path: "..v[2])
  75.                 if v[3] then
  76.                     print("Is a directory.")
  77.                 else
  78.                     print("Is a file.")
  79.                     print("Size: "..tostring(v[4]))
  80.                 end
  81.                
  82.                 if v[3] then
  83.                     fs.makeDir(v[2])
  84.                 else
  85.                     local file = fs.open(v[2], "w")
  86.                     file.write(v[5])
  87.                     file.close()
  88.                 end
  89.                 os.sleep(0.5)
  90.             end
  91.         end
  92.         print("Restore complete.")
  93.         os.sleep(1)
  94.         disk.eject(getDriveOne())
  95.         os.reboot()
  96.     end
  97. end
  98.  
  99. restore()
  100. ]]
  101.  
  102. local function writeBackupToDisk(entries)
  103.     local backup = fs.open("disk/backup", "w")
  104.     backup.write(textutils.serialize(entries))
  105.     backup.close()
  106.     disk.setLabel(getDriveOne(), "Backup of computer "..tostring(os.computerID()))
  107.     local programHandle = fs.open("disk/startup", "w")
  108.     programHandle.write(restoreProgram)
  109.     programHandle.close()
  110.     disk.eject(getDriveOne())
  111. end
  112.  
  113. local function makeBackup()
  114.     local files = getAllFilesInDir("", true)
  115.     local entries = {}
  116.     local size = 0
  117.     for i,v in ipairs(files) do
  118.         if not fs.isDir(v) then
  119.             size = size + fs.getSize(v)
  120.         end
  121.     end
  122.     table.insert(entries, os.getComputerID())
  123.     table.insert(entries, size)
  124.     table.insert(entries, #files)
  125.     for i,v in ipairs(files) do
  126.         local fileEntry = {}
  127.         table.insert(fileEntry, i) --v[1]
  128.         table.insert(fileEntry, v) --v[2]
  129.         table.insert(fileEntry, fs.isDir(v)) --v[3]
  130.         if not fs.isDir(v) then
  131.             table.insert(fileEntry, fs.getSize(v)) --v[4]
  132.             local fileHandle = fs.open(v, "r")
  133.             table.insert(fileEntry, fileHandle.readAll()) --v[5]
  134.             fileHandle.close()
  135.         end
  136.         table.insert(entries, fileEntry)
  137.     end
  138.     return entries
  139. end
  140.  
  141. local function int_main()
  142.     if args[1] == "-disk" then
  143.         while true do
  144.             if getDriveOne() ~= nil then
  145.                 print("Starting backup to disk in drive on side: "..getDriveOne())
  146.                 entries = makeBackup()
  147.                 writeBackupToDisk(entries)
  148.                 break
  149.             else
  150.                 print("Please insert a disk.")
  151.                 os.pullEvent("disk")
  152.             end
  153.         end
  154.     end
  155.     if args[1] == "-rednet" then
  156.         entries = makeBackup()
  157.        
  158.         for i,v in ipairs(rs.getSides()) do
  159.             rednet.open(v)
  160.         end
  161.        
  162.         local packet = {}
  163.         table.insert(packet, "backup")
  164.         table.insert(packet, textutils.serialize(entries))
  165.         packet = textutils.serialize(packet)
  166.        
  167.         if args[2] == nil then
  168.             rednet.broadcast(packet)
  169.         else
  170.             rednet.send(tonumber(args[2]), packet)
  171.         end
  172.        
  173.         for i,v in ipairs(rs.getSides()) do
  174.             rednet.close(v)
  175.         end
  176.     end
  177.    
  178.     return 0
  179.    
  180. end
  181.  
  182. if #args >= 1 then
  183.     print("Starting backup with parameters: "..args[1])
  184.     int_main()
  185. else
  186.     print("USAGE: backup [-rednet/-disk] [backup server ID, if applicable]")
  187.     print("If no backup server ID is specified, the backup will be broadcast.")
  188. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement