엘라스틱서치 인덱스 백업 및 복원 방법 / 스냅샷 생성 및 삭제 쿼리

스냅샷 레포지토리 폴더 생성

엘라스틱서치 설정파일 폴더로 이동

cd /etc/elasticsearch

스냅샷 레포지토리 폴더 생성

mkdir -p repo/레포지토리명

스냅샷 레포지토리 폴더 쓰기 권한 부여

chmod 777 -R repo

-R 옵션을 주면 하위 폴더들에도 권한이 부여됩니다.


스냅샷 레포지토리 등록

엘라스틱서치 설정파일 수정

vi /etc/elasticsearch/elasticsearch.yml

클러스터 모든 마스터 노드, 데이터 노드의 elasticsearch.yml 파일에서 동일하게 아래와 같이 수정해줘야 합니다.

path.repo: ["/etc/elasticsearch/repo/레포지토리명"] # 엘라스틱서치 스냅샷 저장 경로 등록

파일 하단에 스냅샷이 저장될 경로를 등록하고 elasticsearch를 재시작하여 반영해 줍니다.

스냅샷 레포지토리 등록 쿼리

PUT /_snapshot/레포지토리명
{
  "type": "fs",
  "settings": {
    "compress": true,
    "location": "/etc/elasticsearch/repo/레포지토리명"
  }
}

레포지토리 type 종류

파일시스템 fs
빅데이터 파일시스템 hdfs
아마존 s3
마이크로소프트 에저 azure
구글 클라우드 스토리지 gcs

인덱스 스냅샷 백업

클러스터 모든 인덱스 스냅샷 백업 쿼리

PUT _snapshot/레포지토리명/스냅샷명

특정 인덱스 스냅샷 백업 쿼리

PUT _snapshot/레포지토리명/스냅샷명
{
  "indices": "nori_*", # 이름이 nori_로 시작하는 모든 인덱스 스냅샷 찍기
  "ignore_unavailable": true
}

인덱스 스냅샷 복원

스냅샷으로 복원할 인덱스들 중 현재 인덱스와 같은 이름이 있다면 복원할 수 없다. 기존 인덱스를 지우고 복원해야 합니다.

스냅샷 모든 인덱스 복원

POST _snapshot/레포지토리명/스냅샷명/_restore

스냅샷 특정 인덱스 복원

POST _snapshot/레포지토리명/스냅샷명/_restore
{
  "indices": "nori_*", # 이름이 nori_로 시작하는 모든 인덱스
  "ignore_unavailable": true,
  "rename_pattern": "nori_(.+)",
  "rename_replacement": "nori_$1_restored" # 기존 인덱스명 뒤에 _restored 붙여서 복원
}

인덱스명을 변경하여 복원하는 예시입니다.

스냅샷 단일 인덱스 복원

POST _snapshot/레포지토리명/스냅샷명/_restore
{
  "indices": "인덱스명",
  "ignore_unavailable": true
}

스냅샷 복원 성공 메시지

{ "accepted": true }

스냅샷 복원 실패 메시지

snapshot_restore_exception

cannot restore index [인덱스명] because an open index with same name already exists in the cluster. Either close or delete the existing index or restore the index under a different name by providing a rename pattern and replacement name

스냅샷 목록 확인 쿼리

레포지토리 목록 확인

GET _snapshot/_all

레포지토리 스냅샷 목록 확인

GET _snapshot/레포지토리명/_all

스냅샷 인덱스 목록 확인

GET _snapshot/레포지토리명/스냅샷명

indices 중 왼쪽에 점(.)이 붙어있는 인덱스들은 무시해도 됩니다.


스냅샷 삭제 쿼리

레포지토리 삭제

DELETE _snapshot/레포지토리명

인덱스 스냅샷 삭제

DELETE _snapshot/레포지토리명/스냅샷명

Leave a comment