rsync.lua

---Rsync Service.
[email protected] rsync
rsync={
	servers={},
	clients={},
}

---RSync Server
[email protected] port integer TCP Port server is configured to listen on.
[email protected] rsyncserver

---RSync Server Directory.
[email protected] path string Name of directory. Do not include / in the name.
[email protected] localpath string Local Path to map to.
[email protected] rsyncdir

---RSync User.
[email protected] username string
[email protected] password string
[email protected] rsyncuser

---Add a new RSync Server.
[email protected] port integer
[email protected] rsyncserver
[email protected] local rsyncserver = rsync:AddServer(9999)
function rsync:AddServer(port)
--	if rsync.servers[port] then return rsync.servers[port] end
	local rsyncserver = {port=port, dirs={}}
	---Add Directory to RSync server.
	[email protected] rsyncdir
	[email protected] rsyncdir
	[email protected] local rsyncserver = rsync:AddServer(9999)
	--rsyncserver:AddDir{path='/test', localpath='/tmp/test'}
	function rsyncserver:AddDir(rsyncdir)
		if not rsyncdir.users then rsyncdir.users = {} end
		---Add User to RSync directory.
		[email protected] rsyncuser
		[email protected] rsyncuser
		[email protected] local rsyncserver = rsync:AddServer(9999)
		-- local ServerDir = rsyncserver:AddDir{path='/test', localpath='/tmp/test'}
		-- local ServerDir:AddUser{username='test', password='test'}
		function rsyncdir:AddUser(rsyncuser)
			self.users[rsyncuser.username] = rsyncuser
		end
		self.dirs[rsyncdir.path] = rsyncdir
		return rsyncdir
	end
	rsync.servers[port] = rsyncserver
	return rsyncserver
end

function install_container()
	print("Installing Rsync.")
	install_package("rsync")
	return 0
end

function apply_config()
	mkdir("/etc/rsync")
	for _,server in pairs(rsync.servers) do
		debug_print('apply_config', "RSync Server " .. server.port)
		local config = ""
		config = config .. "pid file=/run/rsync." .. server.port .. ".pid\n"
		config = config .. "lock file=/run/rsync." .. server.port .. ".lock\n"
		config = config .. "log file=/tmp/rsync." .. server.port .. ".log\n"
		config = config .. "port=" .. server.port .. "\n\n"
		for _, dir in pairs(server.dirs) do
			config = config .. "[" .. dir.path .. "]\n"
			config = config .. "path=" .. dir.localpath .. "\n"
			if dir.comment then config = config .. "comment=" .. dir.comment .. "\n" end
			if dir.readonly then config = config .. "readonly=true\n" else config = config .. "readonly=false\n" end
			config = config .. "timeout=60\n"
			local userlist = ""
			local passwordlist = ""
			for _, user in pairs(dir.users) do
				if (userlist:len() > 0) then userlist = userlist .. "," end
				userlist = userlist .. user.username
				passwordlist = passwordlist .. user.username .. ":" .. user.password .. "\n"
			end
			if (userlist:len() > 0) then
				config = config .. "auth users=" .. userlist .. "\n"
				config = config .. "secrets file=/etc/rsync/server." .. server.port .. ".pwd\n"
				write_file('/etc/rsync/server.' .. server.port .. '.pwd', passwordlist)
				exec('chmod 0600 /etc/rsync/server.' .. server.port .. '.pwd')
			end
		end
		write_file('/etc/rsync/server.' .. server.port .. '.conf', config)
	end
	return 0
end

function background()
	for _,server in pairs(rsync.servers) do
		for _, dir in pairs(server.dirs) do
			exec("mkdir -p " .. dir.localpath .. " && chmod 0777 " .. dir.localpath)
		end
		exec('rsync --daemon --config /etc/rsync/server.' .. server.port .. '.conf  &')
	end
	return 0
end
generated by LDoc 1.4.6 Last updated 2019-03-17 07:38:48