path.repo: /opt/elasticsearch/snapshots
mkdir -p /opt/elasticsearch/snapshots/repository curl -XPUT 'http://localhost:9200/_snapshot/repository' -H 'Content-Type: application/json' -d '{ "type": "fs", "settings": { "location": "repository", "compress": true } }'
mkdir -p /opt/elasticsearch/snapshots/recovery curl -XPUT 'http://localhost:9200/_snapshot/recovery' -H 'Content-Type: application/json' -d '{ "type": "fs", "settings": { "location": "recovery", "compress": true } }'
#!/bin/bash DAYS=31 # , , SNAPSHOT_DIRECTORY="/opt/elasticsearch/snapshots" BACKUP_DIR="/opt/elasticsearch/elasticsearch_backup" REPOSITORY="repository" LOG="/var/log/elasticsearch/elasticsearch_backup.log" DATE=`date` # , if ! [ -d $BACKUP_DIR ]; then mkdir -p $BACKUP_DIR fi # , $DAYS INDICES=`curator_cli --config /etc/elasticsearch/curator-config.yml --host localhost --port 9200 show_indices --filter_list "[{\"filtertype\":\"age\",\"source\":\"creation_date\",\"direction\":\"older\",\"unit\":\"days\",\"unit_count\":\"$DAYS\"},{\"filtertype\":\"kibana\",\"exclude\":\"True\"},{\"filtertype\":\"pattern\",\"kind\":\"regex\",\"value\":\"elastalert_status\",\"exclude\":\"True\"}]"` #, TEST_INDICES=`echo $INDICES | grep -q -i "error" && echo 1 || echo 0` if [ $TEST_INDICES == 1 ] then echo "$DATE " >> $LOG exit else # $INDICES for i in $INDICES do # $i curator_cli --config /etc/elasticsearch/curator-config.yml --timeout 600 --host localhost --port 9200 snapshot --repository $REPOSITORY --filter_list "{\"filtertype\":\"pattern\",\"kind\":\"regex\",\"value\":\"$i\"}" # $i SNAPSHOT=`curator_cli --config /etc/elasticsearch/curator-config.yml --host localhost --port 9200 show_snapshots --repository $REPOSITORY` # cd $SNAPSHOT_DIRECTORY/$REPOSITORY && tar cjf $BACKUP_DIR"/"$i".tar.bz" ./* # snapshot curator_cli --config /etc/elasticsearch/curator-config.yml --host localhost --port 9200 delete_snapshots --repository $REPOSITORY --filter_list "{\"filtertype\":\"pattern\",\"kind\":\"regex\",\"value\":\"$SNAPSHOT\"}" # curator_cli --config /etc/elasticsearch/curator-config.yml --host localhost --port 9200 delete_indices --filter_list "{\"filtertype\":\"pattern\",\"kind\":\"regex\",\"value\":\"$i\"}" # rm -rf $SNAPSHOT_DIRECTORY/$REPOSITORY/* done fi
#!/bin/bash # $DAYS # ! "-" . "yyyy.mm.dd". # : aaa_bbb.ccc-yyyy.mm.dd.tar.bz DAYS=91 BACKUP_DIR="/opt/elasticsearch/elasticsearch_backup" # THRESHOLD=$(date -d "$DAYS days ago" +%Y%m%d) #echo "THRESHOLD=$THRESHOLD" FILES=`ls -1 $BACKUP_DIR` TODELETE=`for i in $FILES; do echo $i | awk -F- '{printf "%s\n",$2 ;}' | awk -F. '{printf "%s%s%s \n",$1,$2,$3 ;}' | sed "s/$/$i/"; done` echo -e "$TODELETE" |\ while read DATE FILE do [[ $DATE -le $THRESHOLD ]] && rm -rf $BACKUP_DIR/$FILE done
0 1 * * * /bin/bash /opt/elasticsearch/backup_snapshot.sh >> /var/log/elasticsearch/elasticsearch_backup.log 0 3 * * * /bin/bash /opt/elasticsearch/delete_archive.sh >> /var/log/elasticsearch/elasticsearch_backup.log
#!/bin/bash # ARCHIVE=$1 BACKUP_DIR="/opt/elasticsearch/elasticsearch_backup" RECOVERY_DIR="/opt/elasticsearch/snapshots/recovery/" # rm -rf $RECOVERY_DIR/* # tar xjf $BACKUP_DIR/$ARCHIVE -C $RECOVERY_DIR # $SNAPSHOT SNAPSHOT=`curl -s -XGET "localhost:9200/_snapshot/recovery/_all?pretty" | jq '.snapshots[0].snapshot' | sed 's/\"//g'` # curl -XPOST "localhost:9200/_snapshot/recovery/$SNAPSHOT/_restore?pretty" # , Elasticsearch sleep 30 # curl -XDELETE "localhost:9200/_snapshot/recovery/$SNAPSHOT?pretty" # rm -rf $RECOVERY_DIR/*
Source: https://habr.com/ru/post/349192/