ES資料備份與恢復
阿新 • • 發佈:2020-08-12
目錄
Elasticsearch資料備份與恢復
前提
# 注意:
1.在進行本地備份時使用--type需要備份索引和資料(mapping,data)
2.在將資料備份到另外一臺ES節點時需要比本地備份多備份一種資料型別(analyzer,mapping,data,template)
一.本地備份與恢復
# 前提: · 必須要有Node環境和npm軟體:nodejs,npm 1.:下載包 curl --silent --location https://rpm.nodesource.com/setup_12.x | sudo bash - 2.:安裝包(如果本身有node包但是不是新版本建議解除安裝重灌) yum install -y nodejs 3.:驗證安裝是否成功 node -v 4.:設定淘寶映象 npm config set registry http://registry.npm.taobao.org 5.:安裝npm(只需要在一個節點安裝即可,如果前端還有nginx做反向代理可以每個節點都裝) [root@elkstack01 ~]# yum install -y npm 6.:進入下載head外掛程式碼目錄 [root@elkstack01 src]# cd /usr/local/ 7.:從GitHub上克隆程式碼到本地(或者直接從找es-head的包) [root@elkstack01 local]# git clone git://github.com/mobz/elasticsearch-head.git 8.:克隆完成後,進入elasticsearch外掛目錄 [root@elkstack01 local]# cd elasticsearch-head/ 9.:清除快取 [root@elkstack01 elasticsearch-head]# npm cache clean -f #使用npm安裝n模組(不同的專案js指令碼所需的node版本可能不同,所以就需要node版本管理工具) 10.安裝elasticdump [root@db04 ~]# npm install elasticdump -g 11.本地目錄備份 ## 第一次進行的是索引的備份 ## 第二次進行的是資料的備份 [root@db04 bin]# ./elasticdump --input=http://10.0.0.54:9200/student/user --output=/tmp/student_mapping.json --type=mapping Wed, 12 Aug 2020 07:41:59 GMT | starting dump Wed, 12 Aug 2020 07:41:59 GMT | got 1 objects from source elasticsearch (offset: 0) Wed, 12 Aug 2020 07:41:59 GMT | sent 1 objects to destination file, wrote 1 Wed, 12 Aug 2020 07:41:59 GMT | got 0 objects from source elasticsearch (offset: 1) Wed, 12 Aug 2020 07:41:59 GMT | Total Writes: 1 Wed, 12 Aug 2020 07:41:59 GMT | dump complete [root@db04 bin]# ./elasticdump --input=http://10.0.0.54:9200/student/user --output=/tmp/student_data.json --type=data Wed, 12 Aug 2020 07:42:21 GMT | starting dump Wed, 12 Aug 2020 07:42:21 GMT | got 8 objects from source elasticsearch (offset: 0) Wed, 12 Aug 2020 07:42:21 GMT | sent 8 objects to destination file, wrote 8 Wed, 12 Aug 2020 07:42:21 GMT | got 0 objects from source elasticsearch (offset: 8) Wed, 12 Aug 2020 07:42:21 GMT | Total Writes: 8 Wed, 12 Aug 2020 07:42:21 GMT | dump complete ##備份之後用Elasticsearch Head外掛模擬資料丟失,誤刪除student索引和資料並用elasticdump恢復 [root@db04 bin]# ./elasticdump --output=http://10.0.0.54:9200/student --input=/tmp/student_mapping.json --type=mapping Wed, 12 Aug 2020 07:46:21 GMT | starting dump Wed, 12 Aug 2020 07:46:21 GMT | got 1 objects from source file (offset: 0) Wed, 12 Aug 2020 07:46:21 GMT | sent 1 objects to destination elasticsearch, wrote 1 Wed, 12 Aug 2020 07:46:21 GMT | got 0 objects from source file (offset: 1) Wed, 12 Aug 2020 07:46:21 GMT | Total Writes: 1 Wed, 12 Aug 2020 07:46:21 GMT | dump complete [root@db04 bin]# ./elasticdump --output=http://10.0.0.54:9200/student --input=/tmp/student_data.json --type=data Wed, 12 Aug 2020 07:46:40 GMT | starting dump Wed, 12 Aug 2020 07:46:40 GMT | got 8 objects from source file (offset: 0) Wed, 12 Aug 2020 07:46:40 GMT | sent 8 objects to destination elasticsearch, wrote 8 Wed, 12 Aug 2020 07:46:40 GMT | got 0 objects from source file (offset: 8) Wed, 12 Aug 2020 07:46:40 GMT | Total Writes: 8 Wed, 12 Aug 2020 07:46:40 GMT | dump complete
# 根據索引名和型別進行備份
#備份成功。資料恢復成功
二.備份到另一臺ES節點(如果是同一叢集的節點索引不能重名)
# 備份到另一臺ES節點(同一叢集索引不能重名) [root@db04 bin]# ./elasticdump --input=http://10.0.0.54:9200/student --output=http://10.0.0.55:9200/students --type=analyzer Wed, 12 Aug 2020 08:14:00 GMT | starting dump Wed, 12 Aug 2020 08:14:00 GMT | got 1 objects from source elasticsearch (offset: 0) Wed, 12 Aug 2020 08:14:01 GMT | sent 1 objects to destination elasticsearch, wrote 1 Wed, 12 Aug 2020 08:14:01 GMT | got 0 objects from source elasticsearch (offset: 1) Wed, 12 Aug 2020 08:14:01 GMT | Total Writes: 1 Wed, 12 Aug 2020 08:14:01 GMT | dump complete [root@db04 bin]# ./elasticdump --input=http://10.0.0.54:9200/student --output=http://10.0.0.55:9200/students --type=mapping Wed, 12 Aug 2020 08:14:10 GMT | starting dump Wed, 12 Aug 2020 08:14:10 GMT | got 1 objects from source elasticsearch (offset: 0) Wed, 12 Aug 2020 08:14:10 GMT | sent 1 objects to destination elasticsearch, wrote 1 Wed, 12 Aug 2020 08:14:10 GMT | got 0 objects from source elasticsearch (offset: 1) Wed, 12 Aug 2020 08:14:10 GMT | Total Writes: 1 Wed, 12 Aug 2020 08:14:10 GMT | dump complete [root@db04 bin]# ./elasticdump --input=http://10.0.0.54:9200/student --output=http://10.0.0.55:9200/students --type=data Wed, 12 Aug 2020 08:14:15 GMT | starting dump Wed, 12 Aug 2020 08:14:15 GMT | got 8 objects from source elasticsearch (offset: 0) Wed, 12 Aug 2020 08:14:15 GMT | sent 8 objects to destination elasticsearch, wrote 8 Wed, 12 Aug 2020 08:14:15 GMT | got 0 objects from source elasticsearch (offset: 8) Wed, 12 Aug 2020 08:14:15 GMT | Total Writes: 8 Wed, 12 Aug 2020 08:14:15 GMT | dump complete [root@db04 bin]#elasticdump --input=http://10.0.0.54:9200/student --output=http://100.10.0.55:9200/students --type=template
三.本地備份與恢復指令碼
# 1.本地備份指令碼 [root@db04 ~]# vim output_Es.sh #!/bin/bash read -p '要備份的機器是:'${1} #要匯出的索引名 index_name=' student ' for index in `echo $index_name` do echo "start input index ${index}" elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_alias.json --type=alias &> /dev/null elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_analyzer.json --type=analyzer &> /dev/null elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_data.json --type=data &> /dev/null elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_alias.json --type=alias &> /dev/null elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_template.json --type=template &> /dev/null done # 2.恢復指令碼 [root@db04 ~]# vim input_Es.sh #!/bin/bash read -p '要匯入的機器是:'${1} #要匯入的索引名 index_name=' student ' for index in `echo $index_name` do echo "start input index ${index}" elasticdump --input=/data/${index}_alias.json --output=http://${1}:9200/${index} --type=alias &> /dev/null elasticdump --input=/data/${index}_analyzer.json --output=http://${1}:9200/${index} --type=analyzer &> /dev/null elasticdump --input=/data/${index}_data.json --output=http://${1}:9200/${index} --type=data &> /dev/null elasticdump --input=/data/${index}_template.json --output=http://${1}:9200/${index} --type=template &> /dev/null done
測試指令碼
# 0.配置環境變數
[root@db04 ~]# vim /etc/profile.d/elasticdump.sh
export PATH=/root/node-v10.13.0-linux-x64/lib/node_modules/elasticdump/bin:$PATH
[root@db04 ~]# source /etc/profile
# 1.建立備份目錄
[root@db04 ~]# mkdir /data
# 2.執行備份指令碼
[root@db04 ~]# sh output_Es.sh
要備份的機器是:10.0.0.54
start input index student
# 3.檢視備份目錄
[root@db04 ~]# ll /data/
total 16
-rw-r--r-- 1 root root 5 Aug 12 16:59 student_alias.json
-rw-r--r-- 1 root root 101 Aug 12 16:59 student_analyzer.json
-rw-r--r-- 1 root root 1284 Aug 12 16:59 student_data.json
-rw-r--r-- 1 root root 5 Aug 12 16:59 student_template.json
# 模擬誤刪除操作
# 執行恢復指令碼
[root@db04 ~]# sh input_Es.sh
要匯入的機器是:10.0.0.54
start input index student
## 資料恢復