Elasticsearch backup

Install JQ to make Json more human readable.

 
#!/bin/bash

# Backup of elasticsearch
# Scenario: make backup of every open index once a day

function write_to_stderr { 
    echo "$@" 1>&2
}

INDEX_TO_EXLUDE="^_\|SOME_SPECIAL_INDEX"
REPOSITORY="backup"

for INDEX_NAME in `curl 'localhost:9200/_cat/indices?v' 2>/dev/null | grep "\sopen\s" |  awk '{print $3}' `; do
	INDEX_LOG_FILE="/tmp/bck.$INDEX_NAME.log"
	IS_SPECIAL_INDEX=`echo "$INDEX_NAME" | grep -c "$INDEX_TO_EXLUDE" `
	RESULT=$?
	if [ "$RESULT" -eq "0" ]; then
		continue;
	fi
	SNAPSHOT_NAME="$INDEX_NAME@`date '+%Y.%m.%d-%H:%M'`"
	URL="http://localhost:9201/_snapshot/$REPOSITORY/$SNAPSHOT_NAME?wait_for_completion=true&pretty"
	COMMAND="{
		\"indices\": \"$INDEX_NAME\"
	}"
	# curl writes to stderr is transferred  cause elasticsearch write there some statistics
	curl --stderr "$INDEX_LOG_FILE".stats -XPUT "$URL" -d "$COMMAND" > "$INDEX_LOG_FILE"
	STATE=`grep -v "$SNAPSHOT_NAME" "$INDEX_LOG_FILE" | jq '.snapshot.state'`
	if [ "$STATE" != '"SUCCESS"' ]; then
		write_to_stderr $INDEX_NAME
	fi	
done
  1. No comments yet.

  1. No trackbacks yet.