snapshot:
snapshot api是Elasticsearch用於對數據進行備份和恢復的一組api接口,可以通過snapshot api進行跨集群的數據遷移,原理就是從源ES集群創建數據快照,然后在目標ES集群中進行恢復。
Snapshot and restore 模塊允許創建單個索引或者整個集群的快照到遠程倉庫.
快照一般建立在一個共享的文件系統上,這樣的話有一個節點快照,別的節點也是可以看到的,這樣刪除的時候也可以同時刪除,(在最新版里面創建倉庫如果未使用共享文件系統會提示你,在其他節點未找到對應的倉庫)。
一:利用snapshot-api
- 查看elasticsearch版本
curl -XGET 'slug:9200'
- 源elastcisearch集群中創建repository
創建快照前必須先創建repository倉庫,一個repository倉庫可以包含多份快照文件,repository主要有一下幾種類型
fs: 共享文件系統,將快照文件存放於文件系統中 url: 指定文件系統的URL路徑,支持協議:http,https,ftp,file,jar s3: AWS S3對象存儲,快照存放於S3中,以插件形式支持 hdfs: 快照存放於hdfs中,以插件形式支持
- elasticsearch配置文件elasticsearch.yml中設置倉庫路徑:
path.repo: /data/repository
- 調用snapshot api創建repository
curl -XPUT --header 'Content-Type: application/json' 'slug:9200/_snapshot/backup' -d ' { "type": "fs", "settings": { "location": "/data/repository", "compress": true } }'
注意location必須與path.repo相同,或其子目錄
這里注冊了一個名為backup的共享文件系統倉庫,快照會存儲在/data/repository目錄 - 一旦倉庫被注冊了,就可以只用下面的命令去獲取這個倉庫的信息
curl -XGET 'slug:9200/_snapshot/backup?pretty'
- 查看本集群中所有倉庫信息(我這里只有一個倉庫)
curl -XGET 'slug:9200/_snapshot/_all?pretty' curl -XGET 'slug:9200/_snapshot?pretty'
- 共享文件系統倉庫
共享文件系統倉庫 (
"type": "fs"
) 是使用共享的文件系統去存儲快照。 在location
參數里指定的具體存儲路徑必須和共享文件系統里的位置是一樣的並且能被所有的數據節點和master節點訪問。 另外還支持如下的一些參數設置:compress
max_restore_bytes_per_sec
location
指定快照的存儲位置。必須要有
指定是否對快照文件進行壓縮. 默認是
true
.chunk_size
如果需要在做快照的時候大文件可以被分解成幾塊。這個參數指明了每塊的字節數。也可用不同的單位標識。 比如,1g,10m,5k等。默認是
null
(表示不限制塊大小)。每個節點恢復數據的最高速度限制. 默認是
20mb/s
max_snapshot_bytes_per_sec
每個節點做快照的最高速度限制。默認是
20mb/s
- 一個倉庫可以包含同一個集群的多個快照。快照根據集群中的唯一名字進行區分。 在倉庫
backup
里創建一個名為getautoinfobyvin_2020 的快照可以通過下面的命令:
curl -XPUT --header 'Content-Type: application/json' 'slug:9200/_snapshot/backup/getautoinfobyvin_2020' -d ' { "indices": "getautoinfobyvin_2020", "include_global_state": false, "ignore_unavailable": "true", "partial": "false" }'
通過 indices 參數指定快照包含的索引,這個參數支持同時配置多個索引 multi index syntax. 快照請求同樣支持 ignore_unavailable 選項。把這個選項設置為 true 的時候在創建快照的過程中會忽略不存在的索引。默認情況下, 如果沒有設置 ignore_unavailable 在索引不存在的情況下快照請求將會失敗。通過設置 include_global_state 為false 能夠防止 集群的全局狀態被作為快照的一部分存儲起來。默認情況下,如果快照中的1個或多個索引不是全部主分片都可用會導致整個創建快照的過 程失敗。 通過設置 partial 為 true 可以改變這個行為。
- 通過如下的命令去獲得快照的信息
curl -XGET 'slug:9200/_snapshot/backup/getautoinfobyvin_2020?pretty'
快照創建需要時間,依據索引大小
- 通過如下的命令可以把倉庫里所有的快照列出來
curl -XGET 'slug:9200/_snapshot/backup/_all?pretty'
- 可以通過如下的命令將倉庫里的某個快照刪除:
curl -XDELETE 'slug:9200/_snapshot/backup/snapshot1?pretty'
- 查看快照具體信息
curl -XGET 'slug:9200/_snapshot/backup/getautoinfobyvin_2020/_status?pretty'
- 恢復快照中的所有索引
curl -XPOST 'slug:9200/indice_name/_close # 關閉indice curl -XPOST 'slug:9200/_snapshot/backup/snapshot1/_restore?pretty' curl -XPOST 'slug:9200/indice_name/_open # 開啟indice
- 恢復快照中的特定index
curl -XPOST 'slug:9200/_snapshot/backup/snapshot1/_restore' -d ' { "indices": "index_1,index_2", "ingnore_unavailable": true, "include_global_state": false, "rename_pattern": "index_(.+)", "rename_replacement": "restored_index_$1" }'
elsticdump:
-
#yum install nodejs #yum install npm
#npm install n -g
#g latest #npm install elasticdump [-g] - 將索引中的數據導出到本地
elasticdump --input=http://localhost:9200/demo --output=D:/ES/date/demo.json
- 將本地數據導入es中
elasticdump --input=D:/ES/date/demo.json --output=http://localhost:9200/demo1
-
'#拷貝analyzer如分詞 elasticdump \ --input=http://production.es.com:9200/my_index \ --output=http://staging.es.com:9200/my_index \ --type=analyzer '#拷貝映射 elasticdump \ --input=http://production.es.com:9200/my_index \ --output=http://staging.es.com:9200/my_index \ --type=mapping '#拷貝數據 elasticdump \ --input=http://production.es.com:9200/my_index \ --output=http://staging.es.com:9200/my_index \ --type=data
-
--input: 源地址,可為ES集群URL、文件或stdin,可指定索引,格式為:{protocol}://{host}:{port}/{index} --input-index: 源ES集群中的索引 --output: 目標地址,可為ES集群地址URL、文件或stdout,可指定索引,格式為:{protocol}://{host}:{port}/{index} --output-index: 目標ES集群的索引 --type: 遷移類型,默認為data,表明只遷移數據,可選settings, analyzer, data, mapping, alias --limit:每次向目標ES集群寫入數據的條數,不可設置的過大,以免bulk隊列寫滿
- 遷移單個索引
elasticdump --input=http://172.16.0.39:9200/companydatabase --output=http://172.16.0.20:9200/companydatabase --type=settings elasticdump --input=http://172.16.0.39:9200/companydatabase --output=http://172.16.0.20:9200/companydatabase --type=mapping elasticdump --input=http://172.16.0.39:9200/companydatabase --output=http://172.16.0.20:9200/companydatabase --type=data
- 遷移所有索引:以下操作通過elasticdump命令將將集群172.16.0.39中的所有索引遷移至集群172.16.0.20。 注意此操作並不能遷移索引的配置如分片數量和副本數量,必須對每個索引單獨進行配置的遷移,或者直接在目標集群中將索引創建完畢后再遷移數據
elasticdump --input=http://172.16.0.39:9200 --output=http://172.16.0.20:9200
logstash:
-
input { elasticsearch { hosts => [ "100.200.10.54:9200" ] index => "doc" size => 1000 scroll => "5m" docinfo => true scan => true } } filter { json { source => "message" remove_field => ["message"] } mutate { # rename field from 'name' to 'browser_name' rename => { "_id" => "wid" } } } output { elasticsearch { hosts => [ "100.20.32.45:9200" ] document_type => "docxinfo" index => "docx" } stdout { codec => "dots" } }
-
input { elasticsearch { hosts => ["http://172.16.0.39:9200"] index => "*" docinfo => true } } output { elasticsearch { hosts => ["http://172.16.0.20:9200"] index => "%{[@metadata][_index]}" } }
-
input { elasticsearch { hosts => ["http://172.16.20.78:9200"] index => "getautoinfobyvin_2020" docinfo => true } } filter { sleep { time => "20" every => "50" } } output { elasticsearch { hosts => ["http://192.168.100.13:9200"] index => "getautoinfobyvin_2020" } }
通過 indices
參數指定快照包含的索引,這個參數支持同時配置多個索引 multi index syntax. 快照請求同樣支持 ignore_unavailable
選項。把這個選項設置為 true
的時候在創建快照的過程中會忽略不存在的索引。默認情況下, 如果沒有設置 ignore_unavailable
在索引不存在的情況下快照請求將會失敗。通過設置 include_global_state
為false
能夠防止 集群的全局狀態被作為快照的一部分存儲起來。默認情況下,如果快照中的1個或多個索引不是全部主分片都可用會導致整個創建快照的過 程失敗。 通過設置 partial
為 true
可以改變這個行為。