es备份单独的索引进行异机恢复

环境:
os:Centos 7
es(单机):源端6.5.0 目的端:6.8.5

说明:

a.目的端在运行ES有备份的话需要全部删除,确保目的端ES备份目录只保留源端备份过来的文件

 

1.确定备份的路径

源端(192.168.1.136):
path.repo: /home/hxl/single_elasticsearch/esbak

目的端(192.168.1.69):
path.repo: /home/middle/elasticsearch/esbak

 

2.源端进行备份
这里保证当前的es只保留一份备份(若保留多个备份,备份文件都是放同一个目录,无法区分具体是那个备份),因为我们这里是需要拷贝到远程机器恢复的保留多份的话,数据量比较大,压缩和解压,传输都消耗时间.
查看备份
curl -X GET "192.168.1.136:19200/_snapshot/esbackup/_all?pretty"
删除备份
curl -H "Content-Type: application/json" -XDELETE "http://192.168.1.136:19200/_snapshot/esbackup/snapshot_20250515"
curl -H "Content-Type: application/json" -XDELETE "http://192.168.1.136:19200/_snapshot/esbackup/snapshot_20250516"

##创建备份仓库目录
curl -H "Content-Type: application/json" -XPUT http://192.168.1.136:19200/_snapshot/esbackup -d'{
    "type": "fs", 
    "settings": {
        "location": "/home/hxl/single_elasticsearch/esbak"
    }
}'

##备份
curl -H "Content-Type: application/json" -XPUT http://192.168.1.136:19200/_snapshot/esbackup/snapshot_$now_date


curl -H 'Content-Type: application/json' -XPUT "192.168.1.136:19200/_snapshot/esbackup/mysomeindex_bak_20250516?wait_for_completion=true&pretty" -d'
{
  "indices": "inoculate_new,reservation_new"
}'


带上wait_for_completion=true终端窗口会一直等待命令完成,不带wait_for_completion=true参数的话,执行如下命令会理解返回,备份在后台进行

curl -H 'Content-Type: application/json' -XPUT "192.168.1.136:19200/_snapshot/esbackup/mysomeindex_bak_20250516?pretty" -d'
{
  "indices": "inoculate_new,reservation_new"
}'

 

可以通过命令查看备份的情况

[hxl@localhost esbak]$ curl -X GET "192.168.1.136:19200/_snapshot/esbackup/_all?pretty"
{
  "snapshots" : [
    {
      "snapshot" : "mysomeindex_bak_20250516",
      "uuid" : "IQZX86BvStiOXG9eIjTQuw",
      "version_id" : 6050099,
      "version" : "6.5.0",
      "indices" : [
        "inoculate_new",
        "reservation_new"
      ],
      "include_global_state" : true,
      "state" : "SUCCESS",
      "start_time" : "2025-05-16T02:32:50.034Z",
      "start_time_in_millis" : 1747362770034,
      "end_time" : "2025-05-16T02:32:59.034Z",
      "end_time_in_millis" : 1747362779034,
      "duration_in_millis" : 9000,
      "failures" : [ ],
      "shards" : {
        "total" : 10,
        "failed" : 0,
        "successful" : 10
      }
    }
  ]
}

 

备份完成后,这个时候可以查看备份,可以看到只备份了(inoculate_new,reservation_new)

[hxl@localhost esbak]$ curl -X GET "192.168.1.136:19200/_snapshot/esbackup/_all?pretty"
{
  "snapshots" : [
    {
      "snapshot" : "mysomeindex_bak_20250516",
      "uuid" : "IQZX86BvStiOXG9eIjTQuw",
      "version_id" : 6050099,
      "version" : "6.5.0",
      "indices" : [
        "inoculate_new",
        "reservation_new"
      ],
      "include_global_state" : true,
      "state" : "SUCCESS",
      "start_time" : "2025-05-16T02:32:50.034Z",
      "start_time_in_millis" : 1747362770034,
      "end_time" : "2025-05-16T02:32:59.034Z",
      "end_time_in_millis" : 1747362779034,
      "duration_in_millis" : 9000,
      "failures" : [ ],
      "shards" : {
        "total" : 10,
        "failed" : 0,
        "successful" : 10
      }
    }
  ]
}

 

