Tuesday, 7 March 2017

Postgres Database backup in C#.

public static void DatabaseBackup()
{
BackupDatabase("127.0.0.1","5432","postgres","password","MyDataBase",C:\\Users\\Admin\\Desktop\\,"MyDatabase_","C:\\Program Files\\PostgreSQL\\9.4\\bin");
}

public static string BackupDatabase(
            string server,
            string port,
            string user,
            string password,
            string dbname,
            string backupdir,
            string backupFileName,
            string backupCommandDir)
{
try
{

Environment.SetEnvironmentVariable("PGPASSWORD", password);

string backupFile = backupdir + backupFileName + DateTime.Now.ToString("yyyy") + "_" + DateTime.Now.ToString("MM") + "_" + DateTime.Now.ToString("dd") + ".backup";
string BackupString = "-ibv -Z3 -f \"" + backupFile + "\" " +
"-Fc -h " + server + " -U " + user + " -p " + port + " " + dbname;

Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = backupCommandDir + "\\pg_dump.exe";
proc.StartInfo.Arguments = BackupString;

proc.Start();
proc.WaitForExit();
proc.Close();

return backupFile;
}
catch (Exception ex)
{
return null;
}
}

No comments:

Post a Comment