나는 DB가 싫다ㅠㅠ 프론트 엔드 개발하고 싶어ㅠㅠ
하지만 이번에는 서버가 고장 날 걸 대비하여 데이터베이스를 다른 곳에 백업해두라는 일이 떨어졌다...
아니 DB 관리자를 뽑아줘........... 나 DB 하나도 모른단 말이야ㅠㅠㅠ
흑 이번 일은 정말 많이 굴렀다.
expdp
자, 우선 오라클이 설치된 서버의 오라클 계정으로 접속한다.
su - oralce
1. 백업할 SID를 확인한다.
//sid 확인
echo $ORACLE_SID
//sid 변경
export ORACLE_SID=dumptest
2. sqlpuls를 실행한다.
sqlplus / as sysdba
3. 덤프 파일을 생성할 유저를 생성하고 권한을 준다.
//impdp_user가 없을 경우
create user [impdp_user] identified by [impdp1234];
grant resource, dba, connect to [impdp_user];
grant datapump_imp_full_database to [impdp_user];
//user 확인
select username from all_users;
4. 디렉토리를 설정해 준다.
//pump_dir : 덤프파일 디렉토리명 impdp_user : 펌프유저명
create directory [export_dir] as '/u/datadump';
grant read, write on directory [export_dir] to impdp_user;
grant create any directory to system;
//directory 조회
select * from dba_directories;
//sqlplus 종료
exit
5. expdp 실행 ( 이 과정은 sqlplus에서 실행 X)
expdp impdp_user/impdp1234 directory=[export_dir] dumpfile=[test.dump] logfile=[export_220712_test.log] schemas=[testschemas]
expdp 옵션들
- logfile=[파일명]
- nologfile=yes
- nologfile=no (기본)
- content
- content=data_only
- content=metadata_only
- content=all (기본)
- table_exists_action
- content=data_only일 경우 해당 옵션 X
- table_exists_action = skip
- 테이블이 존재하는 경우 데이터는 가져오지 않음
- table_exists_action = truncate
- 테이블이 존재하는 경우, 기존 데이터 행을 내부적으로 길이를 줄여 데이터를 가져온다. 클러스터 테이블에서 사용 불가
- table_exists_action = append
- 테이블이 존재하는 경우, 기존 데이터 행을 변경하지 않고 가져옴
- table_exists_action = replace
- 테이블이 존재하는 경우, 테이블 자체를 내부적으로 drop 하고 다시 생성하여 데이터를 가져옴
- exclude
- exclude = index / table / constraint / grant
- remap_schema
- remap_schema = exprot 스키마 : import 스키마
- remap_tablespace
- remap_tablespace = export 테이블 스페이스 : import 테이블 스페이스
- remap_table
- remap_table = export 테이블 : import 테이블
- reuse_datafiles
- reuse_datafiles = yes
- reuse_datafiles = no (기본)
- data_options
- disable_append_hint
- append 힌트 무효화
- skip_constraint_errors
- 제약 조건 위반이 발생해도 롤백 하지 않고 가져옴
- disable_append_hint
- encryption_password
- 암호 지정
dump 파일 임포트 하기
impdp를 실행할 서버로 가서 sqlplus 접속
sqlplus / as sysdba
1. 우선 테이블 스페이스를 만들어줘야 한다.
//데이터의 용량이 적다면 원하는 경로가 있다면 경로를 지정해주고 경로를 지정해주지 않으면 기본 위치에 생성됨
create tablespace [테이블스페이스명] datafile '/opt/oracle/oradata/orcl/[파일명].dbf' size [30G] autoextend on next [5G] maxsize unlimited;
//데이터 용량이 많다면
CREATE BIGFILE TABLESPACE [테이블스페이스명] DATAFILE '[파일명].dbf' SIZE [50G] AUTOEXTEND ON EXTENT MANAGEMENT LOCAL AUTOALLOCATE SEGMENT SPACE MANAGEMENT AUTO;
//물리적 파일 경로 확인
SELECT FILE_ID,TABLESPACE_NAME,FILE_NAME from dba_data_files;
2. 스키마 생성
create user [유저명] identified by [비밀번호] default tablespace [테이블스페이스명] temporary tablespace temp;
grant CONNECT, RESOURCE to [유저명];
//sqlplus 접속해제
exit
3. 데이터를 가져오기 전에 틀을 먼저 생성해 줘야 한다.
impdp [impdp_user/impdp1234] directory=[export_dir] dumpfile=[220714_test.dump] logfile=[220729_import_test_metadata.log] remap_schema=[exportschema:importschema] exclude=ref_constraint, trigger, statistics, domain_index remap_tablespace=[exporttablespace:importtablespace] content=metadata_only
4. 데이터를 가져온다
impdp [impdp_user/impdp1234] directory=[export_dir] dumpfile=[220714_test.dump] logfile=[220729_import_test_data.log] remap_schema=[exportschema:importschema] exclude=ref_constraint, trigger, statistics, domain_index remap_tablespace=[exporttablespace:importtablespace] content=data_only
5. 인덱스 등을 가져온다.
impdp [impdp_user/impdp1234] directory=[export_dir] dumpfile=[220714_test.dump] logfile=[220729_import_test_ref.log] remap_schema=[exportschema:importschema] include=ref_constraint, trigger, domain_index remap_tablespace=[exporttablespace:importtablespace] content=metadata_only
★ expdp / impdp 작업 진행시 로그가 화면에 출력되는데 Control + C를 누르면 작업이 중단되는 것이 아니라 대화형 명령 모드(export > 프롬프트 / import > 프롬프트) 상태가 되는 데 작업을 모니터링하고 제어 가능 상태가 된다.
※ 대화형 명령 모드에서 사용되는 명령
1. CONTINUE_CLIENT
로그 출력이 화면에 다시 표시되는 일반 클라이언트로 다시 전환
(Interactive-command mode에서 logging mode로 변경)
2. EXIT_CLIENT
클라이언트는 닫히지만 데이터베이스 작업은 계속되므로 작업이 정상적으로 완료됨
3. KILL_JOB
모든 클라이언트를 분리하고 데이터베이스 작업을 종료(attach 되어있는 job을 detach 시키고 현재 돌아가는 job 중지)
4. STOP_JOB
기본적으로 현재 작업이 완료된 후 작업이 중지되고 나중에 재개할 수 있다.
STOP_JOB=IMMEDIATE 옵션을 사용하면 모든 작업 즉시 중단. 작업이 재개되면 작업의 일관성 유지 위해 작업 중 일부를 다시 실행해야 함
5. START_JOB
중지된 작업을 다시 시작(attach 되어 있는 job을 시작 가능)
6. STATUS
작업자의 상태를 포함하여 작업에 대한 기본 정보 표시
※ 실행 중인 JOB 조회 (job_name 조회)
select * from DBA_DATAPUMP_JOBS 뷰를 통하여 조회 가능
※ 작업 이름을 알고 나면 attach={job_name} 다음과 같이 매개변수를 사용하여 클라이언트를 작업에 연결 가능
-expdp impdp_user/impdp1234 attach=SYS_EXPORT_FULL_01
-impdp impdp_user/impdp1234 attach=SYS_IMPORT_FULL_01
'개발일기 > DB, SQL' 카테고리의 다른 글
SQL - DML (0) | 2024.04.08 |
---|---|
SQL - JOIN (0) | 2024.04.08 |
SQL - SELECT (0) | 2024.04.08 |
SQL - DDL (0) | 2024.04.08 |
S3 서버에 대용량 파일 업로드(feat, AWS CLI) - 내용 추가 (0) | 2022.08.13 |