Coder Social home page Coder Social logo

maremare / dbbackuprestore Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 82 KB

Tool to perform a full backup of a SQL Server database and restore it to a new location. SQL Server データベースの完全バックアップ、および新しい場所への復元を行うツールです。

License: MIT License

C# 100.00%
csharp net6 net60 sqlserver tool

dbbackuprestore's Introduction

DbBackupRestore

SQL Server データベースの完全バックアップ、および新しい場所への復元を行うツールです。

データベースの完全バックアップ

構成ファイル に定義された各データベースの完全バックアップをバックアップファイルとして作成します。

  • 実行例
    .\BackupSqlDatabase.exe
  • 実行されるSQL例
    BACKUP DATABASE @databaseName
      TO DISK = @backupFilePath WITH NOFORMAT
    , NAME = @description
    , INIT
    , SKIP
    , NOREWIND
    , NOUNLOAD
    , STATS = 10;

データベースの新しい場所への復元

構成ファイル に定義された各データベースをバックアップファイルより定義された場所へ復元します。

  • 実行例
    .\RestoreSqlDatabase.exe
  • 実行されるSQL例
    RESTORE FILELISTONLY FROM DISK = @backupFilePath;
    
    ALTER DATABASE @databaseName SET OFFLINE WITH ROLLBACK IMMEDIATE;
    
    RESTORE DATABASE @databaseName
      FROM DISK = @backupFilePath WITH REPLACE
    , NOUNLOAD
    , STATS = 5
    , MOVE N'{logicalName1}' TO N'{moveToFilePath1}'
    , MOVE N'{logicalName2}' TO N'{moveToFilePath2}';
    
    ALTER DATABASE @databaseName SET ONLINE;

バックアップ先と復元先フォルダのアクセス権について

バックアップ先と復元先フォルダには SQL Server に対してアクセス許可を与える必要があります。

両ツールを実行するとそれぞれ対象となるフォルダ対して NT Service\MSSQL$インスタンス名 にフルコントロールのアクセス権を付与します。

構成ファイル の 'SqlServerAccount' 設定値が MSSQL$SQLEXPRESS であれば NT Service\MSSQL$SQLEXPRESS にフルコントロールを付与します。

構成ファイル

データベースの完全バックアップ、およびおよび新しい場所への復元の両ツールで同一の構成ファイルを使用します。

// appsettings.json
{
  // ...
  "SqlDatabaseOptions": {
    "ConnectionString": "Data Source=localhost;Integrated Security=True",
    "SqlServerAccount": "MSSQL$SQLEXPRESS",
    "BackupDirectory": "C:\\DB\\BACKUP",
    "RestoreDirectory": "C:\\DB\\RESTORE",
    "ArchiveDirectory": "\\\\fileserver\\share\\backup",
    "CommandTimeoutSeconds": 60,
    "Databases": [
      { "Name": "DB1" },
      { "Name": "DB2" },
      { "Name": "DB3" }
    ]
  },
  // ...
}
appsettings.json の完全な例:
{
  "SqlDatabaseOptions": {
    "ConnectionString": "Data Source=localhost;Integrated Security=True",
    "SqlServerAccount": "MSSQL$SQLEXPRESS",
    "BackupDirectory": "C:\\DB\\BACKUP",
    "RestoreDirectory": "C:\\DB\\RESTORE",
    "ArchiveDirectory": "\\\\fileserver\\share\\backup",
    "CommandTimeoutSeconds": 60,
    "Databases": [
      { "Name": "DB1" },
      { "Name": "DB2" },
      { "Name": "DB3" }
    ]
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}
要素名 説明 設定例
ConnectionString 接続文字列 Data Source=localhost;Integrated Security=True
SqlServerAccount SQL Server サービスアカウント MSSQL$SQLEXPRESS
BackupDirectory *.bak の格納先フォルダ C:\\DB\\BACKUP
ローカルフォルダのみ
RestoreDirectory *.bak の復元先フォルダ C:\\DB\\RESTORE
ローカルフォルダのみ
要アクセス権
ArchiveDirectory *.zip の格納先フォルダ \\\\fileserver\\share\\backup
CommandTimeoutSeconds コマンドタイムアウト秒 60
既定値は60
Databases データベース設定の配列
Databases:Name データベース名 DB1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.