From bdf841ad4b132f38add3046d9dd7eb9bd013457d Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Fri, 5 May 2023 11:24:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=91=E5=B8=83=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PublishSite/PublishForm.cs | 86 ++++++++++++++++++++++-- PublishSite/appsettings.Development.json | 4 +- PublishSite/appsettings.Production.json | 2 +- PublishSite/appsettings.Verify.json | 4 +- PublishSite/appsettings.local.json | 2 +- 5 files changed, 88 insertions(+), 10 deletions(-) diff --git a/PublishSite/PublishForm.cs b/PublishSite/PublishForm.cs index 82fffc54..f990483a 100644 --- a/PublishSite/PublishForm.cs +++ b/PublishSite/PublishForm.cs @@ -247,15 +247,89 @@ namespace PublishSite } else { - using (var client = new SshClient(Host, UserName, Password)) { - var backRootPath = jObjectCofig[] + // 连接到远程服务器 + client.Connect(); - // 判断文件夹是否存在 - var backupFolderPath = "/path/to/backup/folder"; - var command = $"if [ ! -d '{backupFolderPath}' ]; then mkdir {backupFolderPath}; fi"; - var result = client.RunCommand(command); + try + { + var dbName = bakDbName.Text.Trim(); + var backRootPath = backPathTBox.Text.Trim(); + var serviceName = jObjectCofig["DefaultService"]["ServiceName"].ToString().Trim(); + var programFolder = backPublishPath.Text.Trim(); + + var dbUser = jObjectCofig["BackUp"]["DBUser"].ToString().Trim(); + var dbPwd = jObjectCofig["BackUp"]["DBPwd"].ToString().Trim(); + + + var backupFolderPath = Path.Combine(backRootPath, DateTime.Now.ToString("yyyyMMdd")); + + //创建文件夹 + WinformLog("准备当日备份文件夹..."); + + var command = client.CreateCommand($"if not exist \"{backupFolderPath}\" mkdir \"{backupFolderPath}\""); + command.Execute(); + if (command.ExitStatus != 0) + { + throw new Exception($"Failed to create {backupFolderPath}."); + } + + + ////停止服务 + //WinformLog($"停止后台服务{serviceName}..."); + + //var stopServiceCommand = client.CreateCommand($"sc stop \"{serviceName}\""); + //command.Execute(); + + //if (command.ExitStatus != 0) + //{ + // throw new Exception($"Failed to stop {serviceName}."); + //} + + + ////备份后端文件 + + //WinformLog($"备份后端文件..."); + //string archivePath = $"{backupFolderPath}\\NetCore.zip"; + //command = client.CreateCommand($"powershell.exe Compress-Archive -Path \"{programFolder}\\*.*\" -DestinationPath \"{archivePath}\" -Force"); + //command.Execute(); + //if (command.ExitStatus != 0) + //{ + // throw new Exception("Failed to compress and move folder contents."); + //} + + ////启动服务 + //WinformLog($"停止后台服务{serviceName}..."); + + //var startServiceCommand = client.CreateCommand($"sc start \"{serviceName}\""); + //command.Execute(); + + //if (command.ExitStatus != 0) + //{ + // throw new Exception($"Failed to start {serviceName}."); + //} + + + //备份数据库 + + WinformLog($"开始备份数据库{dbName}..."); + + string databaseBackupPath = $"{backupFolderPath}\\db.bak"; + command = client.CreateCommand($"sqlcmd -S . -U \"{dbUser}\" -P \"{dbPwd}\" -Q \"BACKUP DATABASE [{dbName}] TO DISK='{databaseBackupPath}' WITH INIT\""); + command.Execute(); + if (command.ExitStatus != 0) + { + throw new Exception("Failed to backup database."); + } + + + } + catch (Exception ex) + { + + WinformLog($"{ex.Message}", Color.Red); + } } } diff --git a/PublishSite/appsettings.Development.json b/PublishSite/appsettings.Development.json index 1c3de457..b35b0280 100644 --- a/PublishSite/appsettings.Development.json +++ b/PublishSite/appsettings.Development.json @@ -1,8 +1,10 @@ { "BackUp": { "DataBaseName": "IRaCIS_New_Tet", + "DBUser": "sa", + "DBPwd": "123456", "BackPath": "D:\\Develop\\PublishSite\\发布备份", - "ProgramRootPath": "D:\\Develop\\PublishSite\\发布备份", + "ProgramRootPath": "D:\\Develop\\PublishSite\\IRaCIS.NetCore.API", "ConnectionStrings": "Server=.;Database=IRaCIS;Trusted_Connection=True;TrustServerCertificate=true" }, "Credential": { diff --git a/PublishSite/appsettings.Production.json b/PublishSite/appsettings.Production.json index f091dac3..9f44c6bd 100644 --- a/PublishSite/appsettings.Production.json +++ b/PublishSite/appsettings.Production.json @@ -1,7 +1,7 @@ { "BackUp": { "DataBaseName": "IRaCIS_New_Tet", - "FullDBRootPath": "Warning", + "BackPath": "Warning", "ProgramRootPath": "D:\\Develop\\PublishSite\\发布备份", "ConnectionStrings": "Server=.;Database=IRaCIS;Trusted_Connection=True;TrustServerCertificate=true" }, diff --git a/PublishSite/appsettings.Verify.json b/PublishSite/appsettings.Verify.json index f091dac3..c7c6ab34 100644 --- a/PublishSite/appsettings.Verify.json +++ b/PublishSite/appsettings.Verify.json @@ -1,7 +1,9 @@ { "BackUp": { "DataBaseName": "IRaCIS_New_Tet", - "FullDBRootPath": "Warning", + "DBUser": "sa", + "DBPwd": "123456", + "BackPath": "Warning", "ProgramRootPath": "D:\\Develop\\PublishSite\\发布备份", "ConnectionStrings": "Server=.;Database=IRaCIS;Trusted_Connection=True;TrustServerCertificate=true" }, diff --git a/PublishSite/appsettings.local.json b/PublishSite/appsettings.local.json index f091dac3..9f44c6bd 100644 --- a/PublishSite/appsettings.local.json +++ b/PublishSite/appsettings.local.json @@ -1,7 +1,7 @@ { "BackUp": { "DataBaseName": "IRaCIS_New_Tet", - "FullDBRootPath": "Warning", + "BackPath": "Warning", "ProgramRootPath": "D:\\Develop\\PublishSite\\发布备份", "ConnectionStrings": "Server=.;Database=IRaCIS;Trusted_Connection=True;TrustServerCertificate=true" },