随笔 - 229  文章 - 2  评论 - 511  阅读 - 84万

partial backup/restore

 http://www.sqlmag.com/blogs/sql-server-questions-answered/sql-server-questions-answered/tabid/1977/entryid/12771/Using-Partial-Database-Availability-for-Targeted-Restores.aspx

Listing 1: Code to Create a Database with Multiple File Groups

复制代码
CREATE DATABASE ExampleDB;
GO 

ALTER DATABASE ExampleDB ADD FILEGROUP TestFileGroup1;
ALTER DATABASE ExampleDB ADD FILEGROUP TestFileGroup2;
GO 

ALTER DATABASE ExampleDB ADD FILE (
      NAME 
= TestFile1,
      FILENAME 
= 'C:\SQLskills\TestFile1.ndf',
      SIZE 
= 5MB)
TO FILEGROUP TestFileGroup1;
ALTER DATABASE ExampleDB ADD FILE (
      NAME 
= TestFile2,
      FILENAME 
= 'C:\SQLskills\TestFile2.ndf',
      SIZE 
= 5MB)
TO FILEGROUP TestFileGroup2;
GO 

CREATE TABLE ExampleDB..t1 (c1 INTON TestFileGroup1;
CREATE TABLE ExampleDB..t2 (c1 INTON TestFileGroup2;
GO

INSERT INTO ExampleDB..t1 VALUES (1);
INSERT INTO ExampleDB..t2 VALUES (2);
GO 

BACKUP DATABASE ExampleDB TO DISK = 'c:\SQLskills\ExampleDB.bck';
--BACKUP DATABASE Sales FILEGROUP = 'primary'  TO DISK = 'C:\SQLskills\ExampleDBPrimaryFG.bck'-- you can only backup the primary filegroup which contains all the definiton of objects,users...
GO
复制代码

 

Now on a different system you can restore just the primary file group and the first file group. The key is to restore the primary file group first using WITH PARTIAL to let SQL Server know you aren’t restoring the entire database, as shown in Listing 2.
Listing 2: Restoring the Primary File Group Using WITH PARTIAL

 

复制代码
RESTORE DATABASE ExampleDB
    FILEGROUP 
= 'primary'
FROM DISK = 'c:\SQLskills\ExampleDB.bck'
WITH PARTIAL, NORECOVERY;
GO

RESTORE DATABASE ExampleDB
    FILEGROUP 
= 'TestFileGroup1'
FROM DISK = 'c:\SQLskills\ExampleDB.bck'
WITH NORECOVERY;
GO
复制代码

 

Next, you can restore any differential and/or transaction log backups to get the restored file groups to the desired point in time, and then bring the database online using the following code:
RESTORE DATABASE ExampleDB WITH RECOVERY;
If I query the database using the code in Listing 3, it will show me what’s online.

Listing 3: Code Used to Determine Which File Groups Are Online

SELECT [name], [state_desc] FROM ExampleDB.sys.database_files;
GO
name             state_desc
---------------- -----------------
ExampleDB        ONLINE
ExampleDB_log    ONLINE
TestFile1        ONLINE
TestFile2        RECOVERY_PENDING

Now, of course, I can only use the portions of the database that I’ve restored. If I try to access anything in an offline file group, I’ll get an error similar to the following:

SELECT * FROM ExampleDB..t2;
GO
Msg 8653, Level 16, State 1, Line 1
The query processor is unable to produce a plan for the table or view 't2' because the table resides in a filegroup which is not online.

 

 http://www.sqlmag.com/blogs/sql-server-questions-answered/sql-server-questions-answered/tabid/1977/entryid/12771/Using-Partial-Database-Availability-for-Targeted-Restores.aspx

 

posted on   stswordman  阅读(309)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2010年12月 >
28 29 30 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示