SQLSERVER还原主文件组的方法
--备份主文件组和还原主文件组脚本2014-2-10
---------------------------------------------------------------------------------
--还原主分区
--分两种情况
--情况一:如果是在原来机器的原数据库上做主文件组备份,那么还需要备份事务日志(kill掉所有连接)
--然后还原主文件组备份,还原事务日志,不然只还原主文件组备份会显示(正在还原)
--这时候ClassifyResult表是可以访问的,表数据还在
ALTER DATABASE [Bare.Opinion.9095] SET RECOVERY FULL
GO
DECLARE @FileName VARCHAR(200) ,
@CurrentTime VARCHAR(50)
SET @CurrentTime = CONVERT(CHAR(8), GETDATE(), 112)
+ CAST(DATEPART(hh, GETDATE()) AS VARCHAR)
+ CAST(DATEPART(mi, GETDATE()) AS VARCHAR)
SET @FileName = 'E:\DBBackup\Bare.Opinion.9095\Bare.Opinion.9095_Primary_'
+ @CurrentTime + '.bak'
BACKUP DATABASE [Bare.Opinion.9095]
FILEGROUP='PRIMARY' TO DISK=@FileName WITH FORMAT
GO
DECLARE @FileName VARCHAR(200) ,
@CurrentTime VARCHAR(50)
SET @CurrentTime = CONVERT(CHAR(8), GETDATE(), 112)
+ CAST(DATEPART(hh, GETDATE()) AS VARCHAR)
+ CAST(DATEPART(mi, GETDATE()) AS VARCHAR)
SET @FileName = 'E:\DBBackup\Bare.Opinion.9095\Bare.Opinion.9095_Logtail_'
+ @CurrentTime + '.bak'
BACKUP LOG [Bare.Opinion.9095] TO DISK=@FileName WITH NORECOVERY
GO
RESTORE DATABASE [Bare.Opinion.9095]
FILEGROUP='PRIMARY'
FROM DISK=N'E:\DBBackup\Bare.Opinion.9095\Bare.Opinion.9095_Primary_20140210942.bak' WITH FILE = 1,
MOVE N'Barefoot.Opinion.12' TO N'E:\DataBase\Bare.Opinion.9095.mdf',
MOVE N'Barefoot.Opinion.12_log' TO N'E:\DataBase\Bare.Opinion.9095_1.ldf',
REPLACE, STATS = 10
GO
RESTORE LOG [Barefoot.Opinion.9095] FROM DISK=N'E:\DBBackup\Bare.Opinion.9095\Bare.Opinion.9095_Logtail_20140210945.bak' WITH RECOVERY ,REPLACE
GO
ALTER DATABASE [Bare.Opinion.9095] SET RECOVERY SIMPLE
GO
--情况二:如果是在新机器上做主文件组备份,那么不需要备份事务日志
--还原主文件组备份,由于在原机器原数据库的情况下分区文件组还存在并且在线,所以可以访问ClassifyResult表
--但是在新机器上,新机器是没有任何数据库文件的,所以还原了主文件组备份之后就可以使用了
--但是不能访问ClassifyResult表,显示
--消息 679,级别 16,状态 1,第 1 行
--表 'dbo.ClassifyResult' 的索引 'PK_ClassifyResult_T' 的分区之一(分区 ID 72057594050772992)所驻留的文件组("FG_ClassifyResult_ClassId_01")因为处于离线状态或正在还原或已经不存在而无法访问。这种情况可能会限制查询结果。
--这时候把ClassifyResult表改名为ClassifyResult_0,并且原来的分区方案和分区函数都不能删除
--一般情况是只会把Barefoot.Opinion.9095_Primary_20140210942.bak文件拿到新机器上并还原
--然后把ClassifyResult_0表编写表脚本,并导入新数据
ALTER DATABASE [Bare.Opinion.9095] SET RECOVERY FULL
GO
DECLARE @FileName VARCHAR(200) ,
@CurrentTime VARCHAR(50)
SET @CurrentTime = CONVERT(CHAR(8), GETDATE(), 112)
+ CAST(DATEPART(hh, GETDATE()) AS VARCHAR)
+ CAST(DATEPART(mi, GETDATE()) AS VARCHAR)
SET @FileName = 'E:\DBBackup\Bare.Opinion.9095\Bare.Opinion.9095_Primary_'
+ @CurrentTime + '.bak'
BACKUP DATABASE [Bare.Opinion.9095]
FILEGROUP='PRIMARY' TO DISK=@FileName WITH FORMAT
GO
--假定在同一台机器上做还原,还原之前需要先删除原数据库(删除了数据库之后原来分区文件夹E:\DataBase\Barefoot.Opinion.9095\下面就没有任何分区文件了FG_ClassifyResult_ClassId_01),然后直接还原就可以了,不用先新建一个数据库再还原
--模拟在一台新机器上做还原,新机器上是没有Barefoot.Opinion.9095这个数据库的
RESTORE DATABASE [Bare.Opinion.9095]
FILEGROUP='PRIMARY'
FROM DISK=N'E:\DBBackup\Bare.Opinion.9095\Bare.Opinion.9095_Primary_20140210942.bak' WITH FILE = 1,
MOVE N'Barefoot.Opinion.12' TO N'E:\DataBase\Bare.Opinion.9095.mdf',
MOVE N'Barefoot.Opinion.12_log' TO N'E:\DataBase\Bare.Opinion.9095_1.ldf',
REPLACE, STATS = 10
GO
ALTER DATABASE [Bare.Opinion.9095] SET RECOVERY SIMPLE
GO
----------------------------------------------------------------------------
--
--以下代码简单地演示了如何进行文件组的备份及还原(在还原时,模拟了丢失第二次文件组备份文件的情况)。
--创建测试数据库
CREATE DATABASE db ON PRIMARY(
NAME='db_data',
FILENAME= 'c:\db_data.mdf'), FILEGROUP db_fg1(
NAME = 'db_fg1_data',
FILENAME = 'c:\db_fg1_data.ndf'), FILEGROUP db_fg2(
NAME = 'db_fg2_data',
FILENAME = 'c:\db_fg2_data.ndf') LOG ON(
NAME='db_log',
FILENAME ='c:\db.ldf')
GO
--在文件组db_fg1上创建表,并单独创建该文件组的备份
CREATE TABLE db.dbo.tb ( id INT )
ON db_fg1
BACKUP DATABASE db FILEGROUP='db_fg1' TO DISK='c:\db_fg1.bak' WITH FORMAT
GO
在其他文件组上创建表
CREATE TABLE db.dbo.ta ( id INT )
ON [PRIMARY]
CREATE TABLE db.dbo.tc ( id INT )
ON db_fg2
INSERT db.dbo.tb
SELECT id
FROM sysobjects
--备份每个文件组,并且备份事务日志
BACKUP DATABASE db FILEGROUP='PRIMARY' TO DISK='c:\db_primary.bak' WITH FORMAT
BACKUP DATABASE db FILEGROUP='db_fg1' TO DISK='c:\db_fg1_new.bak' WITH FORMAT
BACKUP DATABASE db FILEGROUP='db_fg2' TO DISK='c:\db_fg2.bak' WITH FORMAT
BACKUP LOG db TO DISK='c:\db_log.bak' WITH FORMAT
GO
--删除数据库
DROP DATABASE db
GO
--从文件组备份中恢复数据
RESTORE DATABASE db FILEGROUP='PRIMARY' FROM DISK='c:\db_primary.bak' WITH NORECOVERY,REPLACE
RESTORE DATABASE db FILEGROUP='db_fg1' FROM DISK='c:\db_fg1.bak' WITH NORECOVERY
RESTORE DATABASE db FILEGROUP='db_fg2' FROM DISK='c:\db_fg2.bak' WITH NORECOVERY
RESTORE LOG db FROM DISK='c:\db_log.bak' WITH RECOVERY
SELECT COUNT(*)
FROM db.dbo.tb
GO
--删除测试数据库
DROP DATABASE db
--站长统计