您好,登錄后才能下訂單哦!
這篇文章主要介紹如何實現alwayson的備份還原腳本,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
1、 備份數據庫
在主副本上,將需要做AlwaysOn的數據庫做一次全備和日志備份(NOTE:禁用事務日志備份作業,如果有的話)
替換參數,執行如下腳本生成備份語句,然后執行:
DECLARE @DBName NVARCHAR(255)
DECLARE @SQL NVARCHAR(MAX)
DECLARE @BackupToPath NVARCHAR(500)
SET @DBName='datayesdb' --數據庫名稱
SET @BackupToPath='D:' --數據庫備份在主副本的存放路徑
SET NOCOUNT ON
PRINT '-- ============================================='
PRINT '-- AlwaysOn主副本上備份數據庫(完整備份+事務日志備份)'+CHAR(13)
SET @SQL='USE [master]
GO
ALTER DATABASE ['+@DBName+'] SET RECOVERY FULL;
GO
BACKUP DATABASE ['+@DBName+']
TO DISK='''+@BackupToPath+'\'+@DBName+'.bak'' WITH COMPRESSION
GO
BACKUP LOG ['+@DBName+']
TO DISK='''+@BackupToPath+'\'+@DBName+'.trn'' WITH COMPRESSION
GO'+CHAR(13)
PRINT @SQL
2、 還原數據庫
將備份文件復制到輔助副本服務器,使用NORECOVERY方式還原。
替換參數,執行如下腳本生成備份語句,然后執行:
DECLARE @DBName NVARCHAR(255)
DECLARE @SQL NVARCHAR(MAX)
DECLARE @RestoreFromPath NVARCHAR(MAX)
DECLARE @RestoreToDataFileFolder NVARCHAR(200)
DECLARE @RestoreToLogFileFolder NVARCHAR(200)
SET @DBName='datayesdb' --數據庫名稱
SET @RestoreFromPath='D:\share' --數據庫備份在輔助副本的存放路徑
SET @RestoreToDataFileFolder='D:\SQLData' --數據庫備份的數據文件在輔助副本的還原路徑
SET @RestoreToLogFileFolder='D:\SQLLog' --數據庫備份的日志文件在輔助副本的還原路徑
SET NOCOUNT ON
PRINT '-- ============================================='
PRINT '-- AlwayOn輔助副本還原數據庫(指定NORECOVERY方式還原)'+CHAR(13)
DECLARE @RestoreFilePath NVARCHAR(MAX)
DECLARE @LNAME NVARCHAR(500)
DECLARE @PNAME NVARCHAR(500)
DECLARE @PFName NVARCHAR(500)
DECLARE @BackupType CHAR(1)
SET @RestoreFilePath=''
SET @SQL = 'RESTORE FILELISTONLY FROM DISK = '''+@RestoreFromPath+'\'+@DBName+'.bak'+''''
if OBJECT_ID ('tempdb..#temp')is not null
BEGIN
DROP TABLE #BackupFileList
END
CREATE TABLE #BackupFileList
(
LogicalName NVARCHAR(128) ,
PhysicalName NVARCHAR(260) ,
BackupType CHAR(1) ,
FileGroupName NVARCHAR(128) ,
SIZE NUMERIC(20,0),
MaxSize NUMERIC(20,0) ,
FileID BIGINT ,
CreateLSN NUMERIC(25,0) ,
DropLSN NUMERIC(25,0) NULL ,
UniqueID UNIQUEIDENTIFIER ,
ReadOnlyLSN NUMERIC(25,0) NULL ,
ReadWriteLSN NUMERIC(25,0) NULL ,
BackupSizeInBytes BIGINT ,
SourceBlockSize INT ,
FileGroupID INT ,
LogGroupGUID UNIQUEIDENTIFIER NULL ,
DifferentialBaseLSN NUMERIC(25,0) NULL ,
DifferentialBaseGUID UNIQUEIDENTIFIER ,
IsReadOnly BIT ,
IsPresent BIT ,
TDEThumbprint NVARCHAR(100)
)
INSERT INTO #BackupFileList EXEC (@SQL);
DECLARE CurTBName CURSOR
FOR
SELECT LogicalName,PhysicalName,BackupType FROM #BackupFileList
OPEN CurTBName
FETCH NEXT FROM CurTBName INTO @LNAME,@PNAME,@BackupType
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @PFName=RIGHT(@PNAME, CHARINDEX('\',REVERSE(@PNAME))-1)
SET @RestoreFilePath=' MOVE N'''+@LNAME+''' TO N'''
+CASE WHEN @BackupType='D' THEN @RestoreToDataFileFolder ELSE @RestoreToLogFileFolder END
+'\'+@PFName+''', '+CHAR(13)+@RestoreFilePath
FETCH NEXT FROM CurTBName INTO @LNAME,@PNAME,@BackupType
END
CLOSE CurTBName
DEALLOCATE CurTBName
SET @SQL='USE [master]
GO
RESTORE DATABASE '+@DBName+' FROM DISK = N'''+@RestoreFromPath+'\'+@DBName+'.bak'' WITH FILE = 1,'+CHAR(13)
+@RestoreFilePath
+'NORECOVERY,NOUNLOAD,STATS = 10
GO
RESTORE LOG '+@DBName+' FROM DISK = N'''+@RestoreFromPath+'\'+@DBName+'.trn'' WITH NORECOVERY
GO'+CHAR(13)
PRINT @SQL
DROP TABLE #BackupFileList
以上是“如何實現alwayson的備份還原腳本”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。