# Backup and Restore

## 備份與還原

測試版本：5.6.3

建立資料夾

bitnami：需要加-H 'Content-Type: application/json'

```
-H 'Content-Type: application/json'
```

## 資料夾設定

```
# 打開資料夾所有權限  避免使用者權限問題
sudo chmod -R 777 [path]

# 檢視資料夾詳細設定
ls -al [path]
```

## elasticsearch.yml設定

更新elasticsearch.yml內容 (cluster每一台都要?)

```
# 新增備份絕對路徑
path.repo: ["/mnt/es_backup"]
```

設定完成後重啟es server

```
# bitnami環境
sudo service bitnami restart

# 檢查重啟是否成功
systemctl status bitnami.service
```

## Repository設定

### 建立repository

```
curl -XPUT 'http://[ip]:9200/_snapshot/[repository_name]' -d '{
     "type":"fs",
     "settings":{
         "location":"[backup_absolute_path]",
         "compress":true,
         "max_snapshot_bytes_per_sec" : "50mb", 
         "max_restore_bytes_per_sec" : "50mb"
     }
 }'

 # 可能會回傳一些error資訊，但依然可先查詢repository是否有設定成功
 # 可更改備份與恢復的速度，預設是20mb
 # 建立時使用PUT，而更新設定時則使用POST
```

### 檢查repository設定是否成功

```
 curl -XPOST http://[ip]:9200/_snapshot/[repository_name]/_verify?pretty
```

如果在建立或檢查repository時出現資料夾權限錯誤（repository verification exception）

可自行建立sshfs共享資料夾來解決（參考Linux/sshfs）

### 查詢所有repository設定

```
curl -XGET 'http://[ip]:9200/_snapshot?pretty'

# 已設定完成的結果
{
  "[backup_name]" : {
    "type" : "fs",
    "settings" : {
      "compress" : "true",
      "location" : "[backup_name]"
    }
  }
}
```

### 刪除註冊完成的repository

```
curl -XDELETE 'http://[ip]:9200/_snapshot/[repository_name]?pretty'

# 若repository已有snapshot，則無法直接刪除
```

## Snapshot設定

### 建立snapshot並自行命名，包含所有index

```
curl -XPUT 'http://[ip]:9200/_snapshot/[repository_name]/[snpashot_name]?wait_for_completion=true'

# 可從successful、failed欄位得知備份是否完全成功
# 快照只會處理狀態是open的index
```

### 建立特定index的snapshot

```
curl -XPUT 'http://[ip]:9200/_snapshot/[repository_name]/[snapshot_name]' -d '{"indices":"[index_name]"}'

# snapshot備份完成結果格式都相同
```

### 查詢snapshot建立進度

```
curl -XGET 'http://[ip]:9200/_snapshot/[repository_name]/[snapshot_name]/_status?pretty'
```

### 查詢所有snapshot版本

```
curl -XGET "[ip]:9200/_snapshot/[backup_name]/_all?pretty"

# 若已有儲存snapshot，可看到其相關資訊
{
    "snapshots": [
    {
        "snapshot": "snapshot版本名稱",
        "indices": [儲存的index],
        ...
    }
    ]
}

# 可從successful、failed欄位得知備份是否完全成功
```

### 刪除snapshot

```
curl -XDELETE 'http://140.96.68.132:9200/_snapshot/[repository_name]/[snapshot_name]'
```

## 恢復snapshot

### 列出所有index

```
curl -XGET '[ip]:9200/_cat/indices?v'
```

### 先刪除特定index

```
curl -XDELETE 'http://[ip]:9200/[index_name]'

# 刪除完成後再列出一次所有index，確認刪除成功
```

### 關閉index

```
# 恢復前需先關閉index
# 關閉所有index
curl -XPOST 'http://[ip]:9200/_all/_close'

# 關閉指定index
curl -XPOST 'http://[ip]:9200/[index_name]/_close'
```

### 指定snapshot進行恢復

```
curl -XPOST 'http://[ip]:9200/_snapshot/[repository_name]/[snapshot_name]/_restore?wait_for_completion=true"

# 成功可看到failed:0
# 恢復完成後再檢查一次index
# 在該次恢復完成前，無法再度執行其他snapshot版本的恢復
```

### 指定snapshot中的index進行恢復

```
curl -XPOST 'http://[ip]:9200/_snapshot/[repository_name]/[snapshot_name]/_restore' -d '{
    "indices": "[index_name]", 
    "rename_replacement": "[new_index]"
}‘
```

### 查看恢復進度

```
curl -XGET http://[ip]:9200/_recovery?pretty
```

參考連結：\
<https://docs.bitnami.com/virtual-machine/apps/elasticsearch/>\
<https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.html>\
<http://droidspa.blogspot.com/2016/11/elasticsearch-backup-and-restore.html>\
<http://www.itread01.com/content/1495128248.html>\
<http://keenwon.com/1393.html>\
<http://openskill.cn/article/468>\
<http://smallasa.com/2017/03/09/elasticsearch-backup-and-restore/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://stb11816.gitbook.io/python_note/database/elasticsearch/backup-and-restore.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
