服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2024-10-16 a592da60f0db0d4eb950a81a8530e965444be7b1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Apache.NMS.ActiveMQ.Commands;
using Rhea.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tiger.IBusiness;
 
namespace Tiger.Business
{
    public class DbBackup : IDbBackup
    {
        /// <summary>
        /// 运行备份数据库bat文件
        /// </summary>
        /// <param name="path"></param>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiAction> ExcuteBackupBat(string path)
        {
            var action = new ApiAction();
            if (!File.Exists(path))
            {
                //Console.WriteLine("文件不存在!");
                return null;
            }
 
            StringBuilder builder = new StringBuilder();
            try
            {
                Process process = new Process();
                FileInfo fileInfo = new FileInfo(path);
                process.StartInfo.WorkingDirectory = fileInfo.Directory.FullName;
                //关闭 shell 文件路径不一样
                process.StartInfo.FileName = path;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true; //重定向输入
                process.StartInfo.RedirectStandardOutput = true; //重定向标准输出 
                process.StartInfo.RedirectStandardError = true; //重定向错误输出
                process.StartInfo.CreateNoWindow = true;
                process.Start();
 
                //
                StreamReader reader = process.StandardOutput;
                string line = "";//每次读取一行
                while ((line = reader.ReadLine()) != null)
                {
                    //Console.WriteLine("message:" + line);
                    builder.AppendLine(line);
                }
 
                process.WaitForExit();
                process.Close();
                reader.Close();
                action.Message = builder.ToString();
                Logger.Interface.Info($"执行备份数据库批处理脚本信息: {builder}");
            }
            catch (Exception ex)
            {
                action.CatchExceptionWithLog(ex, $"执行备份数据库批处理脚本出错");
            }
            return action;
        }
    }
}