1. 程式人生 > 其它 >es備份恢復到另外的新機器(帶驗證模式)

es備份恢復到另外的新機器(帶驗證模式)

環境:
OS:Centos 7
原機器架構:3節點組成的叢集(帶驗證模式)
目的叢集架構:單節點(帶驗證模式)
es版本:6.8.5

1.原庫建立備份(叢集環境)
##建立備份倉庫目錄

curl -u elastic:elastic -H "Content-Type: application/json" -XPUT http://192.168.1.100:19200/_snapshot/esbackup -d'{
    "type": "fs", 
    "settings": {
        "location": "/home/middle/esbak/backup"
    }
}'

可以使用如下命令檢視備份的路徑

curl -u elastic:elastic -X GET "http://192.168.1.100:19200/_snapshot/esbackup?pretty"

##執行備份

curl -u elastic:elastic -H "Content-Type: application/json" -XPUT http://192.168.1.100:19200/_snapshot/esbackup/snapshot_20221208

注意叢集環境的備份目錄需要每個節點都可以有讀取寫入的許可權,一般採用nfs方式實現

2.檢視備份情況
可以使用如下命令檢視備份情況
curl -u elastic:elastic -X GET "http://192.168.1.100:19200/_snapshot/esbackup/_all?pretty"

3.將es備份目錄打包,然後上傳到新部署的機器上面
[elasticsearch@localhost single_elasticsearch]$ cd /home/elasticsearch/single_elasticsearch
tar -czvf esbak20221208.tar ./esbak
scp esbak20221208.tar [email protected]:/tmp/

4.新機器安裝部署es
安裝步驟省略,特別注意如下引數跟原來機器一致,因為我們等會將原來機器的備份檔案加壓到該目錄
path.repo: /home/middle/esbak/backup

5.將原主機備份的檔案解壓到新機器path.repo引數指定的目錄


su - elasticsearch
[elasticsearch@localhost tmp]$ cd /tmp/
[elasticsearch@localhost tmp]$ tar -xvf esbak_20221208.tar.gz
[elasticsearch@localhost esbak]$ cd /tmp/backup
[elasticsearch@localhost esbak]$ cp -r ./* /home/middle/esbak/backup/

6.刪除存在安全使用的index並去掉密碼驗證重啟動es
新的機器這個時候是沒有index的

[elasticsearch@pg1 backup]$ curl -u elastic:elastic -X GET 'http://192.168.1.104:19200/_cat/indices?v'
health status index       uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .security-6 _MTw15S4QoWX1nLKTPpi2g   1   0          6            0     19.5kb         19.5kb

因為我們這裡使用的是密碼驗證的,系統會生成.security-6這個索引,我們需要刪除該索引才能恢復

[elasticsearch@pg1 backup]$ curl -u elastic:elastic -XDELETE 'http://192.168.1.104:19200/.security-6?pretty'
{
  "acknowledged" : true
}

殺到會話
[elasticsearch@pg1 backup]$ kill 2136

註釋掉驗證部分
su - elasticsearch
vi /usr/local/services/elasticsearch/config/elasticsearch.yml

##xpack.security.enabled: true
##xpack.security.transport.ssl.enabled: true
##http.cors.enabled: true
##http.cors.allow-origin: "*"
##http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type

然後重新啟動
[elasticsearch@pg1 config]$ cd /usr/local/services/elasticsearch/bin
[elasticsearch@pg1 bin]$ ./elasticsearch -d

[elasticsearch@pg1 bin]$ curl -X GET 'http://192.168.1.104:19200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

這個時候就可以不使用密碼驗證了

6.執行恢復
執行恢復步驟
6.1 建立備份指定目錄

curl -H "Content-Type: application/json" -XPUT http://192.168.1.104:19200/_snapshot/esbackup -d'{
"type": "fs",
"settings": {
"location": "/home/middle/esbak/backup"
}
}'

6.2 檢視備份資訊,這個時候備份資訊已經註冊進來了

[elasticsearch@pg1 backup]$ curl -X GET "http://192.168.1.104:19200/_snapshot/esbackup/_all?pretty"
{
  "snapshots" : [
    {
      "snapshot" : "snapshot_20221208",
      "uuid" : "M7VkzUwNTu6nhOVJvu_9Dw",
      "version_id" : 6080599,
      "version" : "6.8.5",
      "indices" : [
        ".security-6",
        "hospital_info"
      ],
      "include_global_state" : true,
      "state" : "SUCCESS",
      "start_time" : "2022-12-07T17:00:02.869Z",
      "start_time_in_millis" : 1670432402869,
      "end_time" : "2022-12-07T17:00:04.887Z",
      "end_time_in_millis" : 1670432404887,
      "duration_in_millis" : 2018,
      "failures" : [ ],
      "shards" : {
        "total" : 6,
        "failed" : 0,
        "successful" : 6
      }
    }
  ]
}

6.3 執行恢復
[elasticsearch@pg1 bin]$ curl -XPOST http://192.168.1.104:19200/_snapshot/esbackup/snapshot_20221208/_restore
{"accepted":true}

7.驗證

[elasticsearch@pg1 bin]$ curl -X GET 'http://192.168.1.104:19200/_cat/indices?v'
health status index         uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .security-6   vjUKyjj2T2eW4ZZGsdW-4Q   1   0          6            0     19.6kb         19.6kb
yellow open   t_info Fnrd9wPISpe_dYdNCyPSEw   5   1      93325        11389     63.9mb         63.9mb

可以看到索引已經恢復了

8.重新啟用驗證
停掉es
[elasticsearch@pg1 bin]$ kill 2638
修改引數啟用密碼驗證
vi /usr/local/services/elasticsearch/config/elasticsearch.yml

xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type

重新啟動
[elasticsearch@pg1 config]$ cd /usr/local/services/elasticsearch/bin
[elasticsearch@pg1 bin]$ ./elasticsearch -d

這個時候需要賬號密碼登入了
curl -u elastic:舊密碼 -X GET 'http://192.168.1.104:19200/_cat/indices?v'

這裡的密碼是舊叢集的密碼

9.驗證資料

[elasticsearch@pg1 bin]$ curl -u elastic:舊密碼 -H "Content-Type: application/json" -XGET 'http://192.168.1.104:19200/_cat/count/t_info?v&format=json&pretty'
[
  {
    "epoch" : "1670467327",
    "timestamp" : "02:42:07",
    "count" : "93325"
  }
]

##################################恢復具體的某個索引##################################

1.恢復索引t_info

curl -u elastic:elastic -X POST "http://192.168.1.104:19200/_snapshot/esbackup/snapshot_20221208/_restore?pretty" -H 'Content-Type: application/json' -d'
{
  "indices": "t_info"
}
'

2.恢復的時候改名

curl -u elastic:elastic -X POST "http://192.168.1.104:19200/_snapshot/esbackup/snapshot_20221208/_restore?pretty" -H 'Content-Type: application/json' -d'
{
  "indices": "t_info",
  "rename_pattern": "t_info",
  "rename_replacement": "t_info01"
}
'

原來名稱t_info修改為t_info01

參考連線
https://www.elastic.co/guide/en/elasticsearch/reference/6.8/modules-snapshots.html

-- The End --