1. Bastion VM pgcopydb 설치 및 연결 테스트
2. Source DB유저에게 모든 sequence 권한 부여
3. Target DB유저에게 임시로 superuser 권한 부여
4. DB 동기화
5. 복제 상황 모니터링
6. 복제 지연 최소화 대기 및 데이터 검증
7. api, worker, cron pod 제거
8. 복제 지연 없음 확인
9. pgcopydb 프로세스 종료
10. replication slot, origin 삭제
11. target DB유저 superuser 권한 제거
12. api, worker, cron 재배포
순서대로 차근차근 진행해보자
1. Bastion VM pgcopydb 설치 및 연결 테스트
sudo apt update
sudo apt install pgcopydb
mkdir -p pgcopydb-migration
cd pgcopydb-migration
export PGCOPYDB_SOURCE_PGURI="postgres://source-db:thisispassword@127.0.0.1:5432/source-db"export PGCOPYDB_TARGET_PGURI="postgres://target-db:thisispassword@127.0.0.1:5433/target-db"
pgcopydb ping --source"$PGCOPYDB_SOURCE_PGURI" --target "$PGCOPYDB_TARGET_PGURI"# 기대 출력## INFO Successfully could connect to source database## INFO Successfully could connect to target database
2. Source DB유저에게 모든 sequence 권한 부여
-- source-db 유저에게 모든 sequence 권한 부여GRANTSELECT, USAGE ONALL SEQUENCES IN SCHEMA public TO "source-db";
3. Target DB유저에게 임시로 superuser 권한 부여
-- 임시로 superuser 권한 부여ALTERUSER "target-db" WITH SUPERUSER;
4. DB 동기화
nohuppgcopydb clone --follow \
--source "$PGCOPYDB_SOURCE_PGURI" \
--target "$PGCOPYDB_TARGET_PGURI" \
--no-owner \
--no-acl \
--table-jobs 1 \
--index-jobs 1 \
--verbose \
--dir . \
> ./pgcopydb-migration.log 2>&1 &
# 프로세스 ID 저장echo$! > ./pgcopydb-migration.pid
# 실시간 로그 확인tail-f ./pgcopydb-migration.log
# 진행 상황 확인./replication_monitoring.sh
6. 복제 지연 최소화 대기 및 데이터 검증
복제지연 확인
SELECT
slot_name,
active,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)) as lag_size,
pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn) as lag_bytes
FROM pg_replication_slots
WHERE slot_name ='pgcopydb';
-- Result --
slot_name | active | lag_size | lag_bytes
-----------+--------+----------+-----------
pgcopydb | t |541 kB |553920
(1row)
데이터 일관성 검증
pgcopydb compare schema --verbose
pgcopydb compare data --verbose
약 541KB 만큼의 복제 지연이 있다는 의미이다.
# 데이터 정합성 확인
pgcopydb compare data
# Output
16:11:44.270162208 INFO Running pgcopydb version 0.17-1.pgdg22.04+1from "/usr/bin/pgcopydb"
16:11:44.357162208 INFO Using work dir "/tmp/pgcopydb"
16:11:44.358162208 INFO SOURCE: Connecting to "postgres://target-db@127.0.0.1:5432/target-db?keepalives=1&keepalives_idle=10&keepalives_interval=10&keepalives_count=60"
16:11:44.361162208 INFO Re-using catalog caches
16:11:44.365162208 INFO Starting 4table compare processes
Table Name |!| Source Checksum | Target Checksum
-------------------------------+---+--------------------------------------+-------------------------------------
public.user ||9136aaf4-0f32-cd2b-850f-49270ff7c03 |9136aaf4-0f32-cd2b-850f-49270ff7c03