Program.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Microsoft.Win32;
  2. using System.Data;
  3. using System.Diagnostics;
  4. using System.Text;
  5. namespace Test;
  6. internal class Program
  7. {
  8. static void Main()
  9. {
  10. int[,] timeStamp = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
  11. int s = timeStamp[0, 1];
  12. int length = timeStamp.Length;
  13. foreach (var item in timeStamp)
  14. {
  15. }
  16. var t = timeStamp.GetValue(0,0);
  17. //DBTest test = new();
  18. //test.Initialize();
  19. //test.Test();
  20. //test.TestSystem();
  21. //test.TestOthers();
  22. //test.TestRaw();
  23. ProcessTest processTest = new();
  24. processTest.Initialize();
  25. processTest.BackUp("10.4.6.48", 5432, "postgres", "123456", "D://source_db_dump.custom", "tin01_db");
  26. //processTest.BackUpSingleTable("localhost", 5432, "postgres", "123456", "D://source_db_dump.custom", "FROMTIN", "20250630.PM1");
  27. //processTest.BackUpByDateTime("localhost", 5432, "postgres", "123456", "D://source_db_dump.custom", "FROMTIN", "20250630");
  28. processTest.Restore("localhost", 5432, "postgres", "123456", "D://source_db_dump.custom", "DBTestTIN");
  29. }
  30. }
  31. internal class ProcessTest
  32. {
  33. //HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql*
  34. private string? _binPath;
  35. private string? pg_dump;
  36. private string? pg_restore;
  37. public bool Initialize()
  38. {
  39. RegistryKey key = Registry.LocalMachine;
  40. key = key.OpenSubKey("SOFTWARE\\PostgreSQL\\Installations");
  41. if (key is null)
  42. key = key.OpenSubKey("SOFTWARE\\WOW6432Node\\PostgreSQL\\Installations");
  43. if (key is null)
  44. return false;
  45. if (key.GetSubKeyNames().FirstOrDefault(t => t.Contains("postgresql")) is not string nextKey)
  46. return false;
  47. key = key.OpenSubKey(nextKey);
  48. if (key.GetValue("Base Directory") is not string path)
  49. return false;
  50. this.pg_dump = Path.Combine(path, "bin", "pg_dump.exe");
  51. this.pg_restore = Path.Combine(path, "bin", "pg_restore.exe");
  52. return true;
  53. }
  54. public bool BackUp(string serverAddress, int port, string userName, string password, string savePath, string dbName)
  55. {
  56. if (string.IsNullOrEmpty(this.pg_dump))
  57. return false;
  58. Process process = new Process();
  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}\" {dbName}");
  62. process.StartInfo.CreateNoWindow = false;
  63. process.Start();
  64. process.WaitForExit();
  65. return true;
  66. }
  67. public bool Restore(string serverAddress, int port, string userName, string password, string savePath, string dbName)
  68. {
  69. if (string.IsNullOrEmpty(this.pg_restore))
  70. return false;
  71. Process process = new Process();
  72. process.StartInfo.Environment.Add("PGPASSWORD", password);
  73. process.StartInfo.FileName = this.pg_restore;
  74. process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -v -d {dbName} --clean \"{savePath}\"");
  75. process.StartInfo.CreateNoWindow = false;
  76. process.Start();
  77. process.WaitForExit();
  78. return true;
  79. }
  80. public bool BackUpSingleTable(string serverAddress, int port, string userName, string password, string savePath, string dbName, string tableName)
  81. {
  82. if (string.IsNullOrEmpty(this.pg_dump))
  83. return false;
  84. Process process = new Process();
  85. process.StartInfo.RedirectStandardOutput = true;
  86. process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
  87. process.StartInfo.Environment.Add("PGPASSWORD", password);
  88. process.StartInfo.FileName = this.pg_dump;
  89. process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" --table \"{tableName}\" {dbName}");
  90. process.StartInfo.CreateNoWindow = false;
  91. process.Start();
  92. process.WaitForExit();
  93. return true;
  94. }
  95. public bool BackUpByDateTime(string serverAddress, int port, string userName, string password, string savePath, string dbName, string time)
  96. {
  97. if (string.IsNullOrEmpty(this.pg_dump))
  98. return false;
  99. Process process = new Process();
  100. process.StartInfo.RedirectStandardOutput = true;
  101. process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
  102. process.StartInfo.Environment.Add("PGPASSWORD", password);
  103. process.StartInfo.FileName = this.pg_dump;
  104. process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" --table {time}* {dbName}");
  105. process.StartInfo.CreateNoWindow = false;
  106. process.Start();
  107. process.WaitForExit();
  108. return true;
  109. }
  110. }