File: lftp.lua

package info (click to toggle)
lsyncd 2.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 484 kB
  • sloc: ansic: 2,640; sh: 81; makefile: 11
file content (163 lines) | stat: -rw-r--r-- 3,376 bytes parent folder | download | duplicates (2)
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
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- User configuration file for lsyncd.
--
--    Syncs with 'lftp'.
--
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lftp = {

	-----
	-- 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 config = inlet.getConfig()

		local commands = elist.getPaths(
			function(etype, path1, path2)
				if etype == 'Delete' then
					if string.byte(path1, -1) == 47 then
						return 'rm -r '..
							config.targetdir..sub(path1)
					else
						return 'rm '..
							config.targetdir..sub(path1)
					end
				elseif
					etype == 'Create' or
					etype == 'Modify' or
		 			etype == 'Attrib'
		 		then
					if string.byte(path1, -1) == 47 then
						return 'mirror -R '..
							config.source..sub(path1)..' '..
							config.targetdir..sub(path1)
					else
						return 'put '..
							config.source..sub(path1)..
							' -o '..config.targetdir..sub(path1)
					end
				end
			end
		)

		if #commands == 0 then
			spawn(elist, '/bin/true')
			return
		end

		commands = table.concat(commands, ';\n')

		log('Normal', 'Calling lftp with commands\n', commands)

		spawn(elist, '/usr/bin/lftp',
			'<', commands,
			'-u', config.user..','..config.pass, config.host
		)
	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
			error('lftp does not work with excludes', 4)
		end

		log('Normal', 'recursive startup lftp: ', config.source, ' to host: ', config.host)

		spawn(event, '/usr/bin/lftp',
			'-c',
			'open -u '..config.user..','..config.pass..' '..config.host..'; '..
			'mirror -R -e '..config.source..' '..config.targetdir..';'
		)
	end,

	-----
	-- Checks the configuration.
	--
	prepare = function(config)

		if not config.host then
			error('lftps needs "host" configured', 4);
		end

		if not config.user then
			error('lftps needs "user" configured', 4);
		end

		if not config.pass then
			error('lftps needs "pass" configured', 4);
		end

		if not config.targetdir then
			error('lftp needs "targetdir" configured', 4)
		end

		if config.target then
			error('lftp needs NOT "target" configured', 4)
		end

		if config.exclude then
			error('lftp does not work with excludes', 4)
		end

		if config.rsyncOpts then
			error('lftp needs NOT "rsyncOpts" configured', 4)
		end

		if string.sub(config.targetdir, -1) == '/' then
			error('please make targetdir not end with a /', 4)
		end

	end,

	-----
	-- Exit codes for rsync.
	--
	exitcodes = {
		[  0] = 'ok',
		[  1] = 'ok',
	},

	-----
	-- Default delay
	--
	delay = 1,
}

sync{
	lftp,
	host      = 'localhost',
	user      = 'test',
	pass      = 'test',
	source    = 'src',
	targetdir = '.',
}