删除具有活动租约的备份 Blob 文件Delete backup blob files with active leases

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions)

在备份到 Microsoft Azure 存储或从中还原时,SQL Server 获得无限期租约以锁定对 blob 的独占访问。When backing up to or restoring from Microsoft Azure storage, SQL Server acquires an infinite lease to lock exclusive access to the blob. 当成功完成备份或还原过程时,释放租约。When the backup or restore process is successfully completed, the lease is released. 如果备份或还原失败,备份过程将尝试清除所有无效的 blob。If a backup or restore fails, the backup process attempts to clean up any invalid blobs. 但是,如果由于持续很长时间的网络连接故障而导致备份失败,备份过程可能无法再次访问 blob 且 blob 可能保持孤立状态。However, if the backup fails due to prolonged or sustained network connectivity failure, the backup process may not be able gain access to the blob and the blob may remain orphaned. 这意味着在释放租约前,不能写入或删除 blob。This means the blob cannot be written to or deleted until the lease is released. 本主题说明如何释放(中断)租约和删除 blob。This topic describes how to release (break) the lease and delete the blob.

有关租约类型的详细信息,请阅读此文章For more information on lease types, read this article.

如果备份操作失败,它可能生成无效的备份文件。If the backup operation fails, it can result in an invalid backup file. 备份 blob 文件可能还有活动租约,以防止其被删除或覆盖。The backup blob file might also have an active lease, preventing it from being deleted or overwritten. 若要删除或覆盖这类 blob,应首先释放(中断)租约。To delete or overwrite such blobs, the lease should first be released (broken). 如果备份失败,我们建议你清除租约并删除 blob。If there are backup failures, we recommend that you clean up leases and delete blobs. 还可以定期清除租约并删除作为存储管理任务的一部分的 blob。You can also periodically clean up leases and delete blobs as part of your storage management tasks.

如果还原失败,将不阻止后续还原,因此活动租约不会导致问题。If there is a restore failure, subsequent restores are not blocked, so active lease may not be an issue. 仅当必须覆盖或删除 blob 时,才有必要中断租约。Breaking the lease is only necessary when you have to overwrite or delete the blob.

管理孤立的 blobManage orphaned blobs

以下步骤说明在备份或还原活动失败后如何进行清除。The follow steps describe how to clean up after failed backup or restore activity. 可以使用 PowerShell 脚本来执行所有这些步骤。You can do all the steps using PowerShell scripts. 以下部分包括一个 PowerShell 脚本示例:The following section includes an example PowerShell script:

  1. 标识具有租约的 blob: 如果有运行备份过程的脚本或进程,可能可以捕获脚本或进程内的失败并使用它清除 blob。Identify blobs with leases: If you have a script or a process that runs the backup processes, you might be able to capture the failure within the script or process and use that to clean up the blobs. 还可以使用 LeaseStats 和 LeastState 属性来标识具有租约的 blob。You can also use the LeaseStats and LeastState properties to identify blobs with leases on them. 一旦标识了 blob,我们建议查看列表,在删除 blob 前验证备份文件的有效性。Once you have identified the blobs, review the list and verify the validity of the backup file before deleting the blob.

  2. 中断租约: 获得授权的请求可以中断租约而不提供租约 ID。Break the lease: An authorized request can break the lease without supplying a lease ID. 有关详细信息,请参阅 此处See here for more information.


    SQL Server 发出租约 ID 以在还原操作期间建立独占访问。SQL Server issues a lease ID to establish exclusive access during the restore operation. 还原租约 ID 是 BAC2BAC2BAC2BAC2BAC2BAC2BAC2BAC2。The restore lease ID is BAC2BAC2BAC2BAC2BAC2BAC2BAC2BAC2.

  3. 删除 Blob: 要删除具有活动租约的 blob,必须首先中断租约。Delete the Blob: To delete a blob with an active lease, you must first break the lease.

PowerShell 脚本示例PowerShell script example


如果您正在运行 PowerShell 2.0,可能遇到加载 Microsoft WindowsAzure.Storage.dll 程序集的问题。If you are running PowerShell 2.0, you may have problems loading the Microsoft WindowsAzure.Storage.dll assembly. 我们建议升级 Powershell 以解决该问题。We recommend that you upgrade Powershell to solve the issue. 还可使用以下解决方法,以使用以下语句创建或修改 powershell.exe.config 文件以在运行时加载 .NET 2.0 和 .NET 4.0 程序集:You can also use the following workaround to create or modify the powershell.exe.config file to load .NET 2.0 and .NET 4.0 assemblies at runtime with the following:

