--- title: Elasticsearch snapshot實作 tags: Elasticsearch description: View the slide with "Slide Mode". --- # Elasticsearch snapshot實作 :moyai: ## 實作 [Snapshot and restore](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/snapshot-restore.html) ## 1. [Register a snapshot repository](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/snapshots-register-repository.html) ### 1.1 ~~local repository~~ #### 1.1.1 建立本地 repository ```bash= $ mkdir /tmp/es_repo $ chown -R elasticsearch:elasticsearch /tmp/es_repo ``` #### 1.1.2 改`elasticsearch.yml` ```yml path.repo: ["/tmp/es_repo"] ``` #### 1.1.3 對 clsuter 設定 ```htmlmixed= curl -X PUT "localhost:9200/_snapshot/my_backup?pretty" -H 'Content-Type: application/json' -d' { "type": "fs", "settings": { "location": "/tmp/es_repo" } } ' ``` >[name=昱齊]會出錯~ 一定要shared filesystem ### 1.2 google cloud storage #### 1.2.1 安裝套件並重啟服務 ```bash $ /usr/share/elasticsearch/bin/elasticsearch-plugin install repository-gcs $ systemctl restart elasticsearch ``` #### 1.2.2 [gcs 官方教學](https://www.elastic.co/guide/en/elasticsearch/plugins/7.3/repository-gcs-usage.html) 1. 建立bucket -> 一個儲存單位 2. 建立服務帳戶,並取得json 金鑰 #### 1.2.3 將json金鑰放入`elasticsearch-keystore`,並重啟服務 ```bash= $ /usr/share/elasticsearch/bin/elasticsearch-keystore add-file gcs.client.default.credentials_file /etc/elasticsearch/金鑰檔案.json ``` 這邊只需要調整金鑰檔名即可 #### 1.2.4 登記 repository ```htmlmixed= curl -u elastic -X PUT "localhost:9200/_snapshot/my_backup?pretty" -H 'Content-Type: application/json' -d' { "type": "gcs", "settings": { "bucket": "if_efk", "client": "default" } } ' ``` - 可調整的:my_backup、bucket後面接的是gcs上面設定的名稱 ## 2. 建立快照 ```htmlmixed= curl -u elastic -X PUT "localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true&pretty" -H 'Content-Type: application/json' -d' { "indices": "test,.security-7", "ignore_unavailable": true, "include_global_state": false, "metadata": { "taken_by": "yq", "taken_because": "backup before upgrading" } } ' ``` ## 3. 還原快照 ### 3.1 不設參數 ```htmlmixed= curl -u elastic -X POST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore?pretty" ``` ### 3.2 設參數 ```htmlmixed= curl -u elastic -X POST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore?pretty" -H 'Content-Type: application/json' -d' { "indices": "test", "include_global_state": true } ' ``` > [name=昱齊]還有bug,暫時無法成功