无法使用 PowerShell 或 T-SQL 将 SQL 数据库备份到 Azure Blob 存储
Cannot Backup SQL Database to Azure Blob Storage with PowerShell or T-SQL
我相信我正在按照 Microsoft 的文档将 SQL 数据库备份到 Azure Blob 存储;但是,无论我尝试什么,我都会遇到同样的错误。
例如,以下代码创建一个 SQL 凭据并尝试备份数据库。
在 运行 之后,错误指出我不能使用 WITH CREDENTIAL 和 SAS,但 Microsoft 在他们的文档中演示了直接使用两者(https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/sql-server-backup-to-url?view=sql-server-2017#Examples )!
declare @databaseName varchar(50)
declare @destinationAzureStorageContainerPath varchar(256)
declare @destinationBlobPath varchar(256)
declare @timestampUtc as nvarchar(30)
select @timestampUtc = replace(convert(nvarchar(30), getutcdate(), 126), ':', '_');
set @databaseName = 'DWConfiguration'
set @destinationAzureStorageContainerPath = 'https://mystorageaccount.blob.core.windows.net/mystoragecontainer/'
SET @destinationBlobPath = @destinationAzureStorageContainerPath + @databaseName + '_' + @timestampUtc + '.BAK'
if not exists
(select * from sys.credentials
where name = 'https://mystorageaccount.blob.core.windows.net/mystoragecontainer/')
create CREDENTIAL [https://mystorageaccount.blob.core.windows.net/mystoragecontainer/]
with IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = 'sv... this is my token ...';
backup DATABASE @databaseName
to URL = @destinationBlobPath
with CREDENTIAL = 'https://mystorageaccount.blob.core.windows.net/mystoragecontainer/'
,COMPRESSION
,STATS = 5
错误:
Msg 3225, Level 16, State 1, Line 28
Use of WITH CREDENTIAL syntax is not valid for credentials containing a Shared Access Signature.
Msg 3013, Level 16, State 1, Line 28
BACKUP DATABASE is terminating abnormally.
作为替代方法,我决定使用 PowerShell。
Backup-SqlDatabase -ServerInstance "myserver" -Database "DWConfiguration" -BackupFile "https://mystorageaccount.blob.core.windows.net/mystoragecontainer/mydatabase_2019-01-04T20_01_03.127.bak" -SqlCredential "https://mystorageaccount.blob.core.windows.net/mystoragecontainer/"
如您所见,它会导致同样烦人的错误!
Backup-SqlDatabase : System.Data.SqlClient.SqlError: Use of WITH CREDENTIAL syntax is not valid for credentials
containing a Shared Access Signature.
在 blob 本身上,我设置了 "Private (no anonymous access)." 我只希望经过身份验证的请求能够访问 blob。这可能是问题所在吗?如果是这样,为什么 WITH CREDENTIAL
不解决这个问题?
如何简单地将 SQL 数据库的备份保存到我的 Azure 存储帐户?
我认为您的 sql 脚本有误。
您创建了凭据 "Using Shared Access Signature",但是在备份时您使用的 "To URL using storage account identity and access key" 与您之前创建的 sas 不匹配。
我在身边测试过,工作正常(创建凭据 "Using Shared Access Signature",并使用 "To URL using Shared Access Signature" 进行备份)。
IF NOT EXISTS (SELECT * FROM sys.credentials
WHERE name = 'https://xxx.blob.core.windows.net/container')
CREATE CREDENTIAL [https://xxx.blob.core.windows.net/container]
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = 'sv=xxx';
BACKUP DATABASE MyTest
TO URL = 'https://xxx.blob.core.windows.net/container/123.bak'
GO
测试结果如下:
我相信我正在按照 Microsoft 的文档将 SQL 数据库备份到 Azure Blob 存储;但是,无论我尝试什么,我都会遇到同样的错误。
例如,以下代码创建一个 SQL 凭据并尝试备份数据库。
在 运行 之后,错误指出我不能使用 WITH CREDENTIAL 和 SAS,但 Microsoft 在他们的文档中演示了直接使用两者(https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/sql-server-backup-to-url?view=sql-server-2017#Examples )!
declare @databaseName varchar(50)
declare @destinationAzureStorageContainerPath varchar(256)
declare @destinationBlobPath varchar(256)
declare @timestampUtc as nvarchar(30)
select @timestampUtc = replace(convert(nvarchar(30), getutcdate(), 126), ':', '_');
set @databaseName = 'DWConfiguration'
set @destinationAzureStorageContainerPath = 'https://mystorageaccount.blob.core.windows.net/mystoragecontainer/'
SET @destinationBlobPath = @destinationAzureStorageContainerPath + @databaseName + '_' + @timestampUtc + '.BAK'
if not exists
(select * from sys.credentials
where name = 'https://mystorageaccount.blob.core.windows.net/mystoragecontainer/')
create CREDENTIAL [https://mystorageaccount.blob.core.windows.net/mystoragecontainer/]
with IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = 'sv... this is my token ...';
backup DATABASE @databaseName
to URL = @destinationBlobPath
with CREDENTIAL = 'https://mystorageaccount.blob.core.windows.net/mystoragecontainer/'
,COMPRESSION
,STATS = 5
错误:
Msg 3225, Level 16, State 1, Line 28 Use of WITH CREDENTIAL syntax is not valid for credentials containing a Shared Access Signature. Msg 3013, Level 16, State 1, Line 28 BACKUP DATABASE is terminating abnormally.
作为替代方法,我决定使用 PowerShell。
Backup-SqlDatabase -ServerInstance "myserver" -Database "DWConfiguration" -BackupFile "https://mystorageaccount.blob.core.windows.net/mystoragecontainer/mydatabase_2019-01-04T20_01_03.127.bak" -SqlCredential "https://mystorageaccount.blob.core.windows.net/mystoragecontainer/"
如您所见,它会导致同样烦人的错误!
Backup-SqlDatabase : System.Data.SqlClient.SqlError: Use of WITH CREDENTIAL syntax is not valid for credentials containing a Shared Access Signature.
在 blob 本身上,我设置了 "Private (no anonymous access)." 我只希望经过身份验证的请求能够访问 blob。这可能是问题所在吗?如果是这样,为什么 WITH CREDENTIAL
不解决这个问题?
如何简单地将 SQL 数据库的备份保存到我的 Azure 存储帐户?
我认为您的 sql 脚本有误。
您创建了凭据 "Using Shared Access Signature",但是在备份时您使用的 "To URL using storage account identity and access key" 与您之前创建的 sas 不匹配。
我在身边测试过,工作正常(创建凭据 "Using Shared Access Signature",并使用 "To URL using Shared Access Signature" 进行备份)。
IF NOT EXISTS (SELECT * FROM sys.credentials
WHERE name = 'https://xxx.blob.core.windows.net/container')
CREATE CREDENTIAL [https://xxx.blob.core.windows.net/container]
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = 'sv=xxx';
BACKUP DATABASE MyTest
TO URL = 'https://xxx.blob.core.windows.net/container/123.bak'
GO
测试结果如下: