1

我正在尝试将 lsyncd 配置为同步登录用户主页上的文件夹,但是当我尝试捕获他的值时$HOMEos.getenv("HOME")结果总是

错误准备 /etc/lsyncd/lsyncd.conf.lua: /etc/lsyncd/lsyncd.conf.lua: 14: 尝试连接一个 nil 值

我试过os.getenv("PWD")了,它运行时没有显示错误,但它不起作用,在/var/log/lsyncd/lsyncd.status文件中我看到它尝试使用/.mozilla/firefox地址,就好像 PWD 为空一样。

我试图通过运行命令sudo -E service lsyncd restart和修改 sudoers来保留环境变量Defaults env_keep +="HOME",但都是徒劳的。
有任何想法吗?

我附上我的代码:

settings{
    logfile = "/var/log/lsyncd/lsyncd.log",
    statusFile = "/var/log/lsyncd/lsyncd.status",
}

sync{
        default.rsync,
        source =  os.getenv("HOME").."/.mozilla/firefox/",
        target =  "tmp/.mozilla/firefox/",
        delay  = 1,
}
4

2 回答 2

1

Linux does not have a concept of “the logged-in user”. It has a concept of a logged-in user, but there can be many of them at the same time.

You're running a system service, through systemd. Telling sudo to preserve the HOME environment variable means that when you run sudo -E service lsyncd restart, the service command runs with HOME set to the home directory of the user who called sudo. But that has nothing to do with the value of HOME for the service. Systemd does not set the environment of a service based on the environment of the administrative command to start it, they're based on the service configuration.

If you want to synchronize a specific user's files, then hard-code the path to that user's home directory, or use getpwuid or getpwnam to look up the home directory of a user.

If you want to synchronize the files of whichever user is logged in, then don't use a system service. Instead, run lsyncd as part of that user's login session.

于 2017-12-29T14:26:52.980 回答
0

谢谢吉尔斯的回答。不幸的是 getpwuid 和 getpwnam 返回“nil”值,相当于 null de lua。我开发的解决方案允许我在与活动目录同步的多用户和多设备环境中运行 lsyncd,这样我就可以在活动目录中保存 Firefox 标记、谷歌浏览器、svn 设置、filezilla、历史...目录并为任何用户在任何计算机上恢复它们​​。与使用活动目录的 samba 方言 cifs 和 linux 之间的符号链接的兼容性问题使这变得复杂。我的解决方案如下:(仅适用于 Firefox,节省空间,但可以扩展到任何软件或文件夹。配置。)

1-我安装 lsyncd 并将该脚本添加到 /etc/lsyncd/lsyncd.conf.lua (该脚本查找刚刚登录到 /tmp/home 文件的用户的主页,并使用它来创建 url要同步的源和目标。)

function file_exists(file)
  local f = io.open(file, "rb")
  if f then f:close() end
  return f ~= nil
end

-- get all lines from a file, returns an empty 
-- list/table if the file does not exist
function lines_from(file)
  if not file_exists(file) then return {} end
  lines = {}
  for line in io.lines(file) do
    lines[#lines + 1] = line
  end
  return lines
end

-- tests the functions above
local file = '/tmp/lua'
local lines = lines_from(file)

-- print all line numbers and their contents
for k,v in pairs(lines) do
  print('line[' .. k .. ']', v)

end
--save line 1 in home
home = lines[1]


settings{

    logfile = "/var/log/lsyncd/lsyncd.log",
    statusFile = "/var/log/lsyncd/lsyncd.status",
}

sync{

        default.rsync,
-- Add home to url of the source
        source =  home.."/.mozilla/firefox/",
--Add home to url of the target
        target =  home.."/box/.mozilla/firefox/",
        delay  = 1,
        rsync={
                perms = true,
                owner = true,


        },
}

2- 使用 visudo,我允许我想要的用户(在我的情况下是所有用户)使用 sudo 命令在没有密码的情况下运行 lsyncd:ALL ALL=NOPASSWD:SETENV: /usr/sbin/service lsyncd *

3-我在 /etc/profile 文件中添加以下行

//Delete .mozilla in user's local home in start
rm -Rf $HOME/.mozilla
#mkdir -p $HOME/box/.mozilla/Firefox
//Box is the folder shared with the server
mkdir -p $HOME/box/.config
rm -rf $HOME/box/.config/caja
//2> /dev/null prevents an error due to any other cause from being shown to the user and prevents the user from starting up.
cp -rf $HOME/box/.mozilla  $HOME/  2> /dev/null
cp -rf $HOME/box/.config $HOME/ 2> /dev/null
cp -rf $HOME/box/.subversion $HOME/ 2> /dev/null
echo $HOME >> /tmp/lua;
sudo service lsyncd restart; rm -r /tmp/lua
于 2018-03-21T11:15:52.223 回答