在php代码下调用pg_dump时输出为空
我需要通过 php 中的 pg_dump 进行转储,所以我有一个像这样的函数:
function fnDump()
{
exec("/usr/local/bin/sudo -u pg_user /usr/local/bin/pg_dump mon_alarm > /usr/home/user/monitor_test/renew_db/mon_alarm.sql",$out);
var_dump($out);
}
问题是 mon_alarm.sql
文件是空的。 但是当我通过命令行执行此命令时,一切正常。 我应该改变什么来在 php 中创建转储?
I need to make a dump via pg_dump in php so i've got a function like this:
function fnDump()
{
exec("/usr/local/bin/sudo -u pg_user /usr/local/bin/pg_dump mon_alarm > /usr/home/user/monitor_test/renew_db/mon_alarm.sql",$out);
var_dump($out);
}
The problem is that mon_alarm.sql
file is empty.
But when i execute this command via command line everything works fine.
What should i change to create a dump in php?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在标准网络服务器设置下运行 PHP,则它将无法工作,因为它将在网络服务器用户的上下文中运行,因此 sudo 不会让您像这样更改用户上下文。
如果这是一个脚本,您将必须调整 sudo 以仅运行
pg_dump
命令作为用户的无密码 sudo 权限,否则您的 sudo 命令将提示输入密码并破坏您的自动化流程。If you're running PHP under a standard webserver setup, that won't work because it will run under the context of the webserver user, so sudo won't let you change user context like that.
If this is a script you're going to have to adjust sudo to run only the
pg_dump
command as passwordless sudo permissions for the user, otherwise your sudo command will prompt for a password and ruin your automated process.