从C#创建Postgres密码保护的备份

huangapple go评论74阅读模式
英文:

Create Postgres password protected backup from C#

问题

我想要从C#创建一个受密码保护的Postgres备份。

我尝试了下面的代码,但我认为有一种更安全的方法:直接从pg_dump传递备份到存档,而不将其保存在磁盘上,并从环境变量中添加密码,就像使用PGPASSWORD一样。

string pgDumpPath, outFile, host, port, database, user, password, dumpCommand, batFilePath, zipPth, outPath, tmpPath;

pgDumpPath = @"d:\_dev_\postgres\pgsql\bin\pg_dump.exe";
host = "127.0.0.1";
port = "5432";
database = "postgres";
user = "postgres";
password = "mypass";
dumpCommand = pgDumpPath + " -Fc" + " -h " + host + " -p " + port + " -d " + database + " -U " + user;
tmpPath = Path.GetTempPath();
batFilePath = tmpPath + Guid.NewGuid().ToString() + ".cmd";

outFile = @"myBackup";

outPath = @"d:\";

zipPth = @"c:\Program Files-Zipz.exe";
string tmpfile = "\"" + tmpPath + outFile + ".sql\"";
Environment.SetEnvironmentVariable("PGPASSWORD", password);

try
{
    File.WriteAllText(batFilePath,
        "@echo off " + "\n" +
        dumpCommand + "  > " + tmpfile + "\n" + "\"" +
        zipPth + "\"" + " a \"" + outPath + outFile + ".zip\" -p" + password + " " + tmpfile +
        "  &DEL " + tmpfile +
        "  &DEL \"" + batFilePath + "\"",
        Encoding.ASCII);

    if (File.Exists(tmpfile))
        File.Delete(tmpfile);

    var oInfo = new ProcessStartInfo(batFilePath)
    {
        UseShellExecute = false,
        CreateNoWindow = true
    };

    using (var proc = Process.Start(oInfo))
    {
        proc.WaitForExit();
        proc.Close();
    }
}
finally
{
    if (File.Exists(batFilePath))
        File.Delete(batFilePath);
}

这是您提供的代码的翻译部分。如果您需要任何进一步的帮助或解释,请随时告诉我。

英文:

I want to create a password protected Postgres backup from C#

I tried next code but I think there is a more secure way: to pass backup directly from pg_dump to arhive without saving it on the disk and to add password form env variable like is done with PGPASSWORD

   string pgDumpPath, outFile, host, port, database, user, password, dumpCommand, batFilePath, zipPth, outPath, tmpPath;

    pgDumpPath = @"d:\_dev_\postgres\pgsql\bin\pg_dump.exe";
    host = "127.0.0.1";
    port = "5432";
    database = "postgres";
    user = "postgres";
    password = "mypass";
    dumpCommand = pgDumpPath + " -Fc" + " -h " + host + " -p " + port + " -d " + database + " -U " + user;
    tmpPath = Path.GetTempPath();
    batFilePath = tmpPath + Guid.NewGuid().ToString() + ".cmd";

    outFile = @"myBackup";

    outPath = @"d:\";

    zipPth = @"c:\Program Files-Zipz.exe";
    string tmpfile = "\"" + tmpPath + outFile + ".sql\"";
    Environment.SetEnvironmentVariable("PGPASSWORD", password);

    try
    {


        File.WriteAllText(batFilePath,
         "@echo off " + "\n" +
          dumpCommand + "  > " + tmpfile + "\n" + "\"" +
          zipPth + "\"" + " a \"" + outPath + outFile + ".zip\" -p" + password + " " +tmpfile +
           "  &DEL " + tmpfile  +
           "  &DEL \"" + batFilePath + "\"",
         Encoding.ASCII);


        if (File.Exists(tmpfile))
            File.Delete(tmpfile);

        var oInfo = new ProcessStartInfo(batFilePath)
        {
            UseShellExecute = false,
            CreateNoWindow = true
        };

        using (var proc = Process.Start(oInfo))
        {
            proc.WaitForExit();
            proc.Close();
        }
    }
    finally
    {
        if (File.Exists(batFilePath))
            File.Delete(batFilePath);

    }

答案1

得分: 0

以下是您提供的代码的翻译部分:

got a working solution:

    string database = "postgres", user = "postgres", password = "mypass", archivePassword = "mypass2";

    var outFile = @"d:\postgres_" + DateTime.Now.ToString("yyyy") + "_" + DateTime.Now.ToString("MM") + "_" +
                                         DateTime.Now.ToString("dd") + "_" + DateTime.Now.ToString("hh") + DateTime.Now.ToString("mm") +
                                         DateTime.Now.ToString("ss");

    var sInfo = new ProcessStartInfo()
    {
        FileName = @"d:\_dev_\postgres\pgsql\bin\pg_dump.exe",
        Arguments = $"-U {user} -w {database}",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    };

    sInfo.EnvironmentVariables.Add("PGPASSWORD", password);


    var cInfo = new ProcessStartInfo()
    {

        FileName = @"c:\Program Files-Zipz.exe",
        Arguments = $"a -p{archivePassword} -si{Path.GetFileNameWithoutExtension(outFile)} {outFile}",
        UseShellExecute = false,
        RedirectStandardInput = true,
        CreateNoWindow = true
    };



    using (var proc = Process.Start(sInfo))
    {
        using (var comp = Process.Start(cInfo))
        {
            proc.StandardOutput.BaseStream.CopyTo(comp.StandardInput.BaseStream);

            proc.WaitForExit();
            comp.StandardInput.BaseStream.Flush();
            comp.StandardInput.BaseStream.Dispose();
            comp.WaitForExit();

        }
    }

注意:已经将HTML转义字符(例如")还原为正常的双引号。

英文:

got a working solution:

string database = "postgres",user = "postgres", password = "mypass", archivePassword = "mypass2";

            var outFile = @"d:\postgres_" + DateTime.Now.ToString("yyyy") + "_" + DateTime.Now.ToString("MM") + "_" +
                                     DateTime.Now.ToString("dd") + "_" + DateTime.Now.ToString("hh") + DateTime.Now.ToString("mm") +
                                     DateTime.Now.ToString("ss");

            var sInfo = new ProcessStartInfo()
            {
                FileName = @"d:\_dev_\postgres\pgsql\bin\pg_dump.exe",
                Arguments = $"-U {user} -w {database}",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true
            };

            sInfo.EnvironmentVariables.Add("PGPASSWORD", password);


            var cInfo = new ProcessStartInfo()
            {

                FileName = @"c:\Program Files-Zipz.exe",
                Arguments = $"a -p{archivePassword} -si{Path.GetFileNameWithoutExtension(outFile)} {outFile}",
                UseShellExecute = false,
                RedirectStandardInput = true,
                CreateNoWindow = true
            };



            using (var proc = Process.Start(sInfo))
            {
                using (var comp = Process.Start(cInfo))
                {
                    proc.StandardOutput.BaseStream.CopyTo(comp.StandardInput.BaseStream);

                    proc.WaitForExit();
                    comp.StandardInput.BaseStream.Flush();
                    comp.StandardInput.BaseStream.Dispose();
                    comp.WaitForExit();

                }
            }

huangapple
  • 本文由 发表于 2023年7月10日 16:31:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定