반응형
/********************************************************************************************
-- Title : [PGS9.2] Point In Time Recovery(PITR) 시나리오 - ver.dBRang
-- Reference : dbrang.tistory.com
-- Key word : point in time recovery pitr backup recovery restore 온라인백업 복구 복원
샘플 데이터 추가 sample data
********************************************************************************************/
-- Title : [PGS9.2] Point In Time Recovery(PITR) 시나리오 - ver.dBRang
-- Reference : dbrang.tistory.com
-- Key word : point in time recovery pitr backup recovery restore 온라인백업 복구 복원
샘플 데이터 추가 sample data
********************************************************************************************/
**** Setting Initiate
***/
-------------------------
-- Setup WAL Archive mode
-------------------------
-- WAL Archive 모드 설정[p.1]
ㅇ 참조 : dbrang.tistory.com/756
-- WAL 아카이빙 확인
---------------------------------
-- Add time log and WAL Archiving
---------------------------------
-- 로그 테이블 생성[p.2]
-- drop table ttt_init;
Create Table Ttt_Init
( Id Serial Not Null Primary Key
, Step Varchar(50) Null
, Time Timestamp Null
) Tablespace Tbs_Data0;
-- 데이터 로그 추가
insert into ttt_init(step, time)
values ('WAL Archive Mode 후 1차 로그 추가', current_timestamp);
select * from ttt_init order by 1 desc limit 1;
id | step | time
----+-----------------------------------+---------------------------
1 | WAL Archive Mode 후 1차 로그 추가 | 2013-08-06 13:41:18.26808
-- 데이터 1차 추가(before full backup)
do
$$
declare
i int;
begin
i := 1;
loop
if i > 200000 then
exit;
end if; -- exit when i > 10;과 동일
insert into ttt_init(step, time) values ('2차 테스트 로그 데이터 저장', current_timestamp);
i := i + 1;
end loop;
end
$$;
select * from ttt_init order by 1 desc limit 1;
id | step | time
--------+-----------------------------+----------------------------
200001 | 2차 테스트 로그 데이터 저장 | 2013-08-06 13:41:47.976331
-- WAL 로그 파일 확인 확인
-- 현재 사용중인 XLog 확인
select pg_xlogfile_name(pg_current_xlog_location())
, pg_xlogfile_name_offset(pg_current_xlog_location());
pg_xlogfile_name | pg_xlogfile_name_offset
--------------------------+------------------------------------
000000010000000000000003 | (000000010000000000000003,9430552)
-- 로그 스위치[p.3]
select pg_switch_xlog(); -- "0/38FE634"
select pg_xlogfile_name('0/38FE634'), pg_xlogfile_name_offset('0/38FE634');
pg_xlogfile_name | pg_xlogfile_name_offset
--------------------------+------------------------------------
000000010000000000000003 | (000000010000000000000003,9430580)
-- 현재 사용중인 XLog 확인
select pg_xlogfile_name(pg_current_xlog_location())
, pg_xlogfile_name_offset(pg_current_xlog_location());
pg_xlogfile_name | pg_xlogfile_name_offset
--------------------------+-------------------------------------
000000010000000000000003 | (000000010000000000000003,16777216)
-- 데이터 로그 추가
insert into ttt_init(step, time) values ('전체 백업 전 3차 데이터 저장', current_timestamp);
select * from ttt_init order by 1 desc limit 1;
id | step | time
--------+------------------------------+----------------------------
200002 | 전체 백업 전 3차 데이터 저장 | 2013-08-06 13:44:05.113251
-- 테이블 추가 생성[p.4]
-- drop table ttt_before_full_backup;
create table ttt_before_full_backup
as select id, '전체 백업 전의 신규 테이블 생성' "step", current_timestamp "time"
from ttt_init;
-- 시간 체크
select * from ttt_before_full_backup order by 1 desc limit 1;
id | step | time
--------+---------------------------------+-------------------------------
200002 | 전체 백업 전의 신규 테이블 생성 | 2013-08-06 13:44:22.785721+09
-- WAL 로그 파일 확인
----------------------
-- Execute Full Backup
----------------------
-- 전체 백업 시작[p.5]
select pg_start_backup('full backup - 20130806');
pg_start_backup
-----------------
"0/6000020"
-- 전체 백업 시작 XLog 확인
select pg_xlogfile_name('0/6000020'), pg_xlogfile_name_offset('0/6000020');
pg_xlogfile_name | pg_xlogfile_name_offset
--------------------------+-------------------------------
000000010000000000000006 | (000000010000000000000006,32)
-- WAL 로그 파일 확인
# /home/postgres/pgsql/data/pg_xlog ==============================================
-- Full 백업(필요시 chown으로 postgres에 우선 지정)[p.6]
-- 반드시 server running 상태에서 진행
$ tar zcvfP /home/backup/pitr_backup/full_pgdata_20130806.tar.gz /home/postgres/pgsql/data
$ tar zcvfP /home/backup/pitr_backup/full_tbs_data0_20130806.tar.gz /home/database/tbs_data0
$ tar zcvfP /home/backup/pitr_backup/full_ARCHIVE_20130806.tar.gz /home/database/ARCHIVE
-- 전체 백업 종료[p.7]
select pg_stop_backup();
pg_start_backup
-----------------
"0/6000098"
-- 전체 백업시 처리된 XLog 확인
select pg_xlogfile_name('0/6000098'), pg_xlogfile_name_offset('0/6000098');
pg_xlogfile_name | pg_xlogfile_name_offset
--------------------------+--------------------------------
000000010000000000000006 | (000000010000000000000006,152)
-- WAL 로그 파일 확인
---------------------------------
-- Create Tabel after full backup
---------------------------------
-- 테이블 추가 생성[p.8]
-- drop table ttt_after_full_backup;
create table ttt_after_full_backup
as select id, '전체 백업 직후의 테이블 생성' "step", current_timestamp "time"
from ttt_before_full_backup;
-- 시간 체크
select * from ttt_after_full_backup order by 1 desc limit 1;
id | step | time
--------+------------------------------+-------------------------------
200002 | 전체 백업 직후의 테이블 생성 | 2013-08-06 13:48:36.354929+09
-- WAL 로그 파일 확인
-- 현재 사용중인 XLog 확인
select pg_xlogfile_name(pg_current_xlog_location())
, pg_xlogfile_name_offset(pg_current_xlog_location());
pg_xlogfile_name | pg_xlogfile_name_offset
--------------------------+------------------------------------
000000010000000000000008 | (000000010000000000000008,5750492)
-----------------------------------------
-- Create Another Table after full backup
-----------------------------------------
-- 테이블 추가 생성[p.9]
-- drop table ttt_after_full_backup_2;
create table ttt_after_full_backup_2
as select id, '전체 백업 직후 또한번 테이블 생성' "step", current_timestamp "time"
from ttt_after_full_backup;
-- 시간 체크
select * from ttt_after_full_backup_2 order by 1 desc limit 1;
id | step | time
--------+-----------------------------------+-------------------------------
200002 | 전체 백업 직후 또한번 테이블 생성 | 2013-08-06 13:52:05.724249+09
-- WAL 로그 파일 확인
-- 현재 사용중인 XLog 확인
select pg_xlogfile_name(pg_current_xlog_location())
, pg_xlogfile_name_offset(pg_current_xlog_location());
pg_xlogfile_name | pg_xlogfile_name_offset
--------------------------+-------------------------------------
000000010000000000000009 | (000000010000000000000009,12323520)
/***
**** Poin-In-Time-Recovery Tasks
***/
--------------------------
-- Disasters Come In[p.10]
--------------------------
-- CASE.1) postmaster.pid 프로세스 삭제
/*프로세스 확인*/
# ps -ef|grep /home/postgres/pgsql/bin/postgres
# echo $(head -1 /home/postgres/pgsql/data/postmaster.pid)
/*프로세스 삭제*/
# kill -9 {프로세스ID)
또는
# kill -9 $(head -1 /home/postgres/pgsql/data/postmaster.pid)
/*postgres 관련 프로세스 다 삭제*/
# ps -ef |grep
-- CASE.2) 기냥 시스템 재구동
# shutdown -r now
또는
$ pg_ctl stop
-- 서버 상태 확인
# pg_ctl status
pg_ctl: no server running
-------------------
-- Recovery by PITR
-------------------
-- Emergency Log Backup[p.11]
-- 사고 발생시 현재 상황의 로그 백업
$ mv /home/postgres/pgsql/data/pg_xlog /home/backup/pitr_backup/last_pg_xlog_20130806
$ mv /home/database/ARCHIVE /home/backup/pitr_backup/last_ARCHIVE_20130806
-- 기존 PGDATA 및 테이블스페이스 디렉토리의 이름 변경[p.12]
$ mv /home/postgres/pgsql/data /home/postgres/pgsql/data.bad_20130806
$ mv /home/database/tbs_data0 /home/database/tbs_data0.bad_20130806
-- 전체 백업본에서 파일 복원(할 필요 없지 않나? Tar 풀 때 생성될 듯)
$ mkdir /home/postgres/pgsql/data
$ mkdir /home/database/tbs_data0
$ mkdir /home/database/ARCHIVE
-- PGDATA 및 TBS_DATA0 복원[p.13]
$ tar zxvfP /home/backup/pitr_backup/full_tbs_data0_20130806.tar.gz
$ tar zxvfP /home/backup/pitr_backup/full_pgdata_20130806.tar.gz
$ tar zxvfP /home/backup/pitr_backup/full_ARCHIVE_20130806.tar.gz /*사실 불필요*/
# chown -R postgres.postgres /home/postgres/pgsql /*필요시*/
# chown -R postgres.postgres /home/database /*필요시*/
-- LAST pg_xlog 복원[p.14]
$ cp /home/backup/pitr_backup/last_pg_xlog_20130806/0* /home/postgres/pgsql/data/pg_xlog/
-- ARCHIVE 복원[p.15]
$ cp /home/backup/pitr_backup/last_ARCHIVE_20130806/0* /home/database/ARCHIVE/
$ mv /home/database/ARCHIVE /home/database/FULLARCHIVE
/*restore_command의 archive dest*/
$ mkdir /home/database/ARCHIVE
/*신규로 들어갈 archive dest*/
-- recovery.conf 작성[p.16]
-- recovery_target_time은 ttt_after_full_backup 생성 직전으로 설정
$ vi /home/postgres/pgsql/data/recovery.conf
restore_command = 'cp /home/database/FULLARCHIVE/%f %p'
recovery_target_time = '2013-08-06 13:50:00'
----------------------
-- Restart and Confirm
----------------------
-- 파일 정리
# ll -a /tmp
# rm -rf /tmp/.s.PGSQL* /*Port Lock 파일 있으면 삭제*/
# ll /home/postgres/pgsql/data/postmaster.pid
# rm -f /home/postgres/pgsql/data/postmaster.pid /*있으면 삭제*/
-- 혹시 아래 에러 발생시
# pg_ctl -D /home/postgres/pgsql/data start
FATAL: data directory "/home/postgres/pgsql/data" has group or world access
DETAIL: Permissions should be u=rwx (0700).
# chmod -R 700 /home/postgres/pgsql/data
# chmod 700 /home/database/tbs_data0
또는
# chown -R postges.postgres /home/postgres/pgsql/data
# chown -R postges.postgres /home/database/tbs_data0
/*chown으로 안되어서 chmod로 했더니 잘 될 때 있음*/
-- 에러 발생시 재기동 시작[p.17]
# pg_ctl start
-- Full Backup 직전으로 복구된 것 확인
-- ttt_after_full_backup 테이블 없음
=# \d
Schema | Name | Type | Owner
--------+------------------------+----------+--------
public | ttt_after_full_backup | table | mapbak
public | ttt_before_full_backup | table | mapbak
public | ttt_init | table | mapbak
public | ttt_init_id_seq | sequence | mapbak
-- WAL 로그 파일 확인[p.18]
반응형