#!/bin/bash

ES_ID=$(docker ps |grep elasticsearch:abcd|awk '{print $1}')

EXEC=( docker exec -it ${ES_ID})

if [ -n "${ES_ID}" ];then

    if ! (${EXEC[@]} curl -XGET -I http://localhost:9200/_snapshot/es_backup | grep 200 &> /dev/null);then
        ${EXEC[@]} curl -XPUT 'http://localhost:9200/_snapshot/es_backup' -H 'Content-Type: application/json' -d '{"type":"fs","settings":{"location":"/esdata/backup/es_backup", "compress":"true"}}'
    fi
    ${EXEC[@]} curl -XPUT "http://localhost:9200/_snapshot/es_backup/$(date +%Y%m%d%H%M%S)"
    if [ $? = 0 ];then
        echo "$(date)  elasticsearch backup success"
    else
        echo "$(date)  elasticsearch backup faile"
   fi
fi

