PostgreSQLTool.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Microsoft.Win32;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text;
  5. namespace DBBackupTool.Helpers;
  6. internal class PostgreSQLTool
  7. {
  8. //HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql*
  9. private string? pg_dump;
  10. private string? pg_restore;
  11. public bool Initialize()
  12. {
  13. RegistryKey? key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\PostgreSQL\\Installations");
  14. key ??= Registry.LocalMachine.OpenSubKey("SOFTWARE\\WOW6432Node\\PostgreSQL\\Installations");
  15. if (key is null)
  16. return false;
  17. if (key.GetSubKeyNames().FirstOrDefault(t => t.Contains("postgresql")) is not string nextKey)
  18. return false;
  19. key = key.OpenSubKey(nextKey);
  20. if (key?.GetValue("Base Directory") is not string path)
  21. return false;
  22. this.pg_dump = Path.Combine(path, "bin", "pg_dump.exe");
  23. this.pg_restore = Path.Combine(path, "bin", "pg_restore.exe");
  24. return true;
  25. }
  26. public bool BackUp(string serverAddress, int port, string userName, string password, string savePath, string dbName)
  27. {
  28. if (string.IsNullOrEmpty(this.pg_dump))
  29. return false;
  30. Process process = new();
  31. process.StartInfo.Environment.Add("PGPASSWORD", password);
  32. process.StartInfo.FileName = this.pg_dump;
  33. process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" {dbName}");
  34. process.StartInfo.CreateNoWindow = true;
  35. process.Start();
  36. process.WaitForExit();
  37. return true;
  38. }
  39. public bool Restore(string serverAddress, int port, string userName, string password, string savePath, string dbName)
  40. {
  41. if (string.IsNullOrEmpty(this.pg_restore))
  42. return false;
  43. Process process = new();
  44. process.StartInfo.Environment.Add("PGPASSWORD", password);
  45. process.StartInfo.FileName = this.pg_restore;
  46. process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -v -d {dbName} --clean \"{savePath}\"");
  47. process.StartInfo.CreateNoWindow = true;
  48. process.Start();
  49. process.WaitForExit();
  50. return true;
  51. }
  52. public bool BackUpSingleTable(string serverAddress, int port, string userName, string password, string savePath, string dbName, string tableName)
  53. {
  54. if (string.IsNullOrEmpty(this.pg_dump))
  55. return false;
  56. Process process = new Process();
  57. process.StartInfo.RedirectStandardOutput = true;
  58. process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
  59. process.StartInfo.Environment.Add("PGPASSWORD", password);
  60. process.StartInfo.FileName = this.pg_dump;
  61. process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" --table \"{tableName}\" {dbName}");
  62. process.StartInfo.CreateNoWindow = false;
  63. process.Start();
  64. process.WaitForExit();
  65. return true;
  66. }
  67. public bool BackUpByDateTime(string serverAddress, int port, string userName, string password, string savePath, string dbName, string time)
  68. {
  69. if (string.IsNullOrEmpty(this.pg_dump))
  70. return false;
  71. Process process = new Process();
  72. process.StartInfo.RedirectStandardOutput = true;
  73. process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
  74. process.StartInfo.Environment.Add("PGPASSWORD", password);
  75. process.StartInfo.FileName = this.pg_dump;
  76. process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" --table {time}* {dbName}");
  77. process.StartInfo.CreateNoWindow = false;
  78. process.Start();
  79. process.WaitForExit();
  80. return true;
  81. }
  82. }