SQL Server 数据库还原操作
/*以下为参考了相关资料汇总并简化,简简单单通过几个语句实现数据还原,仅作为参考。*/
--查询恢复模式
SELECT recovery_model,recovery_model_desc FROM sys.databases WHERE name ='test'
--查询备份记录
SELECT database_name,type,name,backup_finish_date FROM msdb.dbo.backupset where database_name='test'
--把spid杀掉
SELECT * FROM sys.sysprocesses WHERE dbid=DB_ID('test')
kill 60
--备份数据库FULL
Backup Database test To disk=N'c:\temp\test_20200909.bak' with NAME = N'数据库-TEST-20200909-全备份'
go
--数据库还原操作步骤,在SQL Server 2019 测试可行。
--1.备份日志
USE Master
GO
BACKUP LOG [test] TO DISK = N'c:\temp\test_log_20200909.bak'
WITH NO_TRUNCATE , NOFORMAT, NOINIT, NAME = N'数据库-TEST-20200909-事务日志备份', SKIP, NOREWIND, NOUNLOAD, NORECOVERY , COMPRESSION, STATS = 10, CHECKSUM
GO
--2.验证备份
declare @backupSetId as int
select @backupSetId = position from msdb..backupset
where database_name=N'test' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'test' )
if @backupSetId is null
begin
raiserror(N'验证失败。找不到数据库“test”的备份信息。', 16, 1)
end
RESTORE VERIFYONLY FROM DISK = N'c:\temp\test_20200909.bak' WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
GO
--3.从完整备份还原数据
RESTORE DATABASE [test] FROM DISK='c:\temp\test_20200909.bak' WITH NORECOVERY, REPLACE
go
--4.从事务日志还原数据,还原到某个时间点之前
RESTORE LOG [test] FROM DISK='c:\temp\test_log_20200909.bak' WITH STOPAT='2020-09-09 09:00',RECOVERY
go
--还原数据库,如果数据库提示正在还原中,则执行此命令即可。
RESTORE DATABASE test WITH RECOVERY
go
--三人行,必有吾师 --这是底线,感谢查阅。
- 点赞
- 收藏
- 关注作者
评论(0)