[SQLServer]NetCore中將SQLServer資料庫備份為Sql指令碼

胖紙不爭發表於2022-05-21

NetCore中將SQLServer資料庫備份為Sql指令碼

描述:

最近寫專案收到了一個需求, 就是將SQL Server資料庫備份為Sql指令碼, 如果是My Sql之類的還好說, 但是在網上搜了一大堆, 全是教你怎麼操作SSMS的, 就很d疼!

解決方案:

通過各種查詢資料, 還有一些老哥的幫助, 找到了解決方案:

通過Microsoft.SqlServer.Management.Smo, Microsoft.SqlServer.Management.Sdk.Sfc, Microsoft.SqlServer.Management.Common來解決, 但是不巧的是, 這個方法可能只適用於.Net Framework, 並且微軟已經提供一個合集的類庫封裝為Microsoft.SqlServer.Scripts. 但是我是一個Net5的專案!

但是最後還是找到了, 微軟封裝了一個其它包...emmMicrosoft.SqlServer.SqlManagementObjects, 此類庫可以適用於Net Core.

By: 胖紙不爭
NetCore?群: 743336452

基本使用

Server server = new Server(
    new ServerConnection(
        // 伺服器IP
        _dbBackupOptions.ServerInstance,
        // 登入名
        _dbBackupOptions.Username,
        // 密碼
        _dbBackupOptions.Password
        )
);
// 獲取資料庫
Database templateDb = server.Databases[_dbBackupOptions.DatabaseName];
// 指令碼匯出路徑
string sqlFilePath = string.Format("{0}.sql", $"{dbBackupPath}/{name}");
// 自定義規則
var startWith = _dbBackupOptions.FormatTables.Where(x => x.EndsWith("*")).Select(x => x.TrimEnd('*'));
var endWith = _dbBackupOptions.FormatTables.Where(x => x.StartsWith("*")).Select(x => x.TrimStart('*'));

if (_dbBackupOptions.FormatTables is not null && _dbBackupOptions.FormatTables.Any())
{
    foreach (Table tb in templateDb.Tables)
    {
        if (_dbBackupOptions.FormatTables.Contains(tb.Name) ||
            startWith.Where(x => tb.Name.StartsWith(x)).Any() ||
            endWith.Where(x => tb.Name.EndsWith(x)).Any())
        {
            // 按表獲取Sql
            IEnumerable<string> sqlStrs = tb.EnumScript(_dbBackupOptions.ScriptingOptions);
            // 將Sql向檔案中追加
            using (StreamWriter sw = new StreamWriter(sqlFilePath, true, Encoding.UTF8))
            {
                foreach (var sql in sqlStrs)
                {
                    sw.WriteLine(sql);
                    sw.WriteLine("GO");
                }
            }
        }
    }
}
else
{
    foreach (Table tb in templateDb.Tables)
    {
        IEnumerable<string> sqlStrs = tb.EnumScript(_dbBackupOptions.ScriptingOptions);
        using (StreamWriter sw = new StreamWriter(sqlFilePath, true, Encoding.UTF8))
        {
            foreach (var sql in sqlStrs)
            {
                sw.WriteLine(sql);
                sw.WriteLine("GO");
            }
        }
    }
}

開箱即用(封裝庫Powers.DbBackup)

我針對這個封裝了一個類庫, Powers.DBackup方便簡單使用.

GitHub地址: Powers.DbBackup

配置DbBackup

1. In Startup.cs(Net5):

services.AddDbBackup();

appsettings.json:

"DbBackupOptions": {
    // remote server
    "ServerInstance": "192.168.31.36",
    // database username
    "Username": "sa",
    // password
    "Password": "sa123.",
    // ddatabase name
    "DatabaseName": "PumInfoShop",
    // output options
    "ScriptingOptions": {
      "DriAll": false,
      "ScriptSchema": true,
      "ScriptData": true,
      "ScriptDrops": false
    },
    // match rules
    /**
     * Include 3 rules:
     * 1. Full name: UserTable
     * 2. Start with: Sys*
     * 3. End with: *Table
     */
    "FormatTables": []
  }

OR

services.AddDbBackup(opts =>
{
    opts.ServerInstance = "127.0.0.1";
    opts.Username = "sa";
    opts.Password = "123456";
    opts.DatabaseName = "TestDb";
    opts.ScriptingOptions = new ScriptingOptions
    {
        DriAll = true,
        ScriptSchema = true,
        ScriptData = true,
        ScriptDrops = false
    };
    /**
     * Include 3 rules:
     * 1. Full name: UserTable
     * 2. Start with: Sys*
     * 3. End with: *Table
     */
    opts.FormatTables = new string[] { "Sys*", "Log*", "UserTable", "*Table" };
});
// Or this way
//services.AddDbBackup(opts => new DbBackupOptions
//{
//    ServerInstance = "127.0.0.1",
//    Username = "sa",
//    // .....
//});

2. In Program.cs(Net6):

builder.Services.AddDbBackup();

appsettings.json:

"DbBackupOptions": {
    "ServerInstance": "192.168.31.36",
    "Username": "sa",
    "Password": "sa123.",
    "DatabaseName": "PumInfoShop",
    "ScriptingOptions": {
      "DriAll": false,
      "ScriptSchema": true,
      "ScriptData": true,
      "ScriptDrops": false
    },
    "FormatTables": []
  }

OR

builder.Services.AddDbBackup(opts =>
{
    opts.ServerInstance = "127.0.0.1";
    opts.Username = "sa";
    opts.Password = "123456";
    opts.DatabaseName = "TestDb";
    opts.ScriptingOptions = new ScriptingOptions
    {
        DriAll = true,
        ScriptSchema = true,
        ScriptData = true,
        ScriptDrops = false
    };
    /**
     * Include 3 rules:
     * 1. Full name: UserTable
     * 2. Start with: Sys*
     * 3. End with: *Table
     */
    opts.FormatTables = new string[] { "Sys*", "Log*", "UserTable", "*Table" };
});

// Or this way
//builder.Services.AddDbBackup(opts => new DbBackupOptions
//{
//    ServerInstance = "127.0.0.1",
//    Username = "sa",
//    // .....
//});

使用方法

[HttpGet]
public async Task<ActionResult> StartDbBackup()
{
    var rootPath = "D:/";
    var fileName = DateTime.Now.ToString("yyyyMMddhhmmss"); // No ".sql" suffix is required.
    var (path, size) = await DbBackupExtensions.StartBackupAsync(rootPath, fileName);// path is full path

    return Ok(new
    {
        Path = path,
        Size = size
    });
}

[HttpGet]
public async Task<ActionResult> DeleteDbBackup(string filePath)
{
    var (res, msg) = await DbBackupExtensions.DeleteBackup(filePath);

    if (res)
    {
        return Ok(msg);
    }
    else
    {
        return NotFound(msg);
    }
}

相關文章