Program.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using Microsoft.IdentityModel.Tokens;
  2. using Microsoft.Win32;
  3. using System.Data;
  4. using System.Diagnostics;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using Universal;
  8. namespace Test;
  9. internal class Program
  10. {
  11. static void Main()
  12. {
  13. CompressTest test = new();
  14. test.Test();
  15. //DBTest test = new();
  16. //test.Initialize();
  17. //test.Test();
  18. //test.TestSystem();
  19. //test.TestOthers();
  20. //test.TestRaw();
  21. //ProcessTest processTest = new();
  22. //processTest.Initialize();
  23. //processTest.BackUp("10.4.6.48", 5432, "postgres", "123456", "D://source_db_dump.custom", "tin01_db");
  24. //processTest.BackUpSingleTable("localhost", 5432, "postgres", "123456", "D://source_db_dump.custom", "FROMTIN", "20250630.PM1");
  25. //processTest.BackUpByDateTime("localhost", 5432, "postgres", "123456", "D://source_db_dump.custom", "FROMTIN", "20250630");
  26. //processTest.Restore("localhost", 5432, "postgres", "123456", "D://source_db_dump.custom", "DBTestTIN");
  27. }
  28. }
  29. internal class ProcessTest
  30. {
  31. //HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql*
  32. private string? _binPath;
  33. private string? pg_dump;
  34. private string? pg_restore;
  35. public bool Initialize()
  36. {
  37. RegistryKey key = Registry.LocalMachine;
  38. key = key.OpenSubKey("SOFTWARE\\PostgreSQL\\Installations");
  39. if (key is null)
  40. key = key.OpenSubKey("SOFTWARE\\WOW6432Node\\PostgreSQL\\Installations");
  41. if (key is null)
  42. return false;
  43. if (key.GetSubKeyNames().FirstOrDefault(t => t.Contains("postgresql")) is not string nextKey)
  44. return false;
  45. key = key.OpenSubKey(nextKey);
  46. if (key.GetValue("Base Directory") is not string path)
  47. return false;
  48. this.pg_dump = Path.Combine(path, "bin", "pg_dump.exe");
  49. this.pg_restore = Path.Combine(path, "bin", "pg_restore.exe");
  50. return true;
  51. }
  52. public bool BackUp(string serverAddress, int port, string userName, string password, string savePath, string dbName)
  53. {
  54. if (string.IsNullOrEmpty(this.pg_dump))
  55. return false;
  56. Process process = new Process();
  57. process.StartInfo.Environment.Add("PGPASSWORD", password);
  58. process.StartInfo.FileName = this.pg_dump;
  59. process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" {dbName}");
  60. process.StartInfo.CreateNoWindow = false;
  61. process.Start();
  62. process.WaitForExit();
  63. return true;
  64. }
  65. public bool Restore(string serverAddress, int port, string userName, string password, string savePath, string dbName)
  66. {
  67. if (string.IsNullOrEmpty(this.pg_restore))
  68. return false;
  69. Process process = new Process();
  70. process.StartInfo.Environment.Add("PGPASSWORD", password);
  71. process.StartInfo.FileName = this.pg_restore;
  72. process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -v -d {dbName} --clean \"{savePath}\"");
  73. process.StartInfo.CreateNoWindow = false;
  74. process.Start();
  75. process.WaitForExit();
  76. return true;
  77. }
  78. public bool BackUpSingleTable(string serverAddress, int port, string userName, string password, string savePath, string dbName, string tableName)
  79. {
  80. if (string.IsNullOrEmpty(this.pg_dump))
  81. return false;
  82. Process process = new Process();
  83. process.StartInfo.RedirectStandardOutput = true;
  84. process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
  85. process.StartInfo.Environment.Add("PGPASSWORD", password);
  86. process.StartInfo.FileName = this.pg_dump;
  87. process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" --table \"{tableName}\" {dbName}");
  88. process.StartInfo.CreateNoWindow = false;
  89. process.Start();
  90. process.WaitForExit();
  91. return true;
  92. }
  93. public bool BackUpByDateTime(string serverAddress, int port, string userName, string password, string savePath, string dbName, string time)
  94. {
  95. if (string.IsNullOrEmpty(this.pg_dump))
  96. return false;
  97. Process process = new Process();
  98. process.StartInfo.RedirectStandardOutput = true;
  99. process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
  100. process.StartInfo.Environment.Add("PGPASSWORD", password);
  101. process.StartInfo.FileName = this.pg_dump;
  102. process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" --table {time}* {dbName}");
  103. process.StartInfo.CreateNoWindow = false;
  104. process.Start();
  105. process.WaitForExit();
  106. return true;
  107. }
  108. }
  109. internal class CompressTest
  110. {
  111. public void Test()
  112. {
  113. using MemoryStream stream = new();
  114. Compressor.CompressZipFileDirectory(new(@"E:\Recipes"), stream);
  115. Console.WriteLine(this.SplitSpan(stream, CallBack) ? "Send Success" : "Send Failed");
  116. }
  117. private bool CallBack(byte[] input, int current, int total)
  118. {
  119. Console.WriteLine($"{input.Length} {current} / {total}");
  120. return true;
  121. }
  122. private bool SplitSpan(MemoryStream stream, Func<byte[], int, int, bool> callBack, int packLength = 4096)
  123. {
  124. if (packLength < 256)
  125. return false;
  126. Span<byte> t = stream.ToArray();
  127. int count = t.Length / packLength;
  128. Span<byte> ts;
  129. for (int i = 0; i <= count; i++)
  130. {
  131. if (i == count)
  132. {
  133. ts = t.Slice(i * packLength);
  134. if (callBack?.Invoke(ts.ToArray(), i, count) != true)
  135. return false;
  136. break;
  137. }
  138. ts = t.Slice(i * packLength, packLength);
  139. if (callBack?.Invoke(ts.ToArray(), i, count) != true)
  140. return false;
  141. }
  142. return true;
  143. }
  144. }