123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using Microsoft.Win32;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
- namespace DBBackupTool.Helpers;
- internal class PostgreSQLTool
- {
- //HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql*
- private string? pg_dump;
- private string? pg_restore;
- public bool Initialize()
- {
- RegistryKey? key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\PostgreSQL\\Installations");
- key ??= Registry.LocalMachine.OpenSubKey("SOFTWARE\\WOW6432Node\\PostgreSQL\\Installations");
- if (key is null)
- return false;
- if (key.GetSubKeyNames().FirstOrDefault(t => t.Contains("postgresql")) is not string nextKey)
- return false;
- key = key.OpenSubKey(nextKey);
- if (key?.GetValue("Base Directory") is not string path)
- return false;
- this.pg_dump = Path.Combine(path, "bin", "pg_dump.exe");
- this.pg_restore = Path.Combine(path, "bin", "pg_restore.exe");
- return true;
- }
- public bool BackUp(string serverAddress, int port, string userName, string password, string savePath, string dbName)
- {
- if (string.IsNullOrEmpty(this.pg_dump))
- return false;
- Process process = new();
- process.StartInfo.Environment.Add("PGPASSWORD", password);
- process.StartInfo.FileName = this.pg_dump;
- process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" {dbName}");
- process.StartInfo.CreateNoWindow = true;
- process.Start();
- process.WaitForExit();
- return true;
- }
- public bool Restore(string serverAddress, int port, string userName, string password, string savePath, string dbName)
- {
- if (string.IsNullOrEmpty(this.pg_restore))
- return false;
- Process process = new();
- process.StartInfo.Environment.Add("PGPASSWORD", password);
- process.StartInfo.FileName = this.pg_restore;
- process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -v -d {dbName} --clean \"{savePath}\"");
- process.StartInfo.CreateNoWindow = true;
- process.Start();
- process.WaitForExit();
- return true;
- }
- public bool BackUpSingleTable(string serverAddress, int port, string userName, string password, string savePath, string dbName, string tableName)
- {
- if (string.IsNullOrEmpty(this.pg_dump))
- return false;
- Process process = new Process();
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
- process.StartInfo.Environment.Add("PGPASSWORD", password);
- process.StartInfo.FileName = this.pg_dump;
- process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" --table \"{tableName}\" {dbName}");
- process.StartInfo.CreateNoWindow = false;
- process.Start();
- process.WaitForExit();
- return true;
- }
- public bool BackUpByDateTime(string serverAddress, int port, string userName, string password, string savePath, string dbName, string time)
- {
- if (string.IsNullOrEmpty(this.pg_dump))
- return false;
- Process process = new Process();
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
- process.StartInfo.Environment.Add("PGPASSWORD", password);
- process.StartInfo.FileName = this.pg_dump;
- process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" --table {time}* {dbName}");
- process.StartInfo.CreateNoWindow = false;
- process.Start();
- process.WaitForExit();
- return true;
- }
- }
|