2012-08-25 11:45:50 +00:00
|
|
|
--
|
2012-02-15 19:00:28 +00:00
|
|
|
-- default-rsyncssh.lua
|
|
|
|
--
|
|
|
|
-- Improved rsync - sync with rsync, but moves and deletes executed over ssh.
|
|
|
|
-- A (Layer 1) configuration.
|
|
|
|
--
|
|
|
|
-- Note:
|
2012-02-15 19:10:50 +00:00
|
|
|
-- this is infact just a configuration using Layer 1 configuration
|
2012-02-15 19:00:28 +00:00
|
|
|
-- like any other. It only gets compiled into the binary by default.
|
|
|
|
-- You can simply use a modified one, by copying everything into a
|
2012-02-15 19:10:50 +00:00
|
|
|
-- config file of yours and name it differently.
|
2012-02-15 19:00:28 +00:00
|
|
|
--
|
|
|
|
-- License: GPLv2 (see COPYING) or any later version
|
|
|
|
-- Authors: Axel Kittenberger <axkibe@gmail.com>
|
|
|
|
--
|
2012-08-25 11:45:50 +00:00
|
|
|
--
|
|
|
|
|
|
|
|
if not default then
|
2012-10-05 19:48:06 +00:00
|
|
|
error( 'default not loaded' );
|
2012-08-25 11:45:50 +00:00
|
|
|
end
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
if not default.rsync then
|
|
|
|
error( 'default.rsync not loaded' );
|
2012-08-25 11:45:50 +00:00
|
|
|
end
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
if default.rsyncssh then
|
|
|
|
error( 'default-rsyncssh already loaded' );
|
|
|
|
end
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
--
|
|
|
|
-- rsyncssh extends default.rsync
|
|
|
|
--
|
|
|
|
local rsyncssh = { default.rsync }
|
|
|
|
default.rsyncssh = rsyncssh
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
--
|
|
|
|
-- used to ensure there aren't typos in the keys
|
|
|
|
--
|
|
|
|
rsyncssh.checkgauge = {
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
-- unsets the inherited value of from default.rsync
|
2012-10-06 12:22:08 +00:00
|
|
|
target = false,
|
|
|
|
onMove = true,
|
2012-02-16 07:28:40 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
-- rsyncssh users host and targetdir
|
2012-10-06 12:22:08 +00:00
|
|
|
host = true,
|
|
|
|
targetdir = true,
|
|
|
|
sshExitCodes = true,
|
|
|
|
rsyncExitCodes = true,
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
-- ssh settings
|
|
|
|
ssh = {
|
2012-10-06 12:22:08 +00:00
|
|
|
binary = true,
|
|
|
|
port = true,
|
|
|
|
_extra = true
|
2012-10-05 19:48:06 +00:00
|
|
|
},
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
-- xargs settings
|
|
|
|
xargs = {
|
2012-10-06 12:22:08 +00:00
|
|
|
binary = true,
|
|
|
|
delimiter = true,
|
|
|
|
_extra = true
|
2012-10-05 19:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
--
|
|
|
|
-- Spawns rsync for a list of events
|
|
|
|
--
|
|
|
|
rsyncssh.action = function( inlet )
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
local event, event2 = inlet.getEvent()
|
|
|
|
local config = inlet.getConfig()
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
-- makes move local on target host
|
|
|
|
-- if the move fails, it deletes the source
|
|
|
|
if event.etype == 'Move' then
|
|
|
|
log('Normal', 'Moving ',event.path,' -> ',event2.path)
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
spawn(
|
|
|
|
event,
|
|
|
|
config.ssh.binary,
|
2012-10-08 06:06:34 +00:00
|
|
|
config.ssh._computed,
|
2012-10-05 19:48:06 +00:00
|
|
|
config.host,
|
|
|
|
'mv',
|
|
|
|
'\"' .. config.targetdir .. event.path .. '\"',
|
|
|
|
'\"' .. config.targetdir .. event2.path .. '\"',
|
|
|
|
'||', 'rm', '-rf',
|
|
|
|
'\"' .. config.targetdir .. event.path .. '\"')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- uses ssh to delete files on remote host
|
|
|
|
-- instead of constructing rsync filters
|
|
|
|
|
|
|
|
if event.etype == 'Delete' then
|
2012-10-09 15:47:39 +00:00
|
|
|
|
|
|
|
if
|
|
|
|
config.delete ~= true and
|
|
|
|
config.delete ~= 'running'
|
|
|
|
then
|
2012-10-05 19:48:06 +00:00
|
|
|
inlet.discardEvent(event)
|
2012-02-15 19:00:28 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
-- gets all other deletes ready to be
|
|
|
|
-- executed
|
2012-02-15 19:00:28 +00:00
|
|
|
local elist = inlet.getEvents(
|
2012-10-05 19:48:06 +00:00
|
|
|
function( e )
|
|
|
|
return e.etype == 'Delete'
|
2012-08-25 11:45:50 +00:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
-- returns the paths of the delete list
|
|
|
|
local paths = elist.getPaths(
|
|
|
|
function( etype, path1, path2 )
|
|
|
|
if path2 then
|
|
|
|
return config.targetdir..path1, config.targetdir..path2
|
|
|
|
else
|
|
|
|
return config.targetdir..path1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
-- ensures none of the paths is '/'
|
|
|
|
for _, v in pairs( paths ) do
|
|
|
|
if string.match(v, '^%s*/+%s*$') then
|
|
|
|
log('Error', 'refusing to `rm -rf /` the target!')
|
|
|
|
terminate(-1) -- ERRNO
|
2012-02-15 19:00:28 +00:00
|
|
|
end
|
|
|
|
end
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
log(
|
|
|
|
'Normal',
|
|
|
|
'Deleting list\n',
|
|
|
|
table.concat( paths, '\n' )
|
|
|
|
)
|
|
|
|
|
|
|
|
local params = { }
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-02-15 19:00:28 +00:00
|
|
|
spawn(
|
2012-08-25 11:45:50 +00:00
|
|
|
elist,
|
2012-10-05 19:48:06 +00:00
|
|
|
config.ssh.binary,
|
|
|
|
'<', table.concat(paths, config.xargs.delimiter),
|
|
|
|
params,
|
2012-10-08 06:06:34 +00:00
|
|
|
config.ssh._computed,
|
2012-10-05 19:48:06 +00:00
|
|
|
config.host,
|
|
|
|
config.xargs.binary,
|
|
|
|
config.xargs._extra
|
2012-02-15 19:00:28 +00:00
|
|
|
)
|
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-02-15 19:00:28 +00:00
|
|
|
--
|
2012-10-05 19:48:06 +00:00
|
|
|
-- for everything else a rsync is spawned
|
|
|
|
--
|
|
|
|
local elist = inlet.getEvents(
|
|
|
|
function(e)
|
|
|
|
-- TODO use a table
|
|
|
|
return e.etype ~= 'Move' and
|
|
|
|
e.etype ~= 'Delete' and
|
|
|
|
e.etype ~= 'Init' and
|
|
|
|
e.etype ~= 'Blanket'
|
|
|
|
end
|
|
|
|
)
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
local paths = elist.getPaths( )
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
--
|
|
|
|
-- removes trailing slashes from dirs.
|
|
|
|
--
|
|
|
|
for k, v in ipairs( paths ) do
|
|
|
|
if string.byte(v, -1) == 47 then
|
|
|
|
paths[k] = string.sub(v, 1, -2)
|
|
|
|
end
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
end
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
local sPaths = table.concat(paths, '\n')
|
|
|
|
local zPaths = table.concat(paths, '\000')
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
log('Normal', 'Rsyncing list\n', sPaths)
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
spawn(
|
|
|
|
elist,
|
|
|
|
config.rsync.binary,
|
|
|
|
'<', zPaths,
|
|
|
|
config.rsync._computed,
|
|
|
|
'--from0',
|
|
|
|
'--files-from=-',
|
|
|
|
config.source,
|
|
|
|
config.host .. ':' .. config.targetdir
|
|
|
|
)
|
|
|
|
end
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
-----
|
|
|
|
-- Called when collecting a finished child process
|
|
|
|
--
|
|
|
|
rsyncssh.collect = function( agent, exitcode )
|
|
|
|
|
|
|
|
local config = agent.config
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
if not agent.isList and agent.etype == 'Init' then
|
|
|
|
local rc = config.rsyncExitCodes[exitcode]
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
if rc == 'ok' then
|
|
|
|
log('Normal', 'Startup of "',agent.source,'" finished: ', exitcode)
|
|
|
|
elseif rc == 'again' then
|
2012-11-23 10:03:22 +00:00
|
|
|
if settings('insist') then
|
2012-10-05 19:48:06 +00:00
|
|
|
log('Normal', 'Retrying startup of "',agent.source,'": ', exitcode)
|
2012-02-15 19:00:28 +00:00
|
|
|
else
|
2012-10-05 19:48:06 +00:00
|
|
|
log('Error', 'Temporary or permanent failure on startup of "',
|
|
|
|
agent.source, '". Terminating since "insist" is not set.');
|
|
|
|
terminate(-1) -- ERRNO
|
2012-02-15 19:00:28 +00:00
|
|
|
end
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
elseif rc == 'die' then
|
|
|
|
log('Error', 'Failure on startup of "',agent.source,'": ', exitcode)
|
2012-02-15 19:00:28 +00:00
|
|
|
else
|
2012-10-05 19:48:06 +00:00
|
|
|
log('Error', 'Unknown exitcode on startup of "', agent.source,': "',exitcode)
|
|
|
|
rc = 'die'
|
|
|
|
end
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
return rc
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
end
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
if agent.isList then
|
|
|
|
local rc = config.rsyncExitCodes[exitcode]
|
|
|
|
if rc == 'ok' then
|
|
|
|
log('Normal', 'Finished (list): ',exitcode)
|
|
|
|
elseif rc == 'again' then
|
|
|
|
log('Normal', 'Retrying (list): ',exitcode)
|
|
|
|
elseif rc == 'die' then
|
|
|
|
log('Error', 'Failure (list): ', exitcode)
|
|
|
|
else
|
|
|
|
log('Error', 'Unknown exitcode (list): ',exitcode)
|
|
|
|
rc = 'die'
|
|
|
|
end
|
|
|
|
return rc
|
|
|
|
else
|
|
|
|
local rc = config.sshExitCodes[exitcode]
|
|
|
|
|
|
|
|
if rc == 'ok' then
|
|
|
|
log('Normal', 'Finished ',agent.etype,' ',agent.sourcePath,': ',exitcode)
|
|
|
|
elseif rc == 'again' then
|
|
|
|
log('Normal', 'Retrying ',agent.etype,' ',agent.sourcePath,': ',exitcode)
|
|
|
|
elseif rc == 'die' then
|
|
|
|
log('Normal', 'Failure ',agent.etype,' ',agent.sourcePath,': ',exitcode)
|
|
|
|
else
|
|
|
|
log('Error', 'Unknown exitcode ',agent.etype,' ',agent.sourcePath,': ',exitcode)
|
|
|
|
rc = 'die'
|
2012-02-15 19:00:28 +00:00
|
|
|
end
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
return rc
|
|
|
|
end
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
end
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
--
|
|
|
|
-- checks the configuration.
|
|
|
|
--
|
2012-10-06 12:22:08 +00:00
|
|
|
rsyncssh.prepare = function( config, level )
|
2012-08-25 11:45:50 +00:00
|
|
|
|
2012-10-06 12:22:08 +00:00
|
|
|
default.rsync.prepare( config, level + 1, true )
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
if not config.host then
|
2012-10-08 06:06:34 +00:00
|
|
|
error(
|
|
|
|
'default.rsyncssh needs "host" configured',
|
|
|
|
level
|
|
|
|
)
|
2012-10-05 19:48:06 +00:00
|
|
|
end
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
if not config.targetdir then
|
2012-10-08 06:06:34 +00:00
|
|
|
error(
|
|
|
|
'default.rsyncssh needs "targetdir" configured',
|
|
|
|
level
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
--
|
|
|
|
-- computes the ssh options
|
|
|
|
--
|
|
|
|
if config.ssh._computed then
|
|
|
|
error(
|
|
|
|
'please do not use the internal rsync._computed parameter',
|
|
|
|
level
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2012-11-23 11:01:21 +00:00
|
|
|
local cssh = config.ssh;
|
2012-10-08 06:06:34 +00:00
|
|
|
cssh._computed = { }
|
|
|
|
local computed = cssh._computed
|
|
|
|
local computedN = 1
|
|
|
|
|
|
|
|
if cssh._extra then
|
|
|
|
for k, v in ipairs( cssh._extra ) do
|
|
|
|
computed[ computedN ] = v
|
|
|
|
computedN = computedN + 1
|
|
|
|
end
|
2012-10-05 19:48:06 +00:00
|
|
|
end
|
2012-10-01 19:36:23 +00:00
|
|
|
|
2012-10-08 06:06:34 +00:00
|
|
|
if cssh.port then
|
|
|
|
computed[ computedN ] = '-p'
|
|
|
|
computed[ computedN + 1 ] = cssh.port
|
|
|
|
computedN = computedN + 2
|
2012-11-23 15:05:15 +00:00
|
|
|
|
|
|
|
local rsyncc = config.rsync._computed
|
|
|
|
rsyncc[ #rsyncc ] = '--rsh=ssh -p ' .. cssh.port
|
2012-10-05 19:48:06 +00:00
|
|
|
end
|
2012-10-01 19:36:23 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
-- appends a slash to the targetdir if missing
|
|
|
|
if string.sub(config.targetdir, -1) ~= '/' then
|
|
|
|
config.targetdir = config.targetdir .. '/'
|
|
|
|
end
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
end
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
--
|
|
|
|
-- allow processes
|
|
|
|
--
|
|
|
|
rsyncssh.maxProcesses = 1
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
--
|
|
|
|
-- The core should not split move events
|
|
|
|
--
|
|
|
|
rsyncssh.onMove = true
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
--
|
|
|
|
-- default delay
|
|
|
|
--
|
|
|
|
rsyncssh.delay = 15
|
2012-02-15 19:00:28 +00:00
|
|
|
|
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
--
|
|
|
|
-- no default exit codes
|
|
|
|
--
|
|
|
|
rsyncssh.exitcodes = false
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
--
|
|
|
|
-- rsync exit codes
|
|
|
|
--
|
|
|
|
rsyncssh.rsyncExitCodes = default.rsyncExitCodes
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-05 19:48:06 +00:00
|
|
|
--
|
|
|
|
-- ssh exit codes
|
|
|
|
--
|
|
|
|
rsyncssh.sshExitCodes = default.sshExitCodes
|
|
|
|
|
|
|
|
--
|
|
|
|
-- xargs calls configuration
|
|
|
|
--
|
|
|
|
-- xargs is used to delete multiple remote files, when ssh access is
|
|
|
|
-- available this is simpler than to build filters for rsync for this.
|
|
|
|
--
|
|
|
|
rsyncssh.xargs = {
|
2012-02-16 07:28:40 +00:00
|
|
|
|
2012-08-25 11:45:50 +00:00
|
|
|
--
|
2012-10-05 19:48:06 +00:00
|
|
|
-- the binary called (on target host)
|
|
|
|
binary = '/usr/bin/xargs',
|
2012-02-16 07:28:40 +00:00
|
|
|
|
2012-08-25 11:45:50 +00:00
|
|
|
--
|
2012-10-05 19:48:06 +00:00
|
|
|
-- delimiter, uses null by default, you might want to override this for older
|
|
|
|
-- by for example '\n'
|
|
|
|
delimiter = '\000',
|
|
|
|
|
2012-02-15 19:00:28 +00:00
|
|
|
--
|
2012-10-05 19:48:06 +00:00
|
|
|
-- extra parameters
|
|
|
|
_extra = { '-0', 'rm -rf' }
|
|
|
|
}
|
|
|
|
|
|
|
|
--
|
|
|
|
-- ssh calls configuration
|
|
|
|
--
|
|
|
|
-- ssh is used to move and delete files on the target host
|
|
|
|
--
|
|
|
|
rsyncssh.ssh = {
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-08-25 11:45:50 +00:00
|
|
|
--
|
2012-10-05 19:48:06 +00:00
|
|
|
-- the binary called
|
2012-02-16 07:28:40 +00:00
|
|
|
--
|
2012-10-05 19:48:06 +00:00
|
|
|
binary = '/usr/bin/ssh',
|
2012-02-16 07:28:40 +00:00
|
|
|
|
2012-08-25 11:45:50 +00:00
|
|
|
--
|
2012-10-05 19:48:06 +00:00
|
|
|
-- if set connect to this port
|
2012-02-15 19:00:28 +00:00
|
|
|
--
|
2012-10-05 19:48:06 +00:00
|
|
|
port = nil,
|
2012-08-25 11:45:50 +00:00
|
|
|
|
|
|
|
--
|
2012-10-05 19:48:06 +00:00
|
|
|
-- extra parameters
|
2012-08-25 11:45:50 +00:00
|
|
|
--
|
2012-10-05 19:48:06 +00:00
|
|
|
_extra = { }
|
2012-02-15 19:00:28 +00:00
|
|
|
}
|
2012-10-05 19:48:06 +00:00
|
|
|
|