es备份数据
備份ES數(shù)據(jù)的步驟:
1.創(chuàng)建共享目錄:
何為共享倉庫,其實(shí)就是集群中各個(gè)節(jié)點(diǎn)都能感知到并將數(shù)據(jù)寫入到該倉庫的文件。一般一個(gè)節(jié)點(diǎn)部署在一個(gè)服務(wù)器上,在哪里,怎樣創(chuàng)建一個(gè)文件讓各個(gè)節(jié)點(diǎn)都能往里面寫數(shù)據(jù)呢?
使用sshfs在ES集群中每個(gè)節(jié)點(diǎn)的相同位置掛載一個(gè)共享目錄。
// 在每個(gè)節(jié)點(diǎn)上安裝sshfssudo apt-get install sshfs// 選定一個(gè)節(jié)點(diǎn)的一個(gè)目錄作為共享目錄 mkdir /data/backup chmod -R 777 /data // 在每個(gè)節(jié)點(diǎn)的相同位置創(chuàng)建目錄,并掛載共享目錄 mkdir /mnt/backup chmod -R 777 /mnt sshfs $user@192.168.x.x:/data/backup /mnt/backup -o allow_other// 測試運(yùn)行ES的用戶是否有對(duì)共享目錄的寫權(quán)限 sudo -u elasticsearch touch /mnt/backup/test其中參數(shù)-o allow_other 解決了不同節(jié)點(diǎn)往共享倉庫中寫數(shù)據(jù)的權(quán)限問題。
2.修改elasticsearch.yml文件,添加path.repo配置
在elasticsearch.yml中增加
配置共享倉庫的位置,重啟節(jié)點(diǎn)
3.創(chuàng)建了共享目錄后就可以在這個(gè)共享目錄下為集群創(chuàng)建共享倉庫
// 創(chuàng)建倉庫 PUT _snapshot/my_backup {"type": "fs", "settings": {"location": "/mnt/backup","compress": true} }4.備份索引數(shù)據(jù)。
// 針對(duì)具體的index創(chuàng)建快照備份,其中snapshot_name 是快照的名字。 PUT _snapshot/my_backup/snapshot_name {"indices": "index_1, index_2" }// 5.查看備份狀態(tài) GET _snapshot/my_backup/snapshot_name/_status6.在不同集群之間遷移數(shù)據(jù)
// 備份創(chuàng)建好之后,在共享目錄/root/backup里是這樣的: -rw-r--r-- 1 root root 31 12月 15 22:14 index drwxr-xr-x 3 root root 4096 12月 15 22:14 indices -rw-r--r-- 1 root root 83 12月 15 22:14 metadata-snapshot_name -rw-r--r-- 1 root root 181 12月 15 22:14 snapshot-snapshot_name7.在遷移目標(biāo)的集群上重復(fù)上面創(chuàng)建倉庫的操作,即步驟2,3
// 8.(將源集群的備份內(nèi)容(/root/backup里的所有文件),復(fù)制到遷移目標(biāo)的集群倉庫目錄里) // 類似批量導(dǎo)入,所以只需要在主節(jié)點(diǎn)中恢復(fù)倉庫中的數(shù)據(jù)即可?// 9.使用RESTful API進(jìn)行備份的恢復(fù),如果索引已經(jīng)存在目標(biāo)的集群,需要先關(guān)閉索引,恢復(fù)數(shù)據(jù)后在開啟 POST /index_name/_close POST _snapshot/my_backup/snapshot_name/_restore POST /index_name/_open // 10.查看恢復(fù)的狀態(tài) GET _snapshot/my_backup/snapshot_name/_statuses備份api模板
public class SnapshotTest {private final RestHighLevelClient restClient;public SnapshotTest(RestHighLevelClient restClient) {this.restClient = restClient;}/*** 創(chuàng)建共享倉庫*/public void createRepository(){String locationKey = FsRepository.LOCATION_SETTING.getKey();//共享文件位置String locationValue = "/mnt/backup";String compressKey = FsRepository.COMPRESS_SETTING.getKey();boolean compressValue = true;Settings settings = Settings.builder().put(locationKey, locationValue).put(compressKey, compressValue).build();PutRepositoryRequest request = new PutRepositoryRequest();request.settings(settings);//倉庫名稱request.name("your_backup");request.type(FsRepository.TYPE);request.timeout(TimeValue.timeValueMinutes(1));request.timeout("1m");request.verify(true);AcknowledgedResponse response;try {response = restClient.snapshot().createRepository(request, RequestOptions.DEFAULT);boolean acknowledged = response.isAcknowledged();System.out.println(acknowledged+"成功");} catch (IOException e) {System.out.println("創(chuàng)建共享倉庫失敗");e.printStackTrace();}}/*** 創(chuàng)建快照*/public void createSnapShoot(){CreateSnapshotRequest request = new CreateSnapshotRequest();//倉庫名稱request.repository("your_backup");//快照名稱request.snapshot("snapshot_20201203_sun");//快照索引request.indices("cpu-2020.12.02-000001");request.indicesOptions(IndicesOptions.fromOptions(false, false, true, true));request.partial(false);request.includeGlobalState(true);request.masterNodeTimeout(TimeValue.timeValueMinutes(1));request.masterNodeTimeout("1m");request.waitForCompletion(true);CreateSnapshotResponse response;try {response = restClient.snapshot().create(request, RequestOptions.DEFAULT);SnapshotInfo snapshotInfo = response.getSnapshotInfo();List<String> indices = snapshotInfo.indices();RestStatus status = response.status();int status1 = status.getStatus();} catch (IOException e) {e.printStackTrace();}}/*** 獲取快照*/public void getSnapShoot(){GetSnapshotsRequest request = new GetSnapshotsRequest();//倉庫名稱request.repository("your_backup");String[] snapshots = { "snapshot_20201203_sun" };request.snapshots(snapshots);request.masterNodeTimeout(TimeValue.timeValueMinutes(1));request.masterNodeTimeout("1m");request.verbose(true);request.ignoreUnavailable(false);GetSnapshotsResponse response;try {response = restClient.snapshot().get(request, RequestOptions.DEFAULT);List<SnapshotInfo> snapshotsInfos = response.getSnapshots();SnapshotInfo snapshotInfo = snapshotsInfos.get(0);//快照的REST狀態(tài)RestStatus restStatus = snapshotInfo.status();//快照編號(hào)SnapshotId snapshotId = snapshotInfo.snapshotId();//快照的當(dāng)前狀態(tài)SnapshotState snapshotState = snapshotInfo.state();//有關(guān)分片快照過程中發(fā)生的故障的信息List<SnapshotShardFailure> snapshotShardFailures = snapshotInfo.shardFailures();//快照開始時(shí)間long startTime = snapshotInfo.startTime();//快照結(jié)束時(shí)間long endTime = snapshotInfo.endTime();} catch (IOException e) {System.out.println("獲取快照失敗");e.printStackTrace();}}/*** 恢復(fù)快照*/public void restoreSnapShoot(){RestoreSnapshotRequest request = new RestoreSnapshotRequest("your_backup", "snapshot_20201203_sun");request.indices("cpu-2020.12.02-000001");request.renamePattern("cpu-2020.12.02-000001");request.renameReplacement("restored_sun");request.masterNodeTimeout(TimeValue.timeValueMinutes(1));request.masterNodeTimeout("1m");request.waitForCompletion(true);request.partial(false);request.includeGlobalState(false);request.includeAliases(false);RestoreSnapshotResponse response;try {response = restClient.snapshot().restore(request, RequestOptions.DEFAULT);RestoreInfo restoreInfo = response.getRestoreInfo();List<String> indices = restoreInfo.indices();System.out.println(indices);int status = response.status().getStatus();System.out.println(status);} catch (Exception e) {System.out.println("快照恢復(fù)失敗");e.printStackTrace();}}/*** 獲取所有索引*/public void getAllIndices(){List<Object> list = new ArrayList<>();GetAliasesRequest request = new GetAliasesRequest();GetAliasesResponse alias = null;try {alias = restClient.indices().getAlias(request, RequestOptions.DEFAULT);} catch (IOException e) {e.printStackTrace();}Map<String, Set<AliasMetaData>> map = alias.getAliases();map.forEach((k, v) -> {//忽略elasticesearch 默認(rèn)的if (!k.startsWith(".")) {list.add(k);}});System.out.println("所有索引");for (Object o : list) {System.out.println(o);}}/*** 關(guān)閉索引*/public void closeIndices(){CloseIndexRequest request = new CloseIndexRequest("cpu-2020.12.02-000001");AcknowledgedResponse close;try {close = restClient.indices().close(request, RequestOptions.DEFAULT);boolean acknowledged = close.isAcknowledged();System.out.println(acknowledged);} catch (IOException e) {e.printStackTrace();}}}總結(jié)
- 上一篇: Go 实战 | 一文带你搞懂从单队列到优
- 下一篇: Oracle Tigger触发器 实例