NIU: Backup system settings
[project/luci.git] / modules / niu / luasrc / controller / niu / system.lua
1 --[[
2 LuCI - Lua Development Framework
3
4 Copyright 2009 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local require, pairs, unpack = require, pairs, unpack
16 module "luci.controller.niu.system"
17
18 function index()
19         entry({"niu", "system"}, nil, "System").dbtemplate = "niu/system"
20
21         entry({"niu", "system", "general"}, 
22         cbi("niu/system/general", {on_success_to={"niu"}}), "General", 10)
23         
24         entry({"niu", "system", "backup"}, call("backup"), "Backup Settings", 20)
25 end
26
27 function backup()
28         local os = require "os"
29         local uci = require "luci.model.uci".cursor()
30         local nixio, nutl = require "nixio", require "nixio.util"
31         local fs = require "nixio.fs"
32         local http = require "luci.http"
33         
34         
35         local call = {"/bin/tar", "-cz"}
36         for k, v in pairs(uci:get_all("luci", "flash_keep")) do
37                 if k:byte() ~= 46 then  -- k[1] ~= "."
38                         nutl.consume(fs.glob(v), call)
39                 end
40         end
41         
42         
43         http.header(
44                 'Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
45                         nixio.uname().nodename, os.date("%Y-%m-%d")
46                 }
47         )
48         http.prepare_content("application/x-targz")
49         
50         
51         local fdin, fdout = nixio.pipe()
52         local devnull = nixio.open("/dev/null", "r+")
53         local proc = nixio.fork()
54         
55         if proc == 0 then
56                 fdin:close()
57                 nixio.dup(devnull, nixio.stdin)
58                 nixio.dup(devnull, nixio.stderr)
59                 nixio.dup(fdout, nixio.stdout)
60                 nixio.exec(unpack(call))
61                 os.exit(1)
62         end
63         
64         fdout:close()
65         http.splice(fdin)
66         http.close()
67 end