Skip to content

Instantly share code, notes, and snippets.

@diegofcornejo
Created April 11, 2024 06:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegofcornejo/6ea18ee4242cffee688e38c3320fd7f7 to your computer and use it in GitHub Desktop.
Save diegofcornejo/6ea18ee4242cffee688e38c3320fd7f7 to your computer and use it in GitHub Desktop.
Elasticsearch: Snapshot and Restore

Create repository for snapshots

  • The location is the path where the snapshots will be stored, this path need to be set in the elasticsearch.yml file in the path.repo property
  • example: path.repo: ["/usr/share/elasticsearch/backup"]
  • After setting the path.repo property in the elasticsearch.yml file, restart the elasticsearch service
curl -XPUT -k -u elastic:changeme "https://localhost:9200/_snapshot/repository_backups" -H 'Content-Type: application/json' -d '{
  "type": "fs",
  "settings": {
    "location": "/usr/share/elasticsearch/backup"
  }
}'

Create a snapshot

Execute the following command to create a snapshot of the cluster state:

./create_snapshot.sh

The name of the snapshot is the date and time when the snapshot was created.

Restore a snapshot

First,you need the name of the snapshot you want to restore. You can get the list of snapshots by executing the following command:

curl -XGET -k -u elastic:changeme "https://localhost:9200/_snapshot/repository_backups/_all"

Execute the following command to restore a snapshot of the cluster state:

curl -XPOST -k -u elastic:changeme "https://localhost:9200/_snapshot/repository_backups/snapshot_1/_restore"
# snapshot_1 is the name of the snapshot in the list of snapshots

Some useful commands

Get the status of a snapshot

curl -XGET -k -u elastic:changeme "https://localhost:9200/_snapshot/repository_backups/snapshot_1"

Get the status of all snapshots

curl -XGET -k -u elastic:changeme "https://localhost:9200/_snapshot/repository_backups/_all"

Delete a snapshot

curl -XDELETE -k -u elastic:changeme "https://localhost:9200/_snapshot/repository_backups/snapshot_1"

Delete the repository and all snapshots

curl -XDELETE -k -u elastic:changeme "https://localhost:9200/_snapshot/repository_backups"

For restore the snapshots in other cluster, you need to create the repository in the other cluster and then restore the snapshot

  • Compres the backup folder and copy it to the other cluster
tar -czvf backup.tar.gz /usr/share/elasticsearch/backup
scp backup.tar.gz user@ip:/path
  • Extract the backup folder in the other cluster
tar -xzvf backup.tar.gz
  • Create the repository in the other cluster, same as the first cluster
  • Follow the steps to restore the snapshot
#!/bin/bash
# Configuration
ELASTICSEARCH_URL="https://localhost:9200"
ELASTICSEARCH_USER="elastic"
ELASTICSEARCH_PASSWORD="changeme"
REPOSITORY_NAME="repository_backups"
BACKUP_PREFIX="backup"
BACKUP_LOCATION="/usr/share/elasticsearch/backups"
# Logging function
log() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*"
}
log "Starting backup"
# Check if the backup directory exists, if not, create it
log "Verifying backup directory"
if [ ! -d "$BACKUP_LOCATION" ]; then
log "Backup directory does not exist"
log "Creating backup directory"
if ! mkdir -p "$BACKUP_LOCATION"; then
log "Failed to create backup directory"
exit 1
fi
log "Backup directory created successfully"
fi
# Get the current date in the format YYYYMMDDHHMMSS
DATE=$(date +'%Y%m%d%H%M%S')
# Create the full backup name
BACKUP_NAME="${BACKUP_PREFIX}_${DATE}"
# Perform the backup
log "Performing backup"
RESPONSE=$(curl -s -XPUT -k -u "${ELASTICSEARCH_USER}:${ELASTICSEARCH_PASSWORD}" "${ELASTICSEARCH_URL}/_snapshot/${REPOSITORY_NAME}/${BACKUP_NAME}")
if [ $? -ne 0 ]; then
log "Failed to perform backup: $RESPONSE"
exit 1
fi
log "Server response: $RESPONSE"
# Get the UUID of the snapshot
UUID=$(echo "$RESPONSE" | jq -r '.snapshot.uuid')
if [ -z "$UUID" ]; then
log "Failed to get UUID of the snapshot"
exit 1
fi
log "Snapshot UUID: $UUID"
# Check the backup status
log "Checking backup status"
STATUS=$(curl -s -XGET -k -u "${ELASTICSEARCH_USER}:${ELASTICSEARCH_PASSWORD}" "${ELASTICSEARCH_URL}/_snapshot/${REPOSITORY_NAME}/_status")
if [ $? -ne 0 ]; then
log "Failed to check backup status: $STATUS"
exit 1
fi
log "Backup status: $STATUS"
log "Backup completed successfully"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment