資料庫還原的操作,分兩步進行:第一步,驗證(verify)備份文件;第二步,根據備份策略還原資料庫; 參考《backup1:開始資料庫備份》,備份策略是: 一周一次完整備份,一天一次差異備份,一小時一次事務日誌備份 數據/日誌的每次備份都使用一個單獨的備份文件,數據備份的擴展名是 .bak,日誌備份 ...
資料庫還原的操作,分兩步進行:第一步,驗證(verify)備份文件;第二步,根據備份策略還原資料庫;
參考《backup1:開始資料庫備份》,備份策略是:
- 一周一次完整備份,一天一次差異備份,一小時一次事務日誌備份
- 數據/日誌的每次備份都使用一個單獨的備份文件,數據備份的擴展名是 .bak,日誌備份的擴展名是.trn
一,驗證(Verifiy)備份文件
1,查看備份文件的文件列表(Data File 和 Log File)
由於,數據或日誌的每次備份,都使用一個單獨的備份文件,因此,在備份文件中,只有一個backup set,File選項是1,如果不指定該File選項,預設值是1。
RESTORE FILELISTONLY FROM disk = 'D:\TestDBBackupFolder\Sitedb_bak4.bak' --with file=1;
在SQL Server中,一個備份文件可以存儲多個backup set,每一個backup set都是數據或日誌的一次備份(完整或差異備份),這意味著,一個備份文件能夠存儲多個資料庫備份。為了便於管理備份文件,建議,每一個備份都存儲到單獨的備份文件中,這樣,每個備份文件只存儲一次備份。
返回的結果集中,有三個非常重要的欄位:
- LogicalName:文件的邏輯名稱
- PhysicalName:文件的物理名稱,是文件在OS上的路徑+文件名,例如,D:\Program Files\Microsoft SQL Server\MSSQL\Data\SitedB.mdf;
- Type:文件的類型(L:Log File,D:Data File,F:Full Text Catalog);
選項:FILE = backup_set_file_number,標識被還原的backup set。
For example, a backup_set_file_number of 1 indicates the first backup set on the backup medium and a backup_set_file_number of 2 indicates the second backup set. When not specified, the default is 1, except for RESTORE HEADERONLY in which case all backup sets in the media set are processed.
2,驗證(Verify)備份文件
使用Restore VerifyOnly 命令來驗證備份文件的有效性,如果備份是有效的,SQL Serer返回驗證成功的消息。
RESTORE VERIFYONLY FROM DISK = 'physical_backup_device_name' [ WITH { MOVE 'logical_file_name_in_backup' TO 'operating_system_file_name' [ ,...n ] | FILE = backup_set_file_number }] [;]
如果驗證通過,SQL Server會列印備份有效的消息:
The backup set on file 1 is valid.
選項 Move-To:用於驗證磁碟是否有足夠的Free Space來存儲還原的資料庫文件(Data Files 和 Log Files);
Move子句指定文件的LogicalName,To子句指定文件的PhysicalName,即,存儲該文件的Path+FileName,例如:
restore verifyonly from disk = 'D:\TestDBBackupFolder\Sitedb_bak4.bak' with file=1 ,move 'Site_TestDB_1' to 'D:\TestDBBackupFolder\Site_TestDB_1.mdf' ,move 'Site_TestDB_2' to 'D:\TestDBBackupFolder\Site_TestDB_2.ndf'; ,move 'site_TestDB_log' to 'D:\TestDBBackupFloder\Site_TestDB_log.ldf'
預設情況下,在還原時,數據備份和日誌備份將還原到原始的位置(Original Location),如果計劃將資料庫複製到其他Server上,使用Move-To選項是非常必要的,在執行還原操作前,使用Restore VerifyOnly命令,檢查是有有足夠的Disk Space,是否有潛在的文件命名衝突。
If a RESTORE VERIFYONLY statement is used when you plan to relocate a database on the same server or copy it to a different server, the MOVE option might be necessary to verify that sufficient space is available in the target and to identify potential collisions with existing files.
二,還原數據文件
使用restore dabase 命令將存儲在備份文件中的 backup 還原成一個資料庫,根據備份的不同,將資料庫的還原操作分為兩部分:還原數據文件和還原日誌文件。
還原數據文件的命令,簡化
--To Restore an Entire Database from a Full database backup (a Complete Restore): RESTORE DATABASE database_name FROM DISK 'physical_backup_device_name' [ WITH {[ RECOVERY | NORECOVERY ] | , <general_WITH_options> [ ,...n ]} ][;] <general_WITH_options> ::= MOVE 'logical_file_name_in_backup' TO 'operating_system_file_name' [ ,...n ] | REPLACE | RESTART | RESTRICTED_USER | FILE = { backup_set_file_number | @backup_set_file_number } | STATS [ = percentage ]
1,還原選項(RECOVERY | NORECOVERY ),預設值是RECOVERY
RECOVERY 選項:指定還原操作將所有未提交的事務回滾,並使資料庫可用;如果後續需要從差異備份和事務日誌備份繼續還原操作,那麼必須使用 NORECOVERY選項;RECOVERY 選項用於還原操作的最後一個Restore命令中。
RECOVERY option instructs the restore operation to roll back any uncommitted transactions. After the recovery process, the database is ready for use. If subsequent RESTORE operations (RESTORE LOG, or RESTORE DATABASE from differential) are planned, NORECOVERY should be specified instead.
NORECOVERY 選項:指定還原操作不回滾未提交的事務,後續需要從差異備份或事務日誌備份繼續還原操作,在還原過程的最後一個Restore命令之前,使用NoRecovery選項。
For restoring a database backup and one or more transaction logs or whenever multiple RESTORE statements are necessary (for example, when restoring a full database backup followed by a differential database backup), RESTORE requires the WITH NORECOVERY option on all but the final RESTORE statement. A best practice is to use WITH NORECOVERY on ALL statements in a multi-step restore sequence until the desired recovery point is reached, and then to use a separate RESTORE WITH RECOVERY statement for recovery only.
2,移動選項(Move),僅用於還原資料庫完整備份
MOVE 'logical_file_name_in_backup' TO 'operating_system_file_name' [ ...n ]
在還原過程中,將數據或日誌文件移動到新的位置上,預設情況下,資料庫的每個文件,都會還原到原始的位置上(Original Location);如果需要改變資料庫文件存儲的路徑,通過move-to 選項,為資料庫的每個文件指定新的Location。
Specifies that the data or log file whose logical name is specified by logical_file_name_in_backup should be moved by restoring it to the location specified by operating_system_file_name. The logical file name of a data or log file in a backup set matches its logical name in the database when the backup set was created.
Specify a MOVE statement for every logical file you want to restore from the backup set to a new location. By default, the logical_file_name_in_backup file is restored to its original location.
3,替換選項(Replace),建議不要使用Replace選項,僅用於還原資料庫完整備份
在SQL Server實例中,如果要還原的數據和現存的資料庫同名,那麼,指定Replace選項,SQL Server將會把已存在的同名資料庫刪除。如果沒有指定Replace選項,SQL Server會做安全檢查,不會將現存的同名資料庫刪除。
When the REPLACE option is not specified, a safety check occurs. This prevents overwriting a different database by accident. The safety check ensures that the RESTORE DATABASE statement does not restore the database to the current server if the following conditions both exist:
-
The database named in the RESTORE statement already exists on the current server, and
-
The database name is different from the database name recorded in the backup set.
4,重啟選項(Restart)
在資料庫還原操作中斷(interrupt)時,重啟還原操作,從中斷處重新開始還原操作。
5,限制用戶(RESTRICTED_USER),僅用於還原資料庫完整備份
對新還原的資料庫,限制(restrict)用戶訪問,只允許角色 db_owner, dbcreator 或 sysadmin 的成員的訪問;
三,還原資料庫的日誌文件
要執行事務日誌的備份,資料庫的恢復模式(Recovery Mode)必須是FULL,並且資料庫必須進行過一次完整備份;否則,事務日誌文件處於自動截斷(Auto-Truncate)狀態,無法執行事務日誌的備份。
--To Restore a Transaction Log: RESTORE LOG database_name [ <file_or_filegroup_or_pages> [ ,...n ] ] [ FROM <backup_device> [ ,...n ] ] [ WITH { [ RECOVERY | NORECOVERY ] | , <general_WITH_options> [ ,...n ] | , <point_in_time_WITH_options—RESTORE_LOG> } [ ,...n ] ] [;] <point_in_time_WITH_options—RESTORE_LOG>::= | { STOPAT = { 'datetime'| @datetime_var } | STOPATMARK = { 'mark_name' | 'lsn:lsn_number' } [ AFTER 'datetime'] | STOPBEFOREMARK = { 'mark_name' | 'lsn:lsn_number' } [ AFTER 'datetime'] }View Code
在還原事務日誌時,SQL Server支持還原到時間點,在Restore Log命令中指定StopAt選項,能夠將事務日誌還原到具體的時間點。
四,還原數據文件示例
1,依次還原資料庫的完整備份,差異備份和事務日誌備份
--完整備份還原 RESTORE DATABASE AdventureWorks2012 FROM DISK = 'Z:\SQLServerBackups\AdventureWorks2012.bak' WITH FILE = 1, STATS=5, MOVE 'AdventureWorks2012 TO' 'D:\SQLServer\AdventureWorks2012.mdf', MOVE 'AdventureWorks2012_Log' TO 'D:\SQLServer\AdventureWorks2012_log.ldf', NORECOVERY; --差異備份還原 RESTORE DATABASE AdventureWorks2012 FROM DISK = 'Z:\SQLServerBackups\AdventureWorks2012_2.bak' WITH FILE = 1, STATS=5, NORECOVERY; --日誌備份還原 RESTORE log AdventureWorks2012 FROM DISK = 'Z:\SQLServerBackups\AdventureWorks2012_3.trn' WITH FILE = 1, STATS=5, RECOVERY;
2,將事務日誌還原到某一個時間點
--日誌備份還原到某一個時間點 RESTORE log AdventureWorks2012 FROM DISK = 'Z:\SQLServerBackups\AdventureWorks2012_3.trn' WITH FILE = 1, STATS=5, STOPAT='Apr 15, 2016 12:00 AM' RECOVERY;
參考文檔:
RESTORE VERIFYONLY (Transact-SQL)
RESTORE FILELISTONLY (Transact-SQL)