脚本如何获取上次备份以从 AZURE 还原

Posted

技术标签:

【中文标题】脚本如何获取上次备份以从 AZURE 还原【英文标题】:script how to get last backup to restore from AZURE 【发布时间】:2015-10-13 09:59:39 【问题描述】:

我想从 mu AZURE 服务器恢复上次备份。我有一个脚本,但我只是实现了使用当前日期进行备份,而不是最后一个。你知道如何获得它吗?

`使用[主] 走 DECLARE @name VARCHAR(50) -- 数据库名称 DECLARE @URL VARCHAR(256) -- 备份 URL DECLARE @fileDate VARCHAR(20) -- 用于文件日期

-- 指定最后一次备份的日期格式。 SELECT @fileDate = REPLACE (CONVERT (VARCHAR (10), GETDATE (), 120), '-','_')

--创建凭据以连接到 windows azure 存储服务 如果不存在(SELECT * FROM sys.credentials WHERE credential_identity = 'aaaaaaaaaa') 开始 创建凭据 Pyramidsqlcredential WITH IDENTITY = 'bbbbbbbbbb' ,秘密 = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 结尾 去吧

--能够恢复特定数据库的代码 声明 db_cursor CURSOR FOR 选择名称 FROM master.dbo.sysdatabases WHERE name IN ('STG','DWH','AUXDB','TESTPORTAL') -- 只恢复这些数据库

打开 db_cursor FETCH NEXT FROM db_cursor INTO @name 而@@FETCH_STATUS = 0 开始

--PRE数据库的创建 IF OBJECT_ID(N'PRE_@name', N'U') 不为空 删除数据库@name; 走 创建数据库 [@name] 遏制 = 无 初级 ( NAME = N'PRE_@name', FILENAME = N'G:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Data\PRE_@name.mdf' , SIZE = 609344KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ), FILEGROUP [INMEMORY] 包含 MEMORY_OPTIMIZED_DATA 默认值 登录 ( NAME = N'PRE_@name', FILENAME = N'H:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Data\PRE_@name_log.ldf' , SIZE = 833024KB , MAXSIZE = 2048GB , FILEGROWTH = 10 %)

设置@URL = 'https://xxxxxxxxxx.blob.core.windows.net/'

--将数据库恢复到 Windows azure 存储服务 - 使用 URL 的 blob 恢复数据库 PRE_AUXDB FROM URL = @URL+@name+'/'+@name+''+@fileDate+'*.bak' --FROM URL = 'https://xxxxxxxxxxxxxxxxxxxxxxx.blob.core.windows.net/AUXDB/AUXDB'+REPLACE (CONVERT (VARCHAR(10), GETDATE(), 120), '-','_')+'*.bak' 有凭证='aaaaaaaaaaaaaaa' ,将“PRE_AUXDB_Data”移动到“G:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Data\PRE_@name_Data.mdf” ,统计 = 10

结束

关闭 db_cursor DEALLOCATE db_cursor`

【问题讨论】:

【参考方案1】:

我想我找到了解决办法。该脚本尚未测试。也许它对某人有帮助。

                    USE [master]
                    GO
                    DECLARE @name VARCHAR(50) -- database name  
                    DECLARE @URL VARCHAR(256) -- URL for backup  

                    --Create a credential to connect to the windows azure storage service
                    IF NOT EXISTS (SELECT * FROM sys.credentials WHERE credential_identity = 'credentialname')
                    BEGIN
                    CREATE CREDENTIAL credentialname WITH IDENTITY = 'introduceyouridentity'
                    ,SECRET = 'accountkey'
                    END
                    GO

                    --Code to be able to restore specific databases
                    DECLARE @db_cursor CURSOR 
                        SET @db_cursor = CURSOR FOR
                    SELECT name 
                    FROM master.dbo.sysdatabases 
                    WHERE name IN ('STG','DWH','AUXDB','TESTPORTAL')  -- only restore these databases

                    OPEN @db_cursor   
                    FETCH NEXT FROM @db_cursor INTO @name  

                    WHILE @@FETCH_STATUS = 0   
                    BEGIN 
                    declare @backupfile as Varchar(255) --latest bck in AZURE
                    declare @DBname VARCHAR(50) = 'PRE_'+@name  --name new database ex: PRE_AUXDB

                    --Check latest backup file in Azure per type
                    /* type char(1)
                    Backup type. Can be:
                    D = Database
                    I = Differential database
                    L = Log
                    F = File or filegroup
                    G =Differential file
                    P = Partial
                    Q = Differential partial
                    Can be NULL. */
                    SELECT TOP 1 @backupfile=mf.physical_device_name from msdb..backupset bk      
                    join msdb..backupmediafamily mf on bk.media_set_id = mf.media_set_id   
                    where database_name=@name and bk.type='D' order by  
                    backup_set_id desc
                    --Creation of PRE databases
                    IF  OBJECT_ID(@DBname, N'U') IS NOT NULL 
                    DROP DATABASE @DBname;
                    GO
                    CREATE DATABASE [@DBname]
                     CONTAINMENT = NONE
                     ON  PRIMARY 
                    ( NAME = @DBname, FILENAME = N'\\servername\G$\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Data\'+@DBname+'.mdf' , SIZE = 609344KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ), 
                        FILEGROUP [INMEMORY] CONTAINS MEMORY_OPTIMIZED_DATA  DEFAULT
                     LOG ON 
                    ( NAME = @DBname, FILENAME = N'\\servername\H$\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Data'+@DBname_log+'.ldf' , SIZE = 833024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
                    GO
                    set @URL = 'https://azureservername.blob.core.windows.net/'

                            /*NOT USE
                            --Backup the database to the windows azure storage service - blob using URL
                            BACKUP DATABASES 
                            TO URL = @backupfile
                            WITH CREDENTIAL = 'credentialname' 
                            ,COMPRESSION --Compress the backup
                            ,STATS = 10 --This reports the percentage complete as of the threshold for reporting the next interval
                            GO
                            */

                    --RESTORE the database to the windows azure storage service - blob using URL
                    RESTORE DATABASE PRE_AUXDB 
                    FROM URL = @backupfile
                    WITH CREDENTIAL = 'credentialname'
                    ,MOVE @DBname+'_Data' to '\\servername\G$\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Data\'+@DBname+'_Data.mdf'
                    ,STATS = 10

                    END   

                    CLOSE db_cursor   
                    DEALLOCATE db_cursor

【讨论】:

以上是关于脚本如何获取上次备份以从 AZURE 还原的主要内容,如果未能解决你的问题,请参考以下文章

我应该如何获取 Azure AD B2C 用户的上次登录时间戳?

如何在 Azure Blob 存储中备份和还原 Azure SQL 表,反之亦然

Windows Azure Active Directory备份/还原[暂停]

在本地使用 PowerShell 获取分支上的 Azure DevOps 上次构建 ID

如何获取 PHP 脚本以从已运行的进程发送和接收数据?

Azure 支持SQL Server 2019备份和文件还原