コード例 #1
0
 public PostgresBackupper(string dbIgnore, PostgresBackupperConfig config, IDiagnosticLogger logger)
 {
     DbIgnore = dbIgnore ?? throw new ArgumentNullException(nameof(dbIgnore));
     Config   = config ?? throw new ArgumentNullException(nameof(config));
     _logger  = logger ?? throw new ArgumentNullException(nameof(logger));
     Logs     = new List <Log>();
 }
コード例 #2
0
        private async Task CreateSqlFilesAsync(string[] databases, string outFilePath, PostgresBackupperConfig config)
        {
            if (databases == null)
            {
                throw new ArgumentNullException(nameof(databases));
            }

            if (string.IsNullOrWhiteSpace(outFilePath))
            {
                throw new ArgumentNullException(nameof(outFilePath));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            foreach (var db in databases)
            {
                string resultPath = Path.Combine(outFilePath,
                                                 $"{db}.sql");

                string dumpCommand = $"pg_dump -h {config.Host} -p {config.Port} -U " +
                                     $"{config.Username} -Fp -d {db} > {resultPath}";

                await ExecuteCommandAsync(dumpCommand)
                .ConfigureAwait(continueOnCapturedContext: false);

                if (FileIsEmpty(resultPath))
                {
                    Logs.Add(new Log(DateTime.Now, $"{DateTime.Now}: Failed to make a {db} backup. You may not have access rights"));
                    File.Delete(resultPath);
                }
                else
                {
                    Logs.Add(new Log(DateTime.Now, $"{DateTime.Now}: {db} backup file weight : {new FileInfo(resultPath).Length} Bytes"));
                }
            }
        }