Add configuration backup and import
This commit is contained in:
+115
-2
@@ -28,10 +28,13 @@ public sealed class MainForm : Form
|
||||
private Button _btnStart = null!;
|
||||
private Button _btnReload = null!;
|
||||
private Button _btnStop = null!;
|
||||
private Button _btnBackup = null!;
|
||||
private Button _btnImport = null!;
|
||||
private Button _btnRefreshStatus = null!;
|
||||
private Button _btnSavePortal = null!;
|
||||
|
||||
private string? _selectedDomain;
|
||||
private bool _closingBackupCreated;
|
||||
|
||||
public MainForm(NginxManager manager)
|
||||
{
|
||||
@@ -79,6 +82,7 @@ public sealed class MainForm : Form
|
||||
root.Controls.Add(CreateRouteListPanel(), 0, 2);
|
||||
root.Controls.Add(CreateEditorPanel(), 0, 3);
|
||||
Controls.Add(root);
|
||||
FormClosing += OnFormClosingBackup;
|
||||
}
|
||||
|
||||
private Control CreateHeaderPanel()
|
||||
@@ -316,12 +320,16 @@ public sealed class MainForm : Form
|
||||
_btnStart = CreateButton("启动代理", 110, Color.FromArgb(37, 123, 80), Color.White);
|
||||
_btnReload = CreateButton("重新加载", 110);
|
||||
_btnStop = CreateButton("停止代理", 110, Color.FromArgb(177, 53, 53), Color.White);
|
||||
_btnBackup = CreateButton("手动备份", 110);
|
||||
_btnImport = CreateButton("导入备份", 110);
|
||||
_btnRender.Click += async (s, e) => await RenderOnlyAsync();
|
||||
_btnStart.Click += async (s, e) => await RunNginxAsync("启动", _manager.Start, expectedRunning: true);
|
||||
_btnReload.Click += async (s, e) => await RunNginxAsync("重新加载", _manager.Reload, expectedRunning: true);
|
||||
_btnStop.Click += async (s, e) => await RunNginxAsync("停止", _manager.Stop, expectedRunning: false);
|
||||
actions.Controls.AddRange([_btnRender, _btnStart, _btnReload, _btnStop]);
|
||||
return CreateSectionPanel("Nginx 操作", actions);
|
||||
_btnBackup.Click += (s, e) => ManualBackup();
|
||||
_btnImport.Click += (s, e) => ImportBackup();
|
||||
actions.Controls.AddRange([_btnRender, _btnStart, _btnReload, _btnStop, _btnBackup, _btnImport]);
|
||||
return CreateSectionPanel("Nginx 与数据", actions);
|
||||
}
|
||||
|
||||
private Control CreateSectionPanel(string title, Control content)
|
||||
@@ -446,6 +454,8 @@ public sealed class MainForm : Form
|
||||
_btnStart.Enabled = true;
|
||||
_btnReload.Enabled = false;
|
||||
_btnStop.Enabled = false;
|
||||
_btnBackup.Enabled = true;
|
||||
_btnImport.Enabled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -458,6 +468,8 @@ public sealed class MainForm : Form
|
||||
_btnStart.Enabled = !isRunning;
|
||||
_btnReload.Enabled = isRunning;
|
||||
_btnStop.Enabled = isRunning;
|
||||
_btnBackup.Enabled = true;
|
||||
_btnImport.Enabled = true;
|
||||
}
|
||||
|
||||
private async Task RunNginxAsync(string actionName, Func<int> action, bool expectedRunning)
|
||||
@@ -507,9 +519,110 @@ public sealed class MainForm : Form
|
||||
_btnStart.Enabled = enabled;
|
||||
_btnReload.Enabled = enabled;
|
||||
_btnStop.Enabled = enabled;
|
||||
_btnBackup.Enabled = enabled;
|
||||
_btnImport.Enabled = enabled;
|
||||
_btnRefreshStatus.Enabled = enabled;
|
||||
}
|
||||
|
||||
private void ManualBackup()
|
||||
{
|
||||
try
|
||||
{
|
||||
var path = _manager.CreateManualBackup();
|
||||
MessageBox.Show(
|
||||
$"备份已完成:\n{path}\n\n备份目录最多保留最新 10 个文件。",
|
||||
"NginxMan",
|
||||
MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Information);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"备份失败:\n{ex.Message}", "NginxMan", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void ImportBackup()
|
||||
{
|
||||
using var dialog = new OpenFileDialog
|
||||
{
|
||||
Filter = "NginxMan 备份或配置 (*.zip;*.json)|*.zip;*.json|ZIP 备份包 (*.zip)|*.zip|JSON 配置 (*.json)|*.json",
|
||||
CheckFileExists = true,
|
||||
InitialDirectory = _manager.BackupDirectory,
|
||||
RestoreDirectory = true
|
||||
};
|
||||
|
||||
if (dialog.ShowDialog(this) != DialogResult.OK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var confirm = MessageBox.Show(
|
||||
"导入会替换当前配置,并重新生成入口页与 Nginx 配置。\n\n程序会先自动创建一份“导入前”备份。是否继续?",
|
||||
"确认导入",
|
||||
MessageBoxButtons.YesNo,
|
||||
MessageBoxIcon.Question,
|
||||
MessageBoxDefaultButton.Button2);
|
||||
if (confirm != DialogResult.Yes)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var wasRunning = _manager.IsRunning;
|
||||
var result = _manager.ImportBackup(dialog.FileName);
|
||||
RefreshBasicInfo();
|
||||
LoadDataGrid();
|
||||
ClearForm();
|
||||
RefreshStatus();
|
||||
|
||||
var reloadTip = wasRunning
|
||||
? "\n\nNginx 当前正在运行,请点击“重新加载”使导入的代理配置生效。"
|
||||
: string.Empty;
|
||||
MessageBox.Show(
|
||||
$"导入完成,共恢复 {result.RouteCount} 条映射。\n导入前备份:\n{result.SafetyBackupPath}{reloadTip}",
|
||||
"NginxMan",
|
||||
MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Information);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"导入失败:\n{ex.Message}", "NginxMan", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnFormClosingBackup(object? sender, FormClosingEventArgs e)
|
||||
{
|
||||
if (_closingBackupCreated)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_manager.CreateAutomaticBackup();
|
||||
_closingBackupCreated = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (e.CloseReason is CloseReason.WindowsShutDown or CloseReason.TaskManagerClosing)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var result = MessageBox.Show(
|
||||
$"关闭前自动备份失败:\n{ex.Message}\n\n是否仍然关闭程序?",
|
||||
"NginxMan",
|
||||
MessageBoxButtons.YesNo,
|
||||
MessageBoxIcon.Warning,
|
||||
MessageBoxDefaultButton.Button2);
|
||||
if (result != DialogResult.Yes)
|
||||
{
|
||||
e.Cancel = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RenderOnlyAsync()
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user