Oracle Data Pump là một công cụ mạnh mẽ giúp xuất (export) và nhập (import) dữ liệu trong Oracle Database một cách hiệu quả. Trong đó, expdp
(Export Data Pump) là tiện ích dùng để xuất dữ liệu, metadata (siêu dữ liệu) và các đối tượng trong cơ sở dữ liệu. Bài viết này sẽ hướng dẫn chi tiết cách sử dụng expdp
để export database trong OracleDB, kèm theo các ví dụ thực tế và những lưu ý quan trọng.
expdp
là gì?
expdp
là một tiện ích dòng lệnh (command-line) cho phép bạn xuất dữ liệu và metadata từ một cơ sở dữ liệu Oracle. Nó là một phần của công nghệ Oracle Data Pump, cung cấp khả năng di chuyển dữ liệu tốc độ cao, song song và linh hoạt. So với công cụ exp
truyền thống, expdp
có nhiều ưu điểm vượt trội như hỗ trợ xử lý song song, nén dữ liệu và khả năng tạm dừng/tiếp tục công việc.
Các bước sử dụng expdp
để Export Database
1. Chuẩn bị
- Đảm bảo bạn có quyền truy cập vào Oracle Database với các quyền cần thiết (ví dụ:
CREATE SESSION
,EXP_FULL_DATABASE
). - Xác định thư mục (directory) trên máy chủ để lưu file export. Bạn có thể tạo một thư mục mới hoặc sử dụng thư mục có sẵn. (lưu ý cần truy cập vào máy chủ để tạo đường dẫn)
CREATE OR REPLACE DIRECTORY export_dir AS '/path/to/export';
GRANT READ, WRITE ON DIRECTORY export_dir TO your_user;
2. Cú pháp cơ bản của expdp
Cú pháp chung của expdp
như sau:
expdp username/password@database DIRECTORY=directory_name DUMPFILE=dumpfile_name.dmp LOGFILE=logfile_name.log [OPTIONS]
Trong đó:
username/password@database
: Thông tin đăng nhập vào database.DIRECTORY
: Tên thư mục đã tạo để lưu file export.DUMPFILE
: Tên file export (ví dụ:export_data.dmp
).LOGFILE
: Tên file log (ví dụ:export_log.log
).OPTIONS
: Các tùy chọn bổ sung (ví dụ:SCHEMAS
,TABLES
,FULL
).
Các ví dụ sử dụng expdp
1. Export toàn bộ database (FULL)
Để export toàn bộ database, sử dụng tùy chọn FULL=Y
.
expdp system/password@orcl DIRECTORY=export_dir DUMPFILE=full_export.dmp LOGFILE=full_export.log FULL=Y
2. Export một schema cụ thể
Để export một schema cụ thể, sử dụng tùy chọn SCHEMAS
.
expdp system/password@orcl DIRECTORY=export_dir DUMPFILE=schema_export.dmp LOGFILE=schema_export.log SCHEMAS=hr
3. Export một hoặc nhiều bảng
Để export một hoặc nhiều bảng cụ thể, sử dụng tùy chọn TABLES
.
expdp system/password@orcl DIRECTORY=export_dir DUMPFILE=tables_export.dmp LOGFILE=tables_export.log TABLES=hr.employees,hr.departments
4. Export với nén dữ liệu
Để nén file export, sử dụng tùy chọn COMPRESSION
.
expdp system/password@orcl DIRECTORY=export_dir DUMPFILE=compressed_export.dmp LOGFILE=compressed_export.log SCHEMAS=hr COMPRESSION=ALL
5. Export song song (Parallel)
Để tăng tốc độ export, sử dụng tùy chọn PARALLEL
.
expdp system/password@orcl DIRECTORY=export_dir DUMPFILE=parallel_export.dmp LOGFILE=parallel_export.log SCHEMAS=hr PARALLEL=4
Các tùy chọn phổ biến khác
- INCLUDE: Chỉ export các đối tượng cụ thể (ví dụ:
INCLUDE=TABLE:"IN ('EMPLOYEES', 'DEPARTMENTS')"
). - EXCLUDE: Loại trừ các đối tượng không muốn export (ví dụ:
EXCLUDE=INDEX
). - CONTENT: Chỉ export dữ liệu (
DATA_ONLY
) hoặc metadata (METADATA_ONLY
). - JOB_NAME: Đặt tên cho job export (ví dụ:
JOB_NAME=export_job
).
Quản lý Job Export
- Theo dõi tiến trình: Sử dụng lệnh
expdp
với tùy chọnATTACH
để theo dõi hoặc quản lý job đang chạy.
expdp system/password@orcl ATTACH=export_job
- Tạm dừng/tiếp tục job: Sử dụng lệnh
STOP_JOB
vàSTART_JOB
trong chế độATTACH
.
Lưu ý quan trọng
- Đảm bảo có đủ không gian lưu trữ trên thư mục export.
- Kiểm tra file log để phát hiện lỗi (nếu có).
- Sử dụng tùy chọn
PARALLEL
vàCOMPRESSION
để tối ưu hóa hiệu suất. - Đối với database lớn, nên chạy
expdp
vào thời điểm ít người dùng để tránh ảnh hưởng đến hiệu suất hệ thống.
Kết luận
expdp
là một công cụ mạnh mẽ và linh hoạt để export dữ liệu trong Oracle Database. Bằng cách nắm vững các tùy chọn và cú pháp của expdp
, bạn có thể dễ dàng xuất dữ liệu theo nhu cầu của mình.