Backup và Restore dữ liệu Elasticsearch Index với Elasticdump – Cuongquach.com | Khi quản trị dịch vụ Elasicsearch, hẳn sẽ có những lúc bạn có nhu cầu migrate dữ liệu từ Elasticsearch này sang Elasticsearch nơi khác. Vậy hãy thử dump data Index Elasticsearch và restore từ data backup đó với ‘Elasticdump‘.
Backup Elasticsearch Index với Elasticdump
Github Repository: https://github.com/elasticsearch-dump/elasticsearch-dump
Chúng ta sẽ sử dụng Docker Image elasticsearch-dump
để dễ thao tác nhất. (Xem thêm: Hướng dẫn cài đặt Docker trên CentOS 7)
Giờ docker image elasticdump
.
# docker pull elasticdump/elasticsearch-dump
Tạo một thư mục để chứa data json dump bởi Elasticdump.
# mkdir -p /opt/elasticdump # chmod 755 /opt/elasticdump
Kiểm tra dữ liệu Index trong Elasticsearch host. Giả sử index là orders
.
# curl -X GET "http://172.30.2.132:9200/_cat/indices?v" health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open orders hbA-TQQ0QKqGFLESQo8x9Q 5 1 616719 15708 302.6mb 302.6mb
Backup dữ liệu Elastcisearch index ra file gồm Mapping và Data (có cả Analyzer Index nếu bạn muốn):
Đầu tiên là dữ liệu về Mapping của Index.
# docker run --rm -ti -v /opt/elasticdump/:/tmp elasticdump/elasticsearch-dump \ --input=http://172.30.2.132:9200/orders \ --output=/tmp/orders_mapping.json \ --type=mapping Mon, 22 Jun 2020 16:12:09 GMT | starting dump Mon, 22 Jun 2020 16:12:10 GMT | got 1 objects from source elasticsearch (offset: 0) Mon, 22 Jun 2020 16:12:10 GMT | sent 1 objects to destination file, wrote 1 Mon, 22 Jun 2020 16:12:10 GMT | got 0 objects from source elasticsearch (offset: 1) Mon, 22 Jun 2020 16:12:10 GMT | Total Writes: 1 Mon, 22 Jun 2020 16:12:10 GMT | dump complete
Kế đến là dữ liệu data document của Index ES.
# docker run --rm -ti -v /opt/elasticdump/:/tmp elasticdump/elasticsearch-dump \ --input=http://172.30.2.132:9200/orders \ --output=/tmp/orders_data.json \ --type=data \ --limit=300 Mon, 22 Jun 2020 16:25:50 GMT | starting dump Mon, 22 Jun 2020 16:25:51 GMT | got 300 objects from source elasticsearch (offset: 0) Mon, 22 Jun 2020 16:25:51 GMT | sent 300 objects to destination file, wrote 300 Mon, 22 Jun 2020 16:25:51 GMT | got 300 objects from source elasticsearch (offset: 300) Mon, 22 Jun 2020 16:25:51 GMT | sent 300 objects to destination file, wrote 300 Mon, 22 Jun 2020 16:25:52 GMT | got 300 objects from source elasticsearch (offset: 600) Mon, 22 Jun 2020 16:25:52 GMT | sent 300 objects to destination file, wrote 300 Mon, 22 Jun 2020 16:25:52 GMT | got 300 objects from source elasticsearch (offset: 900) Mon, 22 Jun 2020 16:25:52 GMT | sent 300 objects to destination file, wrote 300 Mon, 22 Jun 2020 16:25:53 GMT | got 300 objects from source elasticsearch (offset: 1200)Sau khi xong, bạn có thể review lại dữ liệu Mapping của Index .
# cat /opt/elasticdump/orders_mapping.json
Và kiểm tra số lượng data document của Index, mỗi 1 dòng là 1 chuỗi json chứa data 1 document trong Index mà bạn đã dump ra.
# cat /opt/elasticdump/orders_data.json | wc -l # cat /opt/elasticdump/orders_data.json | head -n 1
Mỗi một dòng trong orders_data.json, sẽ tương ứng data của 1 document với định dạng json.
Restore Elasticsearch Index với Elasticdump
Xoá Index ở Elasticsearch Cluster mà bạn chuẩn bị restore (nếu muốn xoá).
# curl -X DELETE http://10.79.2.193:9200/orders_restore {"acknowledged":true}
Đầu tiên ta restore Mapping của Index (bạn nên lưu ý case mapping giữa ES version 6.x và version 7.x).
# docker run --rm -ti -v /opt/elasticdump/:/tmp elasticdump/elasticsearch-dump \ --input=/tmp/orders_mapping.json \ --output=https://es.cuongquach.com/orders_restore \ --type=mapping
Kế đến restore data Index Elasticsearch Orders.
# docker run --rm -ti -v /opt/elasticdump/:/tmp elasticdump/elasticsearch-dump \ --input=/tmp/orders_data.json \ --output-index=orders \ --output=https://es.cuongquach.com/orders_restore \ --type=data \ --limit=10000
Rồi sau đó bạn chỉ cần query kiểm tra lại thông tin Index orders_restore
trên Elasticsearch Cluster mới gồm data storage, data document,… là xong rồi đấy. Trân trọng.
Nguồn: https://cuongquach.com/