Securizare backup Postgres

Salutare,caut o solutie de securizare backup Postgres cu parola,pana acum arhivez rezultatul din pg_dump si apoi il sterg,am gasit pe net ca s-ar putea trimite direct in arhivator rezultatul pg_dump fara salvare pe disc,la fel si parola arhivei sa fie citita direct in environment variables

multumesc pt orice sugestie

C#

  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 + " -h " + host + " -p " + port + " -d " + database + " -U " + user;
            tmpPath = Path.GetTempPath();
            batFilePath = tmpPath + Guid.NewGuid().ToString() + ".cmd";
            outFile = @"\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");


            outPath = @"d:\";

            zipPth = @"c:\Program Files\7-Zip\7z.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 Like
string user = "postgres";
string password = "1234";
string archivePassword = "abcd";
string database = "postgres";
string destination = "pg_dump.7z";

using var pg_dump = new Process();
pg_dump.StartInfo.FileName = @"pg_dump.exe";
pg_dump.StartInfo.Arguments = $"-U {user} -w {database}";
pg_dump.StartInfo.EnvironmentVariables.Add("PGPASSWORD", password);
pg_dump.StartInfo.UseShellExecute = false;
pg_dump.StartInfo.RedirectStandardOutput = true;
pg_dump.StartInfo.CreateNoWindow = true;

using var compressor = new Process();
compressor.StartInfo.FileName = @"7z.exe";
compressor.StartInfo.Arguments = $"a -p{archivePassword} -si{Path.GetFileNameWithoutExtension(destination)} {destination}";
compressor.StartInfo.UseShellExecute = false;
compressor.StartInfo.RedirectStandardInput = true;
compressor.StartInfo.CreateNoWindow = true;

pg_dump.Start();
compressor.Start();

pg_dump.StandardOutput.BaseStream.CopyTo(compressor.StandardInput.BaseStream);

pg_dump.WaitForExit();
compressor.StandardInput.BaseStream.Flush();
compressor.StandardInput.BaseStream.Dispose();
compressor.WaitForExit();
2 Likes