반응형
/**********************************************************************************************
-- Title : [2k5] 파일 그룹의 PARTIAL(조각, piecemeal) 복구

-- Reference : hanbitbook.co.kr
-- Key word : restore database filegroup with partial 비상 로그 백업 no_truncate piecemeal
**********************************************************************************************/
USE master;

DROP DATABASE testDB2;

--데이터베이스 생성
CREATE DATABASE [testDB2] ON  PRIMARY
( NAME = N'FG1-1'
, FILENAME = N'd:\FG1-1.mdf'
),
( NAME = N'FG1-2'
, FILENAME = N'd:\FG1-2.ndf'
),
 FILEGROUP [FG2]
( NAME = N'FG2-1'
, FILENAME = N'd:\FG2-1.ndf'
),
( NAME = N'FG2-2'
, FILENAME = N'd:\FG2-2.ndf'
),
FILEGROUP [FG3]
( NAME = N'FG3-1'
, FILENAME = N'd:\FG3-1.ndf'
),
( NAME = N'FG3-2'
, FILENAME = N'd:\FG3-2.ndf'
)
 LOG ON
( NAME = N'testDB2_log'
, FILENAME = N'd:\testDB2_log.ldf'
);

--파일그룹별로 테이블 생성
CREATE TABLE testDB2.dbo.tblP (num int);
CREATE TABLE testDB2.dbo.tbl2 (num int) ON FG2;
CREATE TABLE testDB2.dbo.tbl3 (num int) ON FG3;

--데이터 저장
INSERT INTO testDB2.dbo.tblP VALUES(10);
INSERT INTO testDB2.dbo.tbl2 VALUES(11);
INSERT INTO testDB2.dbo.tbl3 VALUES(12);

--전체 백업
BACKUP DATABASE testDB2 TO disk = 'd:\testDB1.bak', disk = 'd:\testDB2.bak', disk = 'd:\testDB3.bak'
WITH NAME = N'전체백업', INIT, FORMAT;

--데이터 입력/확인
INSERT INTO testDB2.dbo.tblP VALUES(60);
INSERT INTO testDB2.dbo.tbl2 VALUES(61);
INSERT INTO testDB2.dbo.tbl3 VALUES(62);

SELECT * FROM testDB2.dbo.tblP;
SELECT * FROM testDB2.dbo.tbl2;
SELECT * FROM testDB2.dbo.tbl3;

--에러 발생
alter database testDB2 set offline; /*이후 mdf, ndf 파일 삭제*/

alter database testDB2 set online; --에러발생

/*
-- 우선 파일그룹 복원
-- tblP > tbl3 > tbl2의 중요도로 Primary > FG3 > FG2 순으로 복원
*/

--비상 로그 백업
BACKUP LOG testDB2 TO DISK='d:\비상로그1.bak'
 WITH NO_TRUNCATE, INIT;

RESTORE HEADERONLY FROM DISK = 'd:\testDB1.bak';
RESTORE HEADERONLY FROM DISK = 'd:\testDB2.bak';
RESTORE HEADERONLY FROM DISK = 'd:\testDB3.bak';
RESTORE HEADERONLY FROM DISK = 'd:\비상로그1.bak';

--Primary 복원(기본 그룹 복원시에만 partial 옵션 사용)
RESTORE DATABASE testDB2 FILEGROUP='PRIMARY'
FROM disk = 'd:\testDB1.bak', disk = 'd:\testDB2.bak', disk = 'd:\testDB3.bak'
WITH PARTIAL, NORECOVERY;

--비상로그 복원
RESTORE LOG testDB2 FROM DISK='d:\비상로그1.bak';

--Primary 오픈 확인
select * from testDB2.dbo.tblP;

--Primary에 데이터 추가
insert into testDB2.dbo.tblP values(70);

--FG2, FG3 접근 확인
select * from tbl2;
select * from tbl3;

--FG3 복원위해 비상로그 백업
BACKUP LOG testDB2 TO DISK='d:\비상로그2.bak'
 WITH NO_TRUNCATE, INIT;

--FG3 복원
RESTORE DATABASE testDB2 FILEGROUP='FG3'
FROM disk = 'd:\testDB1.bak', disk = 'd:\testDB2.bak', disk = 'd:\testDB3.bak'
WITH NORECOVERY;

RESTORE LOG testDB2  FROM DISK='d:\비상로그2.bak';

--복구 확인
select * from testDB2.dbo.tbl3;
select * from testDB2.dbo.tblP;
select * from testDB2.dbo.tbl2; --요건 아직 복구된 파일 그룹이 아니라서 안됨.

--F3에 데이터 추가
insert into testDB2.dbo.tbl3 values(73);
 
--FG2 복원위해 비상로그 백업
BACKUP LOG testDB2 TO DISK='d:\비상로그3.bak'
 WITH NO_TRUNCATE, INIT;

--FG2 복원
RESTORE DATABASE testDB2 FILEGROUP='FG2'
FROM disk = 'd:\testDB1.bak', disk = 'd:\testDB2.bak', disk = 'd:\testDB3.bak'
WITH NORECOVERY;

RESTORE LOG testDB2  FROM DISK='d:\비상로그3.bak';

--확인
select * from testDB2.dbo.tbl2;
select * from testDB2.dbo.tblP;
select * from testDB2.dbo.tbl3; -- 좀전에 잘되던 FG3는 왜 안될까???? primary 그룹 외에는 partial
                                            -- 옵션 안써야 되서 그러넹...

반응형

+ Recent posts