public void BackupDatabase(string fileName) { if (!this.IsValidRequest()) { this.Clients.Caller.getNotification(Warnings.AccessIsDenied, Warnings.AccessIsDenied); return; } if (string.IsNullOrWhiteSpace(fileName)) { this.Clients.Caller.backupFailed(Warnings.NoFileSpecified); return; } PostgreSQLServer server = new PostgreSQLServer { BinDirectory = ConfigurationHelper.GetDbServerParameter("PostgreSQLBinDirectory"), DatabaseBackupDirectory = ConfigurationHelper.GetDbServerParameter("DatabaseBackupDirectory"), DatabaseName = AppUsers.GetCurrentUserDB(), HostName = ConfigurationHelper.GetDbServerParameter("Server"), PortNumber = Conversion.TryCastInteger(ConfigurationHelper.GetDbServerParameter("Port")), UserId = ConfigurationHelper.GetDbServerParameter("UserId"), Password = ConfigurationHelper.GetDbServerParameter("Password") }; server.Validate(); if (server.IsValid && !string.IsNullOrWhiteSpace(server.BinDirectory) && !string.IsNullOrWhiteSpace(server.DatabaseBackupDirectory)) { this.Backup(server, fileName); return; } this.Clients.Caller.backupFailed(Warnings.ConfigurationError); }
private void Backup(PostgreSQLServer server, string fileName) { string pgdumpPath = Path.Combine(server.BinDirectory, "pg_dump.exe"); backupDirectory = HostingEnvironment.MapPath(server.DatabaseBackupDirectory); if (backupDirectory != null) { backupDirectory = Path.Combine(backupDirectory, fileName); Directory.CreateDirectory(backupDirectory); string path = Path.Combine(backupDirectory, "db.backup"); bool result = this.BackupDatabase(pgdumpPath, server, path); if (result) { StringBuilder message = new StringBuilder(); message.Append(Labels.DatabaseBackupSuccessful); message.Append(" "); message.Append("<a href='"); message.Append( PageUtility.ResolveUrl(Path.Combine(server.DatabaseBackupDirectory, fileName + ".zip"))); message.Append("'"); message.Append(" target='_blank'>"); message.Append(Labels.ClickHereToDownload); message.Append("</a>"); this.Clients.Caller.backupCompleted(message.ToString()); return; } this.Clients.Caller.backupFailed(Warnings.CannotCreateABackup); } }
private void CreateBatchFile(PostgreSQLServer server, string pgDumpPath, string fileName) { Collection<string> commands = new Collection<string>(); commands.Add("@echo off"); commands.Add("SET PGPASSWORD="******"""{0}"" --host ""{1}"" --port {2} --username ""{3}"" --format custom --blobs --verbose --file ""{4}"" ""{5}"""; command = string.Format(CultureInfo.InvariantCulture, command, pgDumpPath, server.HostName, server.PortNumber, server.UserId, fileName, server.DatabaseName); commands.Add(command); commands.Add("exit"); batchFile = fileName + ".bat"; File.WriteAllText(batchFile, string.Join(Environment.NewLine, commands)); }
private bool BackupDatabase(string pgDumpPath, PostgreSQLServer server, string fileName) { this.CreateBatchFile(server, pgDumpPath, fileName); using (Process process = new Process()) { process.StartInfo.FileName = batchFile; process.StartInfo.CreateNoWindow = true; process.StartInfo.ErrorDialog = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.ErrorDataReceived += this.Data_Received; process.OutputDataReceived += this.Data_Received; process.Disposed += this.Completed; process.Start(); process.BeginErrorReadLine(); process.BeginOutputReadLine(); process.WaitForExit(); return true; } }