zhaojp0 2020-01-24
但是需要注意将SQL Server备份到Azure Storage需要确保数据库SQL Server 2012 SP1 CU2及以上版本。
说了这么多,下面我们就一起来看一下,如何将数据库备份到Azure Storage。
首先我们需要准备一个存储账户:
获取access key,本次示例中我们使用access key的方式访问存储账户:
使用如下T-SQL在SQL中创建账户:
IF NOT EXISTS(SELECT * FROM sys.credentials WHERE credential_identity = ‘ bkuptour‘) CREATE CREDENTIAL bkuptourl WITH IDENTITY = ‘sql12bak‘, SECRET = ‘5amfZJpKLcR2lAfEBZod18VYbxZOPUbtKH9RowDQ3Ixv5sGMnSj5Lo/UbPb/zCCRKh/kAxnOaOS9oJELTVVoTg==‘;
创建完成后我们可以在存储账户中创建一个container用来存储备份文件:
创建完成以后可以使用如下T-SQL将数据库备份到Storage Account中:
DECLARE @DB_name VARCHAR(50) -- database name DECLARE @BackupLoc VARCHAR(256) -- path for backup files DECLARE @BackupfileName VARCHAR(256) -- filename for backup DECLARE @fileDate VARCHAR(20) -- used for file name -- specify database backup container location SET @BackupLoc = ‘https://sql12bak.blob.core.chinacloudapi.cn/test/‘ set @fileDate= replace(replace(convert(nvarchar(50),getdate()),‘ ‘,‘_‘),‘:‘,‘_‘) DECLARE db_cursor CURSOR FOR SELECT name FROM master.sys.databases WHERE database_id <>2 and state=0 OPEN db_cursor FETCH NEXT FROM db_cursor INTO @DB_name WHILE @@FETCH_STATUS = 0 BEGIN SET @BackupfileName = @BackupLoc + @DB_name + ‘_‘ + @fileDate + ‘.BAK‘ BACKUP DATABASE @DB_name TO URL = @BackupfileName WITH CREDENTIAL = ‘bkuptourl‘,COMPRESSION print ‘BACKUP DATABASE ‘_name+ ‘ TO URL =‘‘‘+ @BackupfileName +‘‘‘ WITH CREDENTIAL = ‘‘bkuptourl‘‘,COMPRESSION ‘ FETCH NEXT FROM db_cursor INTO @DB_name END CLOSE db_cursor DEALLOCATE db_cursor
备份完成,如下图所示: