一、對正在使用的資料庫,執行還原處理
把當前正在使用的資料庫檔案和日誌檔案移動到 另個路徑下, 然後再用完整備份檔案進行還原
restore database test from disk=n'f:\dbbackup\test\20160511155000_full.bak' with norecovery, replace,
move 'test' to 'f:\dbbackup\新建資料夾\test.mdf',
move 'test_log' to 'f:\dbbackup\新建資料夾\test_log.ldf'
gorestore database test from disk= n'f:\dbbackup\test\20160511155000_full.bak'
go仔細觀察上述完整還原和差異還原的tsql語句,我們可以看到在完整還原階段,使用了with norecovery,而差異還原,則是使用了recovery。
2、完整備份、差異備份一起還原
restore database test from disk=n'f:\dbbackup\test\20160511155000_full.bak' with norecovery, replace,
move 'test' to 'f:\dbbackup\新建資料夾\test.mdf',
move 'test_log' to 'f:\dbbackup\新建資料夾\test_log.ldf'
gorestore database test from disk= n'f:\dbbackup\test\20160511155000_full.bak' with norecovery
gorestore database test from disk= n'f:\dbbackup\test\20160511155600_diff.bak' with recovery (最後一次差異備份)
場景:完整備份15:50進行;15:52差異:20160511155200_diff.bak;15:54差異:
20160511155400_diff.bak;
15:56差異:
20160511155600_diff.bak;15:58差異:
20160511155800_diff.bak;
那麼,假設希望還原到15:56之前的資料,按上面例子即可。
注:差異備份是相對上次完整備份來進行備份的,如:20160511155600_diff.bak 備份了15:50至15:56 期間的資料變化。
3、還原到某個時間點(可精確到秒)
restore database test from disk=n'f:\dbbackup\test\20160511155000_full.bak' with norecovery, replace,
move 'test' to 'f:\dbbackup\新建資料夾\test.mdf',
move 'test_log' to 'f:\dbbackup\新建資料夾\test_log.ldf'
gorestore database test from disk= n'f:\dbbackup\test\20160511155000_full.bak' with norecovery
gorestore database test from disk= n'f:\dbbackup\test\20160511155600_diff.bak' withnorecoverygo
--使用日誌恢復資料庫到10月15日15:59分:
restore log test disk=n'f:\dbbackup\test\20160511155700_log.bak' with recovery, stopat='2016-05-11 15:56:40'
--精確到秒鐘還原點
二、備份
1、差異:backup database @dbname to disk=@filename with differential --差異備份資料庫
2、完整:backup database @dbname to disk=@filename with init
3、日誌:backup log @dbname to disk = @strbackup with init;
1).差異備份是以上乙個全備為基點,這個期間所有差異資料的備份。
2).日誌備份是基於前乙個全備+日誌備份為基點,這個期間的事務日誌的備份。
3).在利用全備+日誌備份時,需要有序並逐個還原所有日誌備份。假設要還原週六的資料,則需要上週日的全備和周一到週六的所有日誌備份才可以。如果有每天的差異備份,則只需要週日的全備+周五的差異備份+週六的日誌備份即可。
declare @filename nvarchar(255)= 'd:\'
declare @dbname nvarchar(30) = 'ciyt'
declare @datestr nvarchar(20) = replace(replace(replace(convert(varchar, getdate(), 120 ),'-',''),' ','_'),':','')
set @filename = @filename + @dbname + '_' + @datestr +'.bak'
backup database @dbname to disk=@filename with init
SQL SERVER 2000 資料庫備份與還原
備份資料庫,例如 backup database northwind to disk c northwind.bak 還原資料庫,例如 返回由備份集內包含的資料庫和日誌檔案列表組成的結果集 restore filelistonly from disk c northwind.bak 還原由backu...
SQLServer收縮資料庫
以下語句用於設定資料庫定時自動收縮資料庫 use master gosp dboption testdb,autoshrink true gouse testdb gocheckpoint go 清空日誌語句 dump transaction testdb with no log 截斷事務日誌 ba...
SQL Server資料庫檢修
使用資料庫的過程中,由於斷電或其他原因,有可能導致資料庫出現一些小錯誤,比如檢索某些表特別慢,查詢不到符合條件的資料等.出現這些情況的原因,往往是因為資料庫有些損壞,或索引不完整.在access中,有個修復資料庫的功能可以解決這個問題,在sql企業管理器,沒有這個功能,要用語句來完成,下面就介紹如何...