This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kingdaro's profile picture

Backup Script to Run in the Background

Started by Kingdaro, 16 December 2012 - 04:20 PM
Kingdaro #1
Posted 16 December 2012 - 05:20 PM
On a server for me and a friend, I thought it'd be nice to have a convenient backup script running in the background of the computer, backing up all the files to a disk every five minutes or so.

I haven't messed with coroutines very much and my logic involving them only reaches the parallel API. Here's my current code:


local args = {...}
if #args < 1 then
  print 'Usage: backup <side>'
  return
end

local backup = disk.getMountPath(args[1])

for _, path in pairs(fs.list('/')) do
  if not path:find('rom')
  and not path:find('disk')
  and not path:find('backup') then
    local newpath = fs.combine(backup, path)

    if fs.exists(newpath) then
      fs.delete(newpath)
    end

    fs.copy(path, newpath)

    print('Copied '..path)
  end
end

Any way to make this run in the background every five minutes? (without the "copied blahblah" messages, of course)</side>
KaoS #2
Posted 17 December 2012 - 03:18 AM
ok, you would have to run it in parallel with the shell


local a=coroutine.wrap(function() os.run({},'rom/programs/shell') end)
local b=coroutine.wrap(function() while true do --EXECUTE BACKUP-- sleep(delay) end end)
local evts={}
while true do
  a(unpack(evts))
  b(unpack(evts))
  evts={os.pullEvent()}
end

EDIT: I realize that most people use the parallel api, I don't but if you wanted to you would do this


local function a() os.run({},'rom/programs/shell') end
local function b() while true do --EXECUTE BACKUP-- sleep(delay) end end
parallel.waitForAny(a,B)/>
Kingdaro #3
Posted 17 December 2012 - 11:34 AM
I sort of wanted to avoid doing that, because it's weird having a shell running within a shell. But if it's the only option, that's fine.

Or is that what the os.run prevents? If so, that's really good to know for a number of reasons.
KaoS #4
Posted 17 December 2012 - 03:17 PM
well there are ways of altering error reporting functions and crashing the shell so you have no parent shell, this comes with numerous issues that are avoided by os.run. if you os.run it then all of your global vars are passed on to the new shell and so the new shell then notices this and does not run startup again (otherwise you could not do this in a startup program without infinite loops that have a scary effect on your system resources), this can also be taken care of within the shell crashing system I guess though…

basically it gets a lot more complicated when trying to have only one shell running, if you still want to do that (just like me:) perfectionist) let me know and I will write up an example of killing shell and starting a new instance