Home > Software design >  How to validate if a newly created backup in Postgre is healthy?
How to validate if a newly created backup in Postgre is healthy?

Time:11-08

My Asp.net Core application performs database backups (Postgree v13) by running the command via cmd. According to the routine, some parameters are passed to the function and it performs the backup, but if there is a problem during the process and it gets corrupted, there is no return parameter that informs this (at least as far as researched). Hj I had this problem in the system of a client who swore to have made a backup, before formatting the server, but when he went to restore it, it was corrupted. This is very embarrassing!

Questions:

1 - Is there any way I can ensure that the backup created is intact and error free? 2 - Is there a way for pg_dump to return a parameter that I can handle the return and prevent the corrupted backup from being generated or displaying an error message?

public string CreateBackup(string usuario, string senha, string servidor, string porta, string nomeBancoDados, string nomeArquivo, string diretorioComando, string diretorioBackup)
{
    try
    {
        var pathString = Path.Combine(Directory.GetCurrentDirectory(), $"wwwroot\\"   diretorioBackup);

        //Verificar se existe a pasta downloads
        if (!Directory.Exists(pathString))
        {
            Directory.CreateDirectory(pathString);
        }

        Environment.SetEnvironmentVariable("PGPASSWORD", senha);

        string fileName = nomeArquivo   "_"   DateTime.Now.ToString("dd")   "_"   DateTime.Now.ToString("MM")   "_"   DateTime.Now.ToString("yyyy")   "_"   DateTime.Now.ToString("HH-mm")   ".backup";
        string backupFile = pathString   "\\"   fileName;
        string BackupString = "-Ft -U "   usuario   " -h "   servidor   " -f "   backupFile   " "   nomeBancoDados;
                        
        Process proc = new Process();
        proc.StartInfo.FileName = "C:\\Program Files\\"   diretorioComando   "\\pg_dump.exe";
        proc.StartInfo.Arguments = BackupString;

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

        return fileName;

    }
    catch (Exception)
    {
        return null;
    }

}

CodePudding user response:

Get the ExitCode of the process after it is done.

If that is 0, pg_dump completed successfully. If not, there was an error.

  • Related