Reputation: 3192
I am writing some c# code to take a backup of my Posgresql database.
public static bool MakeBackup(string schema, string userName, string password, string backupFolder)
{
var database = "Import";
var timestamp = DateTime.Now.ToString("yyyyMMdd-HH-mm-ss");
var file = String.Format("{0}_{1}.sql", schema, timestamp);
var fullPath = String.Format(@"{0}\{1}", backupFolder, file);
var executablePath = @"C:\Program Files\PostgreSQL\9.3\bin\pg_dump.exe";
var arguments = String.Format(@" -h localhost -d{0} -n{1} -U{2} -w{3} > {4}",database, schema, userName, password, fullPath );
Boolean result = false;
try
{
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
info.FileName = executablePath;
info.Arguments = arguments;
info.CreateNoWindow = true;
info.UseShellExecute = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = info;
proc.Start();
proc.WaitForExit();
result = true;
}
catch (Exception ex)
{
//Console.writeline(ex.Message);
}
return result;
}
I started out by getting an access denied error. This I fixed by making sure my application has writing access to the output folder and setting the info.Arguments
to the full pg_dump.exe path.
I tried pause in debug, then copy the arguments and execute them in a command prompt this works fine.
So I don´t get any error but my folder remains empty.
Any suggestions?
Upvotes: 2
Views: 2364
Reputation: 31193
You are trying to forward the output of pg_dump with > to a file. This will not work, since there is no shell handling the output forwarding. Rather you should use pg_dump's option -f output_file
to save the output to a desired file.
At the moment the > file
will just be sent as a command line argument to pg_dump and it might or might not show error for it.
Upvotes: 3