【问题标题】:Why is getenv ("HOME") equal to nil in lsyncd.conf?为什么 lsyncd.conf 中的 getenv ("HOME") 等于 nil?
【发布时间】:2018-03-21 19:15:52
【问题描述】:

我正在尝试配置 lsyncd 以同步登录用户主页上的文件夹,但是当我尝试使用 os.getenv("HOME") 捕获他的 $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 并使用Defaults env_keep +="HOME" 修改sudoers 来保留环境变量,但都是徒劳的。
有任何想法吗?

我附上我的代码:

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,
}

【问题讨论】:

    标签: linux ubuntu lua lsyncd


    【解决方案1】:

    Linux 没有“登录用户”的概念。它有一个一个登录用户的概念,但可以同时有很多用户。

    您正在通过systemd 运行系统服务。告诉sudo 保留HOME 环境变量意味着当您运行sudo -E service lsyncd restart 时,service 命令运行时将HOME 设置为调用sudo 的用户的主目录。但这与服务的HOME 的价值无关。 Systemd 不会根据启动它的管理命令的环境来设置服务的环境,它们是基于服务配置的。

    如果您想同步特定用户的文件,则将路径硬编码到该用户的主目录,或使用getpwuidgetpwnam 查找用户的主目录。

    如果您想同步任何登录用户的文件,请不要使用系统服务。而是将 lsyncd 作为该用户登录会话的一部分运行。

    【讨论】:

      【解决方案2】:

      感谢 Gilles 的回答。不幸的是 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-08
        • 2011-09-24
        • 1970-01-01
        • 2012-02-23
        • 2012-10-28
        • 2019-10-18
        相关资源
        最近更新 更多