在 Elixir 中使用 System.cmd :into 选项写入文件

Write to file with System.cmd :into option in Elixir

我想使用 System.cmd/3 对我的数据库进行 postgres 转储,并将文件写入我的 phoenix 应用程序的本地文件夹,但我无法让它工作。我尝试使用 "into" 选项。

这是我所做的:

System.cmd("pg_dump", ["myapp_dev"], into: "backup.sql")

:into 选项,正如 docs 中明确指出的那样,确实会将结果注入给定的可收集项,这意味着命令的输出将附加到您传递并返回的字符串中.

使用本机 pg_dump --file 选项更容易转储到文件,让 shell 和 pg_dump 执行所有工作,而不是来回传递巨大的 blob。以下应该有效。

System.cmd("pg_dump", ["myapp_dev", "--file=backup.sql"])