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  INT ON  TestFileGroup1;
CREATE   TABLE  ExampleDB..t2 (c1  INT ON  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

 
  

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:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值