반응형
  1. /**********************************************************************************************
    -- Title : [2k] 파일 & 파일그룹 백업 시연
    -- Reference : dbRang.com
    -- Key word : backup database, filegroup
    **********************************************************************************************/

    -- master dB 사용
    use master
    go

    -- 기존 ttt_dB가 있으면 지운다.
    -- 시스템 테이블 sysdatabases에서 찾는겨~
    if exists(select name from sysdatabases where name = 'ttt_dB')
     drop database ttt_dB
    go

    -- ttt_dB 생성과 기본 .mdf 및 .ldf 생성
    create database ttt_dB
    on
    ( name = 'ttt_Sys'
            -- Primary 그룹에 자동 설정(파일 그룹 지정이 없으면 Primary 그룹이 default)
    , filename = 'd:\ttt_Sys.MDF'
    , size = 1MB
    , filegrowth = 0%
    )
    log on
    ( name = 'ttt_Log'  -- Primary 그룹에 자동 설정
    , filename = 'd:\ttt_Log.LDF'
    , size = 1MB
    , filegrowth = 0%
    )
    go

    -- ttt_FG_1 파일 그룹 추가
    alter database ttt_dB
    add
     filegroup ttt_FG_1
    go

    -- ttt_FG_1 파일 그룹에 ttt_F_1 파일 추가
    alter database ttt_dB
    add file
    ( name = 'ttt_F_1'
    , filename = 'd:\ttt_F_1.ndf'
    , size = 2MB
    , filegrowth = 0%
    ) to filegroup ttt_FG_1
    go

    -- ttt_FG_2 파일 그룹 추가
    alter database ttt_dB
    add
     filegroup ttt_FG_2
    go

    -- ttt_FG_2 파일 그룹에 ttt_F_2 파일 추가
    alter database ttt_dB
    add file
    ( name = 'ttt_F_2'
    , filename = 'd:\ttt_F_2.ndf'
    , size = 2MB
    , filegrowth = 0%
    ) to filegroup ttt_FG_2
    go

    -- ttt_dB 사용
    use ttt_dB
    go

    -- Primary 그룹에 ttt_T_0 테이블 생성
    create table ttt_T_0
    (
     T_0_ID int not null
    )  /* FileGroup을 지정하지 않으면 default로 Primary에 생성됨. */
    go

    -- ttt_FG_1 파일그룹에 ttt_T_1 테이블 생성
    create table ttt_T_1
    (
     T_1_ID int not null primary key
    , T_1_CH char(5) not null
    , T_1_VC varchar(5) null
    ) on ttt_FG_1
    go

    -- ttt_FG_2 파일 그룹에 ttt_T_2 테이블 생성
    create table ttt_T_2
    (
     T_2_ID int not null primary key
    , T_2_CH char(5) not null
    , T_2_VC varchar(5) null
    ) on ttt_FG_2
    go

    -- Ctrl+D 를 눌러 결과창을 그리드로 볼수 있게하고 5번째 그리드로 확인한다.
    -- 아니면 상단 메뉴에서 [쿼리]-[표 형태로결과 표시]를 클릭 하시오.
    -- 아래 SP에서 지정된 파일 그룹을 확인 할 수 있다.
    -- 보기 힘드니깐 exec를 하나씩 실행하고 5번째 그리드를 통해서 파일 그룹 확인혀~
    exec sp_help ttt_T_0
    exec sp_help ttt_T_1
    exec sp_help ttt_T_2

    -- 데이터 입력
    insert ttt_T_0 values(0)
    insert ttt_T_0 values(1)
    insert ttt_T_1 values(1,'CH_aa','VC_aa')
    insert ttt_T_1 values(2,'CH_ab','VC_ab')
    insert ttt_T_2 values(1,'CH_ba','VC_ba')
    insert ttt_T_2 values(2,'CH_bb','VC_bb')
    go

    -- 확인
    select * from ttt_T_0
    select * from ttt_T_1
    select * from ttt_T_2
    go

    -- master 사용
    use master
    go

    -- 디바이스 추가.
    -- 재 작업시 이미 디바이스가 설정되어 있을 수 있지요...^^;; 그땐 지워...
    -- sp_dropdevice 'ttt_Device_F' -- 디바이스 삭제
    -- sp_dropdevice 'ttt_Device_P'
    -- sp_dropdevice 'ttt_Device_1'
    -- sp_dropdevice 'ttt_Device_2'
    -- sp_dropdevice 'ttt_Device_L'
    -- sp_dropdevice 'ttt_Device_L2'
    -- sp_dropdevice 'ttt_Device_L3'
    -- sp_dropdevice 'ttt_Device_L4'
    -- ttt_Full backup
    exec sp_addumpdevice 'disk', 'ttt_Device_F', 'd:\ttt_FG_F_Bak.dat'  
    -- ttt_FG_Primary backup
    exec sp_addumpdevice 'disk', 'ttt_Device_P', 'd:\ttt_FG_P_Bak.dat'  
    -- ttt_FG_1 backup
    exec sp_addumpdevice 'disk', 'ttt_Device_1', 'd:\ttt_FG_1_Bak.dat'  
    -- ttt_FG_2 backup
    exec sp_addumpdevice 'disk', 'ttt_Device_2', 'd:\ttt_FG_2_Bak.dat'  
    -- ttt_Log1 backup //Primary 변경 로그 백업
    exec sp_addumpdevice 'disk', 'ttt_Device_L', 'd:\ttt_FG_L_Bak.dat'  
    -- ttt_Log2 backup //FG1 변경 로그 백업
    exec sp_addumpdevice 'disk', 'ttt_Device_L2', 'd:\ttt_FG_L2_Bak.dat'
    -- ttt_Log3 backup //FG2 변경 로그 백업
    exec sp_addumpdevice 'disk', 'ttt_Device_L3', 'd:\ttt_FG_L3_Bak.dat'
    -- ttt_Log4 backup //복원 전 선행 로그 백업
    exec sp_addumpdevice 'disk', 'ttt_Device_L4', 'd:\ttt_FG_L4_Bak.dat'

    -- 아래부터 나오는 ##번호는 첨부된 엑셀파일에서 데이터 입력 현황을 볼 수 있는 태그요.
    -- ##1. 일단 전체 백업
    backup database ttt_dB
     to ttt_Device_F
    go

    -- ttt_T_0 테이블에 간단한 데이터 입력
    insert ttt_dB.dbo.ttt_T_0 values(100)
    go

    -- ##2. Primary 그룹 백업
    backup database ttt_dB
     filegroup = 'PRIMARY'
     to ttt_Device_P
     with init  -- init option? 찾아서 공부해~ 캭~!
    go

    -- ttt_T_0 테이블에 간단한 데이터 입력
    insert ttt_dB.dbo.ttt_T_0 values(200)
    go

    -- ##3. ttt_T_0 테이블의 Log 백업
    backup log ttt_dB
     to ttt_Device_L
     with init
    go

    -- ttt_T_1에 간단한 데이터 입력
    insert ttt_dB.dbo.ttt_T_1 values(100,'CH100','VC100')
    go

    -- ##4. ttt_FG_1 그룹 백업
    backup database ttt_dB
     filegroup = 'ttt_FG_1'
     to ttt_Device_1
     with init
    go

    -- ttt_T_1 테이블에 간단한 데이터 입력
    insert ttt_dB.dbo.ttt_T_1 values(200,'CH200','VC200')
    go

    -- ##5. ttt_FG_1 로그 백업
    backup log ttt_dB
     to ttt_Device_L2
     with init
    go

    -- ttt_T_2에 간단한 데이터 입력
    insert ttt_dB.dbo.ttt_T_2 values(300,'CH300','VC300')
    go

    -- ##6. ttt_FG_2 그룹 백업
    backup database ttt_dB
     filegroup = 'ttt_FG_2'
     to ttt_Device_2
     with init
    go

    -- ttt_T_2에 간단한 데이터 입력
    insert ttt_dB.dbo.ttt_T_2 values(400,'CH400','VC400')
    go

    -- ##7. ttt_FG_2 로그 백업
    backup log ttt_dB
     to ttt_Device_L3
     with init
    go

    -- ##8. ttt_FG_2의 ttt_T_2 테이블에 데이터 입력
    -- 이 건은 로그 백업을 하지 않지만 차후에 이건까지 복구를 한다.
    insert ttt_dB.dbo.ttt_T_2 values(900,'CH900','VC900')
    go

    -- 현재까지의 전체 테이블 확인
    select * from ttt_dB.dbo.ttt_T_0
    select * from ttt_dB.dbo.ttt_T_1
    select * from ttt_dB.dbo.ttt_T_2

    -- 이 다음에 할일은 dB 파일을 망가뜨리는 것이니 잘 따라 하시게...
    -- 우선 SQL Server 서비스 관리자에서 서비스를 중지한다.
    -- 다음 탐색기 d: 드라이브에서 ttt_F_2.ndf를 삭제.. 이는 ttt_FG_2를 임의로 망가뜨리는
    -- 것이라네..
    -- 그런 후, SQL Server 서비스 관리자에서 서비스를 시작해 보시게.
    -- EM을 열고 해당 서버에서 새로고침을 한번하고 데이터베이스를 열어보면 ttt_dB 아이콘이
    -- 회색으로 되면서
    -- (주의대상)이라고 표시됨.... 당근 사용이 불가...
    -- 이제부터 ttt_FG_2 그룹의 ttt_F_2 복구 시작.

    -- 주의! 파일및 파일그룹 백업의 복원은 선행적으로 로그 백업이 우선되어야 한다.
    -- dB가 망가졌는데 어캐 로그 백업을 하냐? 그건~ 만든사람 맘인거 같어~
    -- 암턴 해봐~ 잘돼~
    backup log ttt_dB
     to ttt_Device_L4
     with no_truncate, init  -- no_truncate 옵션 : 에러 직전까지의 로그 복원위한 옵션...
    go

    -- ttt_FG_2 파일 그룹 복원
    restore database ttt_dB
     filegroup = 'ttt_FG_2'
     from ttt_Device_2
     with norecovery
     -- norecovery 옵션 : 쉽게 말해 이 작업 다음에 또 복구할 작업이 있다...할때 쓴다.
    go

    -- ttt_FG_2에서 로그백업한 내용 복구
    restore log ttt_dB
     from ttt_Device_L3
     with norecovery
    go

    -- 파일및파일 그룹 복구 선행된 로그 백업 복구
    -- no_truncate 옵션으로 인해 에러 직전까지의 데이터 복구 가능...
    restore log ttt_dB
     from ttt_Device_L4
     with recovery
    go

    -- ##9. 푸하하하~!
    -- 확인해 보자공....
    select * from ttt_dB.dbo.ttt_T_0
    select * from ttt_dB.dbo.ttt_T_1
    select * from ttt_dB.dbo.ttt_T_2

반응형

+ Recent posts