3.对备份目录进行tar打包,然后传输到目的机器

[hxl@localhost single_elasticsearch]$ cd /home/hxl/single_elasticsearch
[hxl@localhost single_elasticsearch]$ tar -czvf mysomeindex_bak_20250516.tar.gz ./esbak

上传到目的机器,我们这里先放到临时目录
[hxl@localhost single_elasticsearch]$cd /home/hxl/single_elasticsearch
[hxl@localhost single_elasticsearch]$scp mysomeindex_bak_20250516.tar.gz root@192.168.1.69:/tmp/

 

4.修改传输到目标机器文件的权限

[root@hxl-biz-b42060e-test tmp]#cd /tmp
[root@hxl-biz-b42060e-test tmp]#chown hxl:hxl ./mysomeindex_bak_20250516.tar.gz

 

5.解压到备份目录
查看压缩文件的路径情况

[root@hxl-biz-b42060e-test tmp]# su - hxl
[hxl@hxl-biz-b42060e-test tmp]$ tar -tvf mysomeindex_bak_20250516.tar.gz 
drwxrwxr-x hxl/hxl   0 2025-05-16 10:32 ./esbak/
drwxr-xr-x hxl/hxl   0 2025-05-16 10:32 ./esbak/indices/
...

 

6.若目的端已经有备份需要进行删除(若没有可以跳过)

删除存在的备份
curl -u elastic:hxl123 -H "Content-Type: application/json" -XDELETE "http://192.168.1.69:19200/_snapshot/esbackup/mysomeindex_bak_20250516" {"acknowledged":true}

查看是否还有没有备份 [hxl@hxl-biz-b42060e-test esbak]$ curl -u elastic:hxl123 -X GET "192.168.1.69:19200/_snapshot/esbackup/_all?pretty" { "snapshots" : [ ] }

然后检查下目的端的ES备份目录是否为空

[root@hxl-biz-b42060e-test esbak]# pwd
/home/middle/elasticsearch/esbak

 

7.解压备份文件并拷贝备份文件到目的端的备份目录

[hxl@hxl-biz-b42060e-test tmp]$ cd /tmp
[hxl@hxl-biz-b42060e-test tmp]$ tar --use-compress-program=pigz -xvf mysomeindex_bak_20250516.tar.gz

[hxl@hxl-biz-b42060e-test esbak]$ pwd
/tmp/esbak
[hxl@hxl-biz-b42060e-test esbak]$ mv * /home/middle/elasticsearch/esbak/

 

8.查看目的端ES注册的备份信息

[hxl@hxl-biz-b42060e-test esbak]$ curl -u elastic:hxl123 -X GET "192.168.1.69:19200/_snapshot/esbackup/_all?pretty"
{
  "snapshots" : [
    {
      "snapshot" : "mysomeindex_bak_20250516",
      "uuid" : "IQZX86BvStiOXG9eIjTQuw",
      "version_id" : 6050099,
      "version" : "6.5.0",
      "indices" : [
        "inoculate_new",
        "reservation_new"
      ],
      "include_global_state" : true,
      "state" : "SUCCESS",
      "start_time" : "2025-05-16T02:32:50.034Z",
      "start_time_in_millis" : 1747362770034,
      "end_time" : "2025-05-16T02:32:59.034Z",
      "end_time_in_millis" : 1747362779034,
      "duration_in_millis" : 9000,
      "failures" : [ ],
      "shards" : {
        "total" : 10,
        "failed" : 0,
        "successful" : 10
      }
    }
  ]
}

 

9.恢复

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

curl -u elastic:hxl123 -XPOST "http://192.168.1.69:19200/_snapshot/esbackup/mysomeindex_bak_20250516/_restore?wait_for_completion=true" -H 'Content-Type: application/json' -d'
{
  "indices": "inoculate_new,reservation_new"
}'

 

恢复后通过如下命令查看

查看
curl -u elastic:hxl123 -X GET 'http://192.168.1.69:19200/_cat/indices?v'

 

posted @ 2025-05-16 16:48  slnngk  阅读(62)  评论(0)    收藏  举报