解决将 pgbackrest 作为 cron 作业运行时发现的权限问题



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

错误 [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 OS用户(postgres(及其组访问PostgreSQL数据目录。从源代码查看此代码:

/*
* 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的访问的另一种方法 是使用 PAM 身份验证/etc/security/access.conf来设置用户,允许或不允许用户使用crontab或修改系统中的系统cron作业/etc/cron.d/目录。

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

您的问题不会显示您尝试运行哪些实际命令,而是像postgres一样运行thiscommand,只需

sudo -u postgres thiscommand

如果你有su但没有sudo,改编是次要的,但并非完全微不足道:

su -c thiscommand postgres

使用sudo,您可以对作为其他用户的确切操作设置细粒度的限制,因此从这个意义上讲,它比 完全无限su.

最新更新