0
votes

I making Backup and Restore to system.

For make Backup im using this code:

textInformacao.Text = "";


        Environment.SetEnvironmentVariable("PGPASSWORD", ConfiguracaoSistema.Password);

        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = @Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\backup\\pg_dump.exe";
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = false;
        psi.CreateNoWindow = true;
        psi.Arguments = string.Format(@"-h {0} -p {1} -U {2} -F c -b -v -f {3} {4}", caminhoServidor, ConfiguracaoSistema.Port, ConfiguracaoSistema.UserName, arquivoSaida, ConfiguracaoSistema.NomeDataBase);
        psi.UseShellExecute = false;

        p = Process.Start(psi);
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.EnableRaisingEvents = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.OutputDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
        p.ErrorDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
        p.Exited += (sender, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += "Processo Finalizado!"; })); };
        p.Start();
        p.BeginOutputReadLine();
        p.BeginErrorReadLine();

If I get this file and open pgAdmin, all will do right, database will be restored.

for make restore im using this code:

textInformacao.Text = "";

        Environment.SetEnvironmentVariable("PGPASSWORD", ConfiguracaoSistema.Password);

        string caminhoArquivo = @Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\testesigep_vs.backup";



        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = @Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\backup\\pg_restore.exe";
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = false;
        psi.CreateNoWindow = true;
        psi.Arguments = string.Format(@"-h {0} -p {1} -U {2} -c -d {3} -v {4}", caminhoServidor, ConfiguracaoSistema.Port, ConfiguracaoSistema.UserName, ConfiguracaoSistema.NomeDataBase, caminhoArquivo);
        psi.UseShellExecute = false;


        p = Process.Start(psi);
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.EnableRaisingEvents = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.OutputDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
        p.ErrorDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
        p.Exited += (sender, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += "Processo Finalizado!"; })); };
        p.Start();
        p.BeginOutputReadLine();
        p.BeginErrorReadLine();

when I run this code, I have a "big problem" all lines are duplicate. look my database restored:

Database picture

look picture, look ID column have 2 values same.

How can I solve this problem? Why in PgAdmin Restore Have no Problem, and in c# do this problem?

EDIT:::::::::::::::::::::::::::::::::::::::::::::::::::: Start Restore

pg_restore: connecting to database for restore pg_restore: dropping DATABASE sigep_vs pg_restore: [archiver (db)] Error while PROCESSING TOC: pg_restore: [archiver (db)] Error from TOC entry 2949; 1262 158041 DATABASE sigep_vs sigep pg_restore: [archiver (db)] could not execute query: ERROR: cannot drop the currently open database Command was: DROP DATABASE sigep_vs;

pg_restore: creating DATABASE "sigep_vs" pg_restore: [archiver (db)] could not execute query: ERROR: database "sigep_vs" already exists Command was: CREATE DATABASE sigep_vs WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C' LC_CTYPE = 'C';

Environment.SetEnvironmentVariable("PGPASSWORD", ConfiguracaoSistema.Password);

        string caminhoArquivo = @Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\testesigep_vs.backup";

        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = @Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\backup\\pg_restore.exe";
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = false;
        psi.CreateNoWindow = true;
        psi.Arguments = string.Format(@"-h {0} -p {1} -U {2} -d {3} -v {4}", caminhoServidor, ConfiguracaoSistema.Port, ConfiguracaoSistema.UserName, ConfiguracaoSistema.NomeDataBase, caminhoArquivo);
        psi.UseShellExecute = false;

        p = new Process { StartInfo = psi };

        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.EnableRaisingEvents = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.OutputDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
        p.ErrorDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
        p.Exited += (sender, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += "Processo Finalizado!"; })); };
        p.Start();
        p.BeginOutputReadLine();
        p.BeginErrorReadLine();
1
Do you get the same behaviour if you just execute the commands? The C# code is just noise in this case and doesn't offer anything.Panagiotis Kanavos
I tested only in pgadmin and worked, I dont know why having this problemHimorriveL
Did you try the commands you posted here? pg_dump and pg_restore ? pgAdmin is a different tool. If you used the wrong parameters, saying that pgAdmin worked doesn't say anythingPanagiotis Kanavos
That looks like you restored without first dropping the target objects, but you specified -c for pg_restore, so it should work... Try to capture the standard output from pg_restore, that will shed light on the matter.Laurenz Albe
look he is telling database is in use, but is localhost and I can tell, he is not in UseHimorriveL

1 Answers

0
votes

Now, is working:

Environment.SetEnvironmentVariable("PGPASSWORD", ConfiguracaoSistema.Password);

        string caminhoArquivo = @Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\testesigep_vs.backup";

        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = @Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\backup\\pg_restore.exe";
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = false;
        psi.CreateNoWindow = true;
        psi.Arguments = string.Format(@"-h {0} -p {1} -U {2} -d {3} -v {4}", caminhoServidor, ConfiguracaoSistema.Port, ConfiguracaoSistema.UserName, ConfiguracaoSistema.NomeDataBase, caminhoArquivo);
        psi.UseShellExecute = false;

        p = new Process { StartInfo = psi };

        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.EnableRaisingEvents = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.OutputDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
        p.ErrorDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
        p.Exited += (sender, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += "Processo Finalizado!"; })); };
        p.Start();
        p.BeginOutputReadLine();
        p.BeginErrorReadLine();`