/********************************************************************************************
-- Title : [PGS9.2] Cold Backup 및 Recovery - ver.dBRang
-- Reference : dbrang.tistory.com
-- Key word : cold backup 콜드
********************************************************************************************/
/***
**** 환경 구성
***/
----------------------------------------
-- postgres에 테이블 생성 및 데이터 입력
----------------------------------------
create table postgres_ttt(a int, b int);
insert into postgres_ttt values (1,1);
insert into postgres_ttt values (2,2);
insert into postgres_ttt values (3,3);
select * from postgres_ttt;
---------------------------
-- tttdb생성 및 테이블 생성
---------------------------
create database tttdb owner = sys tablespace = tbs_data0;
=# \connect tttdb;
create table tttdb_ttt(a int, b int);
insert into tttdb_ttt values (1,1);
insert into tttdb_ttt values (2,2);
insert into tttdb_ttt values (3,3);
select * from tttdb_ttt;
create table tttdb_ttt_on_tbs_data3
( a int
, b int
) tablespace tbs_data3;
insert into tttdb_ttt_on_tbs_data3 values (1,1);
insert into tttdb_ttt_on_tbs_data3 values (2,2);
insert into tttdb_ttt_on_tbs_data3 values (3,3);
select * from tttdb_ttt_on_tbs_data3;
/***
**** Cold Backup
***/
----------------------
-- 기본 파일 경로 확인
----------------------
select name, setting from pg_settings
where name in ('config_file', 'hba_file', 'ident_file', 'archive_command', 'data_directory');
-- 테이블스페이스 경로 확인
select pg_get_userbyid(t.spcowner) "tbs_ownr", t.oid "tbs_id", t.spcname "tbs_nm"
, pg_size_pretty(pg_tablespace_size(t.oid)) "tbs_sz"
, case when pg_tablespace_location(t.oid) = ''
then u.tbs_location
else pg_tablespace_location(t.oid)
end "ts_location"
from pg_tablespace t
left join (select a.setting || '/' || case when b.tbs_nm = 'default'
then 'base' else b.tbs_nm
end "tbs_location"
, 'pg_' || b.tbs_nm "tbs_nm"
from pg_settings a
cross join (select 'default' "tbs_nm" union all
select 'global'
) b
where name = 'data_directory'
) u
on t.spcname = u.tbs_nm
order by t.spcname;
--------------------------
-- Cold Backup Script 작성
--------------------------
cp /home/postgres/pgsql/data/postgresql.conf /home/backup/130728/postgresql.conf
cp /home/postgres/pgsql/data/pg_hba.conf /home/backup/130728/pg_hba.conf
cp /home/postgres/pgsql/data/pg_ident.conf /home/backup/130728/pg_ident.conf
tar zcvfP /home/backup/130728/ARCHIVE.tar.gz /home/database/ARCHIVE
tar zcvfP /home/backup/130728/tbs_data0.tar.gz /home/database/tbs_data0
tar zcvfP /home/backup/130728/tbs_data1.tar.gz /home/database/tbs_data1
tar zcvfP /home/backup/130728/tbs_data2.tar.gz /home/database/tbs_data2
tar zcvfP /home/backup/130728/tbs_data3.tar.gz /home/database/tbs_data3
tar zcvfP /home/backup/130728/data.tar.gz /home/postgres/pgsql/data
/*"/home/postgres/pgsql"이하를 아예 하는게 좋지 않을까?*/
/*"/home/postgres/pgsql"이하를 아예 하는게 좋지 않을까?*/
-----------------
-- 접속 세션 확인
-----------------
=# \conenct postgres
SELECT datname, pid, usename, client_addr, client_port, waiting, state, query
FROM pg_stat_activity;
-----------------------
-- tttdb 접속 세션 삭제
-----------------------
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
where datname = 'tttdb';
----------------------
-- 서비스 중지 및 백업
----------------------
$ pg_ctl stop -m fast
-- Cold Backup
-- root로 실행
$ su - root
# Cold Backup Script 수행
/***
**** Cold Backup Recovery
***/
----------------------------------------------
-- shutdown 상태에서 postgresql.conf 삭제 복구
----------------------------------------------
-- postgresql.cong 삭제
$ rm -f postgresql.conf
$ pg_ctl start
postgres cannot access the server configuration file
"/home/postgres/pgsql/data/postgresql.conf": 그런 파일이나 디렉터리가 없습니다
-- 복구
# cp /home/backup/130728/postgresql.conf /home/postgres/pgsql/data/
# chown postgres.postgres /home/postgres/pgsql/data/postgresql.conf
# su - postgres
# pg_ctl start
-------------------------------
-- 운영중 PGDATA 폴더 삭제 복구
-------------------------------
=# /c tttdb;
select * from tttdb_ttt;
insert into tttdb_ttt
select 4,4 union all
select 5,5;
-- PGDATA 폴더 삭제
-- 계속해서 오류 발생됨
$ rm -rf /home/postgres/pgsql/data
LOG: could not open temporary statistics file
"pg_stat_tmp/pgstat.tmp": 그런 파일이나 디렉터리가 없습니다
LOG: archiver process (PID 24545) exited with exit code 1
WARNING: pgstat wait timeout
-- kill로 postgres 관련 프로세스 강제 종료
# ps -ef |grep postgres
# kill -9 {관련 프로세스}
$ pg_ctl status
no service running
-- 복구
# tar zxvfP /home/backup/130728/data.tar.gz
$ pg_ctl start
$ pg_ctl status
-- tttdb 확인
-- tttdb_ttt의 4,5행이 없어짐.
-- tttdb_ttt는 tbs_data0에 있어 PGDATA와 상관 없는데..
-- 아무래도 복구시 log에 의해 LSN 특정 시점으로 돌아간거?
=# \c tttdb
select * from tttdb_ttt;
----------------------
-- tbs_data3 폴더 삭제
----------------------
-- 확인
select * from tttdb_ttt_on_tbs_data3;
-- 폴더 삭제
# rm -rf /home/database/tbs_data3
-- 확인
$ pg_ctl status -- running
$ psql
=# \c tttdb
select * from tttdb_ttt;
select * from tttdb_ttt_on_tbs_data3;
ERROR: could not open file "pg_tblspc/16388/PG_9.2_201204301/16392/16396": 그런 파일이나 디렉터리가 없습니다
STATEMENT: select * from tttdb_ttt_on_tbs_data3;
ERROR: could not open file "pg_tblspc/16388/PG_9.2_201204301/16392/16396": 그런 파일이나 디렉터리가 없습니다
-- 확인
-- 삭제가 됐음에도 결과가 나온다. 메모리에서 읽나?
select * from tttdb_ttt_on_tbs_data3;
-- 데이터 추가 입력
Insert Into tttdb_ttt_on_tbs_data3 Values (111, 111);
-- 온라인 상태에서 복원
# tar zxvfP /home/backup/130728/tbs_data3.tar.gz
-- 확인
-- 111이 나온다. 역시 로그에서 복구하나보다.
select * from tttdb_ttt_on_tbs_data3;
/***
**** 결론
***/
-- 콜드백업 복구는 가능한 shutdown 상태에서 진행.
-- TBS 폴더가 없어도 해당 테이블을 건들지 않는한 오류 안 남.
-- TBS 폴더가 없어도 우선 메모리에서 읽어 들이는 경우 있음.
-- TBL에 쓸 때 폴더가 없으면 그 때 에러나 감.
-- TBS는 LSN에 적용을 받지 않는 듯...ㅡ.ㅡ''