Msbuild任务恢复SQL Server数据库

时间:2009-01-07 10:06:48

标签: sql-server msbuild

什么是msbuild任务的快速示例/演示,删除然后还原数据库,还可以对恢复的数据库运行任何.sql文件?

4 个答案:

答案 0 :(得分:3)

使用ExecTask并调用sqlcmd.exe或cmd脚本,以便对数据库执行任何操作。

答案 1 :(得分:2)

我们使用ExecTask和powershell脚本。相关的Powershell代码在这里:

$Server = new-object ("Microsoft.SqlServer.Management.Smo.Server") $ServerName
$Database = new-object ("Microsoft.SqlServer.Management.Smo.Database") ($Server, $DatabaseName)

function RestoreDb([string] $BackupFile =  $(throw "Backup file required."))
{

  write-host "Taking db offline"
  $Database.SetOffline()
  $restore = new-object ('Microsoft.SqlServer.Management.Smo.Restore')


  $fil=new-object "Microsoft.SqlServer.Management.Smo.BackupDeviceItem"
  $fil.DeviceType='File'
  $fil.Name= $BackupFile
  $restore.Action="Database"
  $restore.Devices.Add($fil)
  $restore.Database=$DatabaseName
  $restore.ReplaceDatabase = $true
  $restore.Restart  = $true
  write-host "Restoring database $DatabaseName  on  $ServerName from file $BackupFile"
  $restore.SqlRestore($Server)
  write-host "Taking db online"
  $Database.SetOnline()
}

相关的MSBuild

<Exec WorkingDirectory="$(BuildRoot)"   Command="powershell -command  .\run_migration.ps1"/>

答案 2 :(得分:2)

以下是我编写的msbuild任务的大纲:

    public class RestoreDb : Task
    {
        #region RequiredParameters
        [Required]
        public string ServerName
        {
            get
            {
                return m_ServerName;
            }
            set
            ...
        }
        [Required]
        public string DbName
        ...
        public string BackupFileName
        ...
        #endregion
        public override bool Execute()
        {
            try
            {
                SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder();
                connectionStringBuilder.IntegratedSecurity = true;
                connectionStringBuilder.DataSource = ServerName;
                SqlConnection connection = new SqlConnection(connectionStringBuilder.ToString());
                connection.Open();

                Server server = new Server(new ServerConnection(connection));

                if (server.Databases[DbName] != null)
                {
                    Log.LogMessage("Dropping existing " + DbName + " on " + ServerName);
                    server.Databases[DbName].Drop();
                }
                else
                {
                    Log.LogMessage(DbName + " on " + ServerName + " doesn't exist.");
                }

                Log.LogMessage(MessageImportance.High, "Restoring " + DbName + " on " + ServerName);
                Database newDb = new Database(server, DbName);

                Restore rs = new Restore();
                rs.NoRecovery = false;
                FileInfo fi = new FileInfo(server.Settings.BackupDirectory + "\\" + BackupFileName);
                BackupDeviceItem bdi = new BackupDeviceItem(fi.FullName, DeviceType.File);
                rs.Devices.Add(bdi);
                rs.Database = DbName;
                rs.Action = RestoreActionType.Database;

                rs.SqlRestore(server);
                Log.LogMessage(MessageImportance.High, "Restoring done.");
            }
            catch( Exception exc)
            {
                Log.LogErrorFromException(exc);
            }
            return !Log.HasLoggedErrors;
        }
        private string m_DbName;
        ...
    }
}

用作:

<UsingTask AssemblyFile="bin\Release\MyTask.dll" TaskName="RestoreDb" />
    <RestoreDb ServerName="localhost\sql2005" DbName="myDb" BackupFileName="myDb.bak"/>

欢迎任何和所有评论!

答案 3 :(得分:2)

我使用MSBuild扩展包MSBuild extension pack来修改配置文件,创建应用程序池,甚至发送电子邮件。