Backup/FlashBack

Flashback Query, Flashback version query, Flashback Table

잇꼬 2024. 1. 31. 17:04
728x90
반응형
SMALL

■ Flashback Query
- 특정 시간에 query를 수행할 수 있다.
- select 문의 as of 절을 사용하여 데이터를 확인 할 시간 기록을 지정할 수 있다. 
- 데이터의 불일치 분석에 유용하다. 

#) scn 번호 확인

select current_scn from v$database;


#) scn 번호, 시간+날짜 정보 확인

select current_scn, systimestamp from v$database;



#) 테이블 생성 후 확인

create table hr.emp_30 as select * from hr.employees where department_id = 30;

=> scn/시간+날짜 체크하기

select count(*) from hr.emp_30;


#) update 문 + commit

update hr.emp_30 set salary = 30000 where employee_id = 114;
commit;


Commit complete.

#) scn번호 확인
SYS@ora11g>

select current_scn, systimestamp from v$database;


#) 114번의 급여 정보 확인

select salary from hr.emp_30 where employee_id = 114;


#) (상황)잘못 저장되었다! 
=> as of timestamp to_timestamp('2024-01-30 16:12:00', 'YYYY-MM-DD HH24:MI:SS') 절 사용!
=> update 하기 전의 시간을 봐야 한다.

select salary
from hr.emp_30 as of timestamp to_timestamp('2024-01-30 16:12:00', 'YYYY-MM-DD HH24:MI:SS')
where employee_id = 114;
select *
from hr.emp_30 as of timestamp to_timestamp('2024-01-30 16:12:30', 'YYYY-MM-DD HH24:MI:SS');


#) undo를 확인하면 undo_retention에서 시간 확인(900초) 

show parameter undo


NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
undo_management                      string      AUTO
undo_retention                       integer     900
undo_tablespace                      string      UNDO1

■ Flashback version query
version 절을 사용하여 두 point-in-time (시간대의 범위 시점, 일반적)또는 두 scn 사이에 존재하는 행의 모든 버전을 검색할 수 있다. 

#) 위에서 생성한 hr.emp_30 활용

select * from hr.emp_30;

#) current한 scn번호, 시간 확인 

select current_scn, systimestamp from v$database;


#) update문 + 시간 확인 

update hr.emp_30 set salary = salary * 1.1 where employee_id =115;
select current_scn, systimestamp from v$database;


#) delete문  + 시간 확인 

delete from hr.emp_30 where employee_id = 116;
select current_scn, systimestamp from v$database;


CURRENT_SCN SYSTIMESTAMP
----------- ---------------------------------------------------------------------------
    2400427 30-JAN-24 04.28.20.065195 PM +09:00

#) commit문 

commit;
select current_scn, systimestamp from v$database;


CURRENT_SCN SYSTIMESTAMP
----------- ---------------------------------------------------------------------------
    2400431 30-JAN-24 04.28.26.670051 PM +09:00

#) as of 절로 확인

select *
from hr.emp_30 as of timestamp to_timestamp('2024-01-30 16:28:00', 'YYYY-MM-DD HH24:MI:SS');


EMPLOYEE_ID FIRST_NAME           LAST_NAME                 EMAIL
----------- -------------------- ------------------------- -------------------------
PHONE_NUMBER         HIRE_DATE JOB_ID         SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
-------------------- --------- ---------- ---------- -------------- ---------- -------------
        114 Den                  Raphaely                  DRAPHEAL
515.127.4561         07-DEC-02 PU_MAN          30000                       100            30

        115 Alexander            Khoo                      AKHOO
515.127.4562         18-MAY-03 PU_CLERK         3100                       114            30

        116 Shelli               Baida                     SBAIDA
515.127.4563         24-DEC-05 PU_CLERK         2900                       114            30

        117 Sigal                Tobias                    STOBIAS
515.127.4564         24-JUL-05 PU_CLERK         2800                       114            30

        118 Guy                  Himuro                    GHIMURO
515.127.4565         15-NOV-06 PU_CLERK         2600                       114            30

        119 Karen                Colmenares                KCOLMENA
515.127.4566         10-AUG-07 PU_CLERK         2500                       114            30

