comparison src/luan/host/Backup.luan @ 1374:72b699bad1a4

add Backup.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 07 Jul 2019 22:49:36 -0600
parents
children ba1b4583c2d5
comparison
equal deleted inserted replaced
1373:372906d1d08b 1374:72b699bad1a4
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local ipairs = Luan.ipairs or error()
4 local Io = require "luan:Io.luan"
5 local uri = Io.uri or error()
6 local Thread = require "luan:Thread.luan"
7 local run_in_lock = Thread.run_in_lock or error()
8 local backup_write_lock = Thread.backup_write_lock or error()
9
10
11 local Backup = {}
12
13 local function backup_nonlocal(from,to)
14 to.mkdir()
15 for _, from_child in ipairs(from.children()) do
16 local name = from_child.name()
17 local to_child = to.child(name)
18 if from_child.is_directory() then
19 if name ~= "local" then
20 backup_nonlocal( from_child, to_child )
21 end
22 elseif from_child.is_file() then
23 to_child.link_to(from_child)
24 else
25 error(from_child.to_string().." isn't dir or file")
26 end
27 end
28 end
29
30 local function backup_all(from,to)
31 to.mkdir()
32 for _, from_child in ipairs(from.children()) do
33 local name = from_child.name()
34 local to_child = to.child(name)
35 if from_child.is_directory() then
36 backup_all( from_child, to_child )
37 elseif from_child.is_file() then
38 to_child.link_to(from_child)
39 else
40 error(from_child.to_string().." isn't dir or file")
41 end
42 end
43 end
44
45 local function backup_local(from,to)
46 local path = "site/private/local/backup"
47 from = from.child(path)
48 to = to.child(path)
49 if from.exists() then
50 from.is_directory() or error(from.to_string().." isn't dir")
51 backup_all(from,to)
52 end
53 end
54
55 local function backup(backup_dir)
56 local sites_dir = uri "file:sites"
57 backup_dir.mkdir()
58 for _, site_dir in ipairs(sites_dir.children()) do
59 local to = backup_dir.child(site_dir.name())
60 backup_nonlocal( site_dir, to )
61 backup_local( site_dir, to )
62 end
63 end
64
65 function Backup.backup(backup_dir)
66 run_in_lock( backup_write_lock, backup, backup_dir )
67 end
68
69 return Backup