private string RunDb2Look(string database, string user, string password, string arguments) { // Get DB2Look path var db2Home = ConfigurationManager.AppSettings["DB2Home"]; if (string.IsNullOrEmpty(db2Home)) { throw new Exception("DB2Home setting not set in the configuration. Please set the value for it in " + Application.ProductName + ".exe.config file"); } else if (!Directory.Exists(db2Home)) { throw new Exception("DB2Home directory '" + db2Home + "' does not exist. Please set the value for it in " + Application.ProductName + ".exe.config file"); } var db2Look = Path.Combine(db2Home, "db2look.exe"); if (!File.Exists(db2Look)) { throw new Exception("db2look executable '" + db2Look + "' does not exist. Please make sure you have a DB2 client properly installed"); } var db2LookArguments = string.Format( "-d {0} -i {1} -w {2} -a -e -x ", database, user, password) + arguments; // Build script var scriptFile = Path.GetTempFileName(); try { scriptFile = Path.Combine(Path.GetDirectoryName(scriptFile) ?? string.Empty, Path.GetFileNameWithoutExtension(scriptFile) + ".bat"); var scriptContents = "@echo off" + Environment.NewLine; scriptContents += @"@set PATH=%~d0%~p0..\db2tss\bin;%PATH%" + Environment.NewLine; scriptContents += @"@cd " + db2Home + @"\..\bnd" + Environment.NewLine; scriptContents += @"@db2clpsetcp" + Environment.NewLine; scriptContents += "\"" + db2Look + "\" " + db2LookArguments; File.WriteAllText(scriptFile, scriptContents); var executor = new BackgroundProcessExecutor(); var commandOutput = executor.RunBackgroundProcess(scriptFile, null); if (commandOutput.ExitCode != 0) { throw new Exception("db2look.exe returned unsuccessful return code of " + commandOutput.ExitCode + ". " + commandOutput.StandardError + Environment.NewLine + commandOutput.StandardError); } // Clean up standard output var output = commandOutput.StandardOutput; output = CleanText(output, _trailingSpaceRegex); return(output); } finally { try { File.Delete(scriptFile); } catch (Exception ex) { _log.ErrorFormat("Error deleting file {0}. {1}", scriptFile, ex.Message); _log.Error(ex.Message, ex); } } }
private string RunPgDump([NotNull] string host, [NotNull] string database, [NotNull] string user, [NotNull] string password, int port) { if (host.IsNullEmptyOrWhitespace()) { throw new ArgumentNullException("host"); } if (database.IsNullEmptyOrWhitespace()) { throw new ArgumentNullException("database"); } if (user.IsNullEmptyOrWhitespace()) { throw new ArgumentNullException("user"); } if (password.IsNullEmptyOrWhitespace()) { throw new ArgumentNullException("password"); } // Get pg_dump path var postgreSqlHome = ConfigurationManager.AppSettings["PostgreSQLHome"]; if (string.IsNullOrEmpty(postgreSqlHome)) { throw new Exception("PostgreSQLHome setting not set in the configuration. Please set the value for it in " + Application.ProductName + ".exe.config file"); } else if (!Directory.Exists(postgreSqlHome)) { throw new Exception("PostgreSQLHome directory '" + postgreSqlHome + "' does not exist. Please set the value for it in " + Application.ProductName + ".exe.config file"); } var pgDump = Path.Combine(postgreSqlHome, "pg_dump.exe"); if (!File.Exists(pgDump)) { throw new Exception("pg_dump.exe executable '" + pgDump + "' does not exist. Please make sure you have it properly installed"); } // pg_dump.exe --username=mmedic --schema-only --host=localhost Test var pgDumpArguments = string.Format( "--host={0} --port={1} --username={2} --schema-only --no-password {3} ", host, port, user, database); // Build script var tempFile = Path.GetTempFileName(); var scriptFile = Path.Combine(Path.GetDirectoryName(tempFile) ?? string.Empty, Path.GetFileNameWithoutExtension(tempFile) + ".bat"); try { File.Move(tempFile, scriptFile); var scriptContents = "@echo off" + Environment.NewLine; scriptContents += @"@set PGPASSWORD="******"\"" + pgDump + "\" " + pgDumpArguments; File.WriteAllText(scriptFile, scriptContents); var executor = new BackgroundProcessExecutor(); var commandOutput = executor.RunBackgroundProcess(scriptFile, null); if (commandOutput.ExitCode != 0) { throw new Exception("pg_dump.exe returned unsuccessful return code of " + commandOutput.ExitCode + ". " + commandOutput.StandardError + Environment.NewLine + commandOutput.StandardError); } // Clean up standard output var output = commandOutput.StandardOutput; output = CleanText(output, _trailingSpaceRegex); return(output); } finally { try { File.Delete(scriptFile); } catch (Exception ex) { _log.ErrorFormat("Error deleting file {0}. {1}", scriptFile, ex.Message); _log.Error(ex.Message, ex); } } }