dump dockerized postgres到本地文件c#



你好,我试图转储postgres数据库在docker。我在powershell上使用的脚本是:

docker exec -t timescaledb pg_dumpall -c -U postgres > ..dump_timescalesdump_prova.sql

按预期工作。我不知道如何在c#中运行它。我尝试了以下操作:

internal static bool dumpTdb(DateTime time)
{

try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "powershell.exe";
startInfo.Arguments = $"/C  docker exec -t timescaledb pg_dumpall -c -U postgres > ..\dump_timescales\dump_prova_{time}.sql";
process.StartInfo = startInfo;
process.Start();
}
catch (Exception) 
{
return false;
}
return true;
}

我没有得到错误,但是没有文件被转储。我错过了什么?从c#中转储数据库的最佳实践是什么?

附加信息:我正在从时间触发Azure功能运行此功能。

process.Start-顾名思义-启动进程。如果在调用dumpTdb后退出程序,那么所有子进程,包括转储进程,都将被杀死。

调用process.WaitForExit()等待转储完成,如下所示

internal static bool dumpTdb(DateTime time)
{

try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "powershell.exe";
startInfo.Arguments = $"/C  docker exec -t timescaledb pg_dumpall -c -U postgres > ..\dump_timescales\dump_prova_{time}.sql";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
catch (Exception) 
{
return false;
}
return true;
}

最新更新