forked from lsyncd/lsyncd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault-rsync.lua
185 lines (167 loc) · 4.42 KB
/
default-rsync.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- default-rsync.lua
--
-- Syncs with rsync ("classic" Lsyncd)
-- A (Layer 1) configuration.
--
-- Note:
-- this is infact just a configuration using Layer 1 configuration
-- like any other. It only gets compiled into the binary by default.
-- You can simply use a modified one, by copying everything into a
-- config file of yours and name it differently.
--
-- License: GPLv2 (see COPYING) or any later version
-- Authors: Axel Kittenberger <[email protected]>
--
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if not default then error('default not loaded'); end
if default.rsync then error('default-rsync already loaded'); end
default.rsync = {
-----
-- Spawns rsync for a list of events
--
action = function(inlet)
-- gets all events ready for syncing
local elist = inlet.getEvents(
function(event)
return event.etype ~= 'Init' and event.etype ~= 'Blanket'
end
)
-----
-- replaces filter rule by literals
--
local function sub(p)
if not p then
return
end
return p:gsub('%?', '\\?'):
gsub('%*', '\\*'):
gsub('%[', '\\['):
gsub('%]', '\\]')
end
local paths = elist.getPaths(
function(etype, path1, path2)
if string.byte(path1, -1) == 47 and etype == 'Delete' then
return sub(path1)..'***', sub(path2)
else
return sub(path1), sub(path2)
end
end)
-- stores all filters with integer index
-- local filterI = inlet.getExcludes();
local filterI = {}
-- stores all filters with path index
local filterP = {}
-- adds one entry into the filter
-- @param path ... path to add
-- @param leaf ... true if this the original path
-- false if its a parent
local function addToFilter(path)
if filterP[path] then
return
end
filterP[path]=true
table.insert(filterI, path)
end
-- adds a path to the filter, for rsync this needs
-- to have entries for all steps in the path, so the file
-- d1/d2/d3/f1 needs filters
-- 'd1/', 'd1/d2/', 'd1/d2/d3/' and 'd1/d2/d3/f1'
for _, path in ipairs(paths) do
if path and path ~= '' then
addToFilter(path)
local pp = string.match(path, '^(.*/)[^/]+/?')
while pp do
addToFilter(pp)
pp = string.match(pp, '^(.*/)[^/]+/?')
end
end
end
local filterS = table.concat(filterI, '\n')
local filter0 = table.concat(filterI, '\000')
log('Normal', 'Calling rsync with filter-list of new/modified files/dirs\n', filterS)
local config = inlet.getConfig()
local delete = nil
if config.delete then delete = { '--delete', '--ignore-errors' }; end
spawn(elist, config.rsyncBinary,
'<', filter0,
config.rsyncOpts,
'-r',
delete,
'--force',
'--from0',
'--include-from=-',
'--exclude=*',
config.source,
config.target)
end,
-----
-- Spawns the recursive startup sync
--
init = function(event)
local config = event.config
local inlet = event.inlet
local excludes = inlet.getExcludes()
local delete = nil
if config.delete then delete = { '--delete', '--ignore-errors' }; end
if #excludes == 0 then
log('Normal', 'recursive startup rsync: ', config.source, ' -> ', config.target)
spawn(event, config.rsyncBinary,
delete,
config.rsyncOpts,
'-r',
config.source,
config.target)
else
local exS = table.concat(excludes, '\n')
log('Normal', 'recursive startup rsync: ',config.source,
' -> ',config.target,' excluding\n',exS)
spawn(event, config.rsyncBinary,
'<', exS,
'--exclude-from=-',
delete,
config.rsyncOpts,
'-r',
config.source,
config.target)
end
end,
-----
-- Checks the configuration.
--
prepare = function(config)
if not config.target then
error('default.rsync needs "target" configured', 4)
end
if config.rsyncOps then
error('did you mean rsyncOpts with "t"?', 4)
end
-- appends a / to target if not present
if string.sub(config.target, -1) ~= '/' then
config.target = config.target..'/'
end
end,
-----
-- rsync uses default collect
----
-----
-- By default do deletes.
--
delete = true,
-----
-- The rsync binary to be called.
--
rsyncBinary = '/usr/bin/rsync',
-----
-- Calls rsync with this default short opts.
--
rsyncOpts = '-lts',
-----
-- Exit codes for rsync.
--
exitcodes = default.rsyncExitCodes,
-----
-- Default delay
--
delay = 15,
}