<?xml version="1.0"?>
        <startup useLegacyV2RuntimeActivationPolicy="true">
            <supportedRuntime version="v4.0.30319"/>
            <supportedRuntime version="v2.0.50727"/>

以下示例脚本标识具有活动租约的 blob,然后中断它们。The following example script identifies blobs with active leases and then breaks them. 该示例还演示如何为释放租约 ID 进行筛选。The example also demonstrates how filter for release lease IDs.

有关运行此脚本的提示Tips on running this script


如果在运行此脚本的同时执行备份到 Azure Blob 存储服务,则备份可能失败,因为此脚本将中断备份操作此时要获取的租约。If a backup to the Azure Blob storage service is running at the same time as this script, the backup can fail since this script will break the lease that the backup is trying to concurrently acquire. 在维护时段或没有正在执行或预计要运行的备份时运行此脚本。Run this script during a maintenance window or when no backups are running or expected to run.

  • 在运行此脚本之前,应为存储帐户、存储密钥、容器和 Azure 存储程序集路径和名称参数添加值。Before you run this script, you should add values for the storage account, storage key, container, and the Azure storage assembly path and name parameters. 存储程序集的路径为 SQL ServerSQL Server实例的安装目录。The path of the storage is assembly is the installation directory of the instance of SQL ServerSQL Server. 存储程序集的文件名为 Microsoft.WindowsAzure.Storage.dll。The file name for the storage assembly is Microsoft.WindowsAzure.Storage.dll.

  • 如果没有具有已锁定租约的 blob,你应看到以下消息:There are no blobs with locked lease statusIf there are no blobs with locked leases you should see the following message: There are no blobs with locked lease status

  • 如果有具有已锁定租约的 blob,你应看到以下消息:Breaking LeasesThe lease on <URL of the Blob> is a restore lease: You will see this message only if you have a blob with a restore lease that is still active.The lease on <URL of the Blob> is not a restore lease Breaking lease on <URL of the Bob>.If there are blobs with locked leases, you should see the following messages: Breaking Leases, The lease on <URL of the Blob> is a restore lease: You will see this message only if you have a blob with a restore lease that is still active., and The lease on <URL of the Blob> is not a restore lease Breaking lease on <URL of the Bob>.

$storageAccount = "<myStorageAccount>"
$storageKey = "<myStorageKey>"
$blobContainer = "<myBlobContainer>"
$storageAssemblyPathName = "<myStorageAssemblyPathName>"
# well known Restore Lease ID  
$restoreLeaseId = "BAC2BAC2BAC2BAC2BAC2BAC2BAC2BAC2"  
# load the storage assembly without locking the file for the duration of the PowerShell session  
$bytes = [System.IO.File]::ReadAllBytes($storageAssemblyPath)  
$cred = New-Object 'Microsoft.WindowsAzure.Storage.Auth.StorageCredentials' $storageAccount, $storageKey  
$client = New-Object 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient' "https://$storageAccount.blob.core.windows.net", $cred  
$container = $client.GetContainerReference($blobContainer)  
# list all the blobs  
$blobs = $container.ListBlobs($null,$true)
# filter blobs that are have Lease Status as "locked"
$lockedBlobs = @()  
foreach($blob in $blobs)  
    $blobProperties = $blob.Properties
    if($blobProperties.LeaseStatus -eq "Locked")  
        $lockedBlobs += $blob  

if($lockedBlobs.Count -gt 0)  
    Write-Host "Breaking leases..."
    foreach($blob in $lockedBlobs )
            $blob.AcquireLease($null, $restoreLeaseId, $null, $null, $null)  
            Write-Host "The lease on $($blob.Uri) is a restore lease."  
        catch [Microsoft.WindowsAzure.Storage.StorageException]  
            if($_.Exception.RequestInformation.HttpStatusCode -eq 409)  
                Write-Host "The lease on $($blob.Uri) is not a restore lease."  
        Write-Host "Breaking lease on $($blob.Uri)."  
        $blob.BreakLease($(New-TimeSpan), $null, $null, $null) | Out-Null  
} else { Write-Host " There are no blobs with locked lease status." }

另请参阅See also

从 SQL Server 备份到 URL 的最佳做法和故障排除SQL Server Backup to URL Best Practices and Troubleshooting