在本地文件C#中转换停靠的邮政

发布于 2025-02-07 22:49:36 字数 1118 浏览 2 评论 0原文

嗨,我试图将Postgres DB倒在Docker中。我在PowerShell上使用的脚本是:

docker exec -t timescaledb pg_dumpall -c -u postgres> .. \ dump_timescales \ dump_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#倾倒DB的最佳做法是哪种?

其他信息:我从触发Azure函数的时间开始运行此功能。

Hi I am trying to dump a postgres db in a docker. The script I use on powershell is :

docker exec -t timescaledb pg_dumpall -c -U postgres > ..\dump_timescales\dump_prova.sql

It work as expected. I am not able to figure it out how to run it in c#.
I tried the following:

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 = 
quot;/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;
        }

I get no error however no file is dumped.
What am I missing?
Which is the best practice to dump a db from c#?

Additional info: I am running this function from a Time Triggered Azure function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗化ㄋ丶相逢 2025-02-14 22:49:36

process.start - 顾名思义 - 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;
        }

process.Start - as the name implies - starts a process. If you exit your program after calling dumpTdb, then all child processes, including your dump process, will be killed.

Call process.WaitForExit() to wait for the dump to finish like this

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 = 
quot;/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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文