【问题标题】:Solve a permission problem found when running pgbackrest as a cron job解决将 pgbackrest 作为 cron 作业运行时发现的权限问题
【发布时间】:2019-09-25 01:36:09
【问题描述】:

我在使用 pgbackrest 进行数据库备份的 cron 作业设置中的 ubuntu 主机上出现权限错误。

错误 [041]: : 无法打开 /var/lib/postgresql/10/main/global/pg_control

cron 作业设置为在我的管理员帐户下运行。我看到解决此问题的唯一选择是将目录权限更改为 /var/lib/postgresql/10/main 以允许我的管理员帐户进入,我不想这样做。

显然只有 postgres 用户可以访问此目录,我发现使用该用户无法设置 cron 作业。 即

postgres@host110:~/$ crontab -e
You (postgres) are not allowed to use this program (crontab)
See crontab(1) for more information

我还能做什么? pgbackrest 手册中没有这方面的更多信息。

【问题讨论】:

    标签: postgresql ubuntu cron


    【解决方案1】:

    仅允许 PostgreSQL 操作系统用户 (postgres) 及其组访问 PostgreSQL 数据目录。见the source的这段代码:

        /*
         * Check if the directory has correct permissions.  If not, reject.
         *
         * Only two possible modes are allowed, 0700 and 0750.  The latter mode
         * indicates that group read/execute should be allowed on all newly
         * created files and directories.
         *
         * XXX temporarily suppress check when on Windows, because there may not
         * be proper support for Unix-y file permissions.  Need to think of a
         * reasonable check to apply on Windows.
         */
    #if !defined(WIN32) && !defined(__CYGWIN__)
        if (stat_buf.st_mode & PG_MODE_MASK_GROUP)
            ereport(FATAL,
                    (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                     errmsg("data directory \"%s\" has invalid permissions",
                            DataDir),
                     errdetail("Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).")));
    #endif
    

    如果数据目录允许该组进入,该组通常也会拥有pg_control的权限。

    所以如果你给它postgres'主要用户组,你可以允许该pgBackRest用户进入。

    如果系统进行了相应的配置,postgres 可以创建crontab

    来自man crontab

    可以允许或禁止不同用户运行cron 作业。为此,请使用cron.allowcron.deny 文件。如果cron.allow 文件存在,则 用户必须列在其中才能使用cron 如果cron.allow 文件不存在但cron.deny 文件存在,则用户不得列在其中 cron.deny 文件以使用 cron。如果这些文件都不存在,则只允许超级用户使用cron。另一种限制访问cron的方法 是在/etc/security/access.conf中使用PAM认证设置用户,允许或不允许使用crontab或修改系统cron中的作业 /etc/cron.d/目录。

    【讨论】:

    • 是的。添加 'postgres' 到 cron.allow 修复它。
    【解决方案2】:

    如果您能够在提示符下sudo -u postgres,您也可以在您的cron 工作中进行。

    您的问题并未显示您尝试运行哪些实际命令,而是将thiscommand 运行为postgres,只是

    sudo -u postgres thiscommand
    

    如果你有su,但没有sudo,则适应很小,但并非完全无关紧要:

    su -c thiscommand postgres
    

    使用sudo,您可以对作为其他用户的具体操作设置细粒度限制,因此从这个意义上说,它比完全无限制的su 更安全。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    相关资源
    最近更新 更多