#) scn번호로 확인

=> 정확한 시간을 알 수 없다면? 

=> between and 로 minvalue ~ maxvalue 로 scn 번호 유추할 수 있다! 

select versions_xid, employee_id, last_name, salary 
from hr.emp_30 versions between scn minvalue and maxvalue;


VERSIONS_XID     EMPLOYEE_ID LAST_NAME                     SALARY
---------------- ----------- ------------------------- ----------
1300150084010000         116 Baida                           2900
1300150084010000         115 Khoo                            3410
                         114 Raphaely                       30000
                         115 Khoo                            3100
                         116 Baida                           2900
                         117 Tobias                          2800
                         118 Himuro                          2600
                         119 Colmenares                      2500

8 rows selected.

#) 시간을 알고 있을 경우에
=> between timestamp to_timestamp() and timestamp to_timestamp(); 이용하면 된다.

select versions_xid, employee_id, last_name, salary 
from hr.emp_30 versions between timestamp to_timestamp('2024-01-30 16:28:00', 'YYYY-MM-DD HH24:MI:SS') and to_timestamp('2024-01-30 16:29:00', 'YYYY-MM-DD HH24:MI:SS');


VERSIONS_XID     EMPLOYEE_ID LAST_NAME                     SALARY
---------------- ----------- ------------------------- ----------
1300150084010000         116 Baida                           2900
1300150084010000         115 Khoo                            3410
                         114 Raphaely                       30000
                         115 Khoo                            3100
                         116 Baida                           2900
                         117 Tobias                          2800
                         118 Himuro                          2600
                         119 Colmenares                      2500


if) data 양이 적다면 insert, update문 으로 진행할 수 있다. 하지만, 전체 data를 수정해야 한다면?
■ Flashback Table(특정 시간대로 recover, =:불안전한 복구)
- 백업으로 복원하지 않고 테이블을 특정 시점으로 recovery 할 수 있다.
- 데이터베이스 온라인 상태를 유지 한다.(=: 복구를 온라인중에 가능하다)
- flashback table 작업을 수행하기 위해서 undo tablespace 에서 데이터를 사용한다.
  (단, 권한부여가 되어야 한다.)
- 객체권한 : ex) grant flashback on hr.emp_30 to scott;
- 시스템권한 : ex) grant flashback any table to hr; (=> 함부러 권한 부여하지 않는다.)
- flashback table 에 대한 행(row) 이동이 활성화 되어 있어야 한다.

#) SCN번호, 시간+날짜 확인

select current_scn, systimestamp from v$database;

CURRENT_SCN SYSTIMESTAMP
----------- ---------------------------------------------------------------------------
    2401084 30-JAN-24 04.46.38.070951 PM +09:00

#) delete 문

delete from hr.emp_30;
select current_scn, systimestamp from v$database;


CURRENT_SCN SYSTIMESTAMP
----------- ---------------------------------------------------------------------------
    2401107 30-JAN-24 04.47.33.687373 PM +09:00

#) commit 문

commit;
select current_scn, systimestamp from v$database;


CURRENT_SCN SYSTIMESTAMP
----------- ---------------------------------------------------------------------------
    2401118 30-JAN-24 04.47.40.744403 PM +09:00

#) 테이블 확인 

select * from hr.emp_30;
select current_scn, systimestamp from v$database;


CURRENT_SCN SYSTIMESTAMP
----------- ---------------------------------------------------------------------------
    2401124 30-JAN-24 04.47.51.499886 PM +09:00

#) flashback table으로 해결방안   
#1) 행 이동의 활성화 설정

alter table hr.emp_30 enable row movement;


#2) flashback table

flashback table hr.emp_30 to timestamp to_timestamp('2024-01-30 16:47:00', 'YYYY-MM-DD HH24:MI:SS');


#3) table 확인

select count(*) from hr.emp_30;
select * from hr.emp_30;


#4) 행의 이동 비활성화

alter table hr.emp_30 disable row movement;

 

728x90
반응형
LIST

'Backup > FlashBack' 카테고리의 다른 글

블록 손상(block corruption)  (1) 2024.01.31
Flashback Data Archive, Flashback Database  (0) 2024.